UNPKG

docusaurus-plugin-typedoc-api

Version:

Docusaurus plugin that provides source code API documentation powered by TypeDoc.

218 lines (213 loc) 8.46 kB
'use strict'; function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } const react = require('react'); const marked = require('marked'); const markedSmartypants = require('marked-smartypants'); const client = require('@docusaurus/plugin-content-docs/client'); const CodeBlock = require('@theme/CodeBlock'); const MDX = require('@theme/MDXComponents'); const useReflectionMap = require('../hooks/useReflectionMap.js'); const markdown = require('../utils/markdown.js'); const jsxRuntime = require('react/jsx-runtime'); const _interopDefault = e => e && e.__esModule ? e : { default: e }; const CodeBlock__default = /*#__PURE__*/_interopDefault(CodeBlock); const MDX__default = /*#__PURE__*/_interopDefault(MDX); /* eslint-disable react/no-array-index-key */ const ADMONITION_START = /^:{3}([a-z]+)? *(.*)\n/; const ADMONITION_END = '\n:::'; marked.marked.setOptions({ gfm: true }); marked.marked.use(markedSmartypants.markedSmartypants()); marked.marked.use({ extensions: [{ name: 'admonition', level: 'block', start(src) { return src.match(ADMONITION_START)?.index; }, tokenizer(src, tokens) { const match = ADMONITION_START.exec(src); if (match) { const keyword = match[1] ?? 'info'; const title = match[2] ? match[2].trim() : undefined; const startIndex = match.index; const startEndIndex = startIndex + match[0].length; const endIndex = src.indexOf(ADMONITION_END, startEndIndex); const endEndIndex = endIndex + 4; const token = { type: 'admonition', raw: src.slice(startIndex, endEndIndex), text: src.slice(startEndIndex, endIndex), title, keyword, tokens: [] }; this.lexer.blockTokens(token.text, token.tokens); return token; } return undefined; } }] }); const TOKEN_TO_TAG = { blockquote: 'blockquote', br: 'br', code: 'pre', codespan: 'code', def: 'span', // ??? del: 'del', em: 'em', escape: 'span', // ??? hr: 'hr', html: 'div', paragraph: 'p', strong: 'strong', table: 'table', tablecell: 'td', tablerow: 'tr' }; function convertAstToElements(ast) { const elements = []; let counter = 0; // eslint-disable-next-line complexity ast.forEach(token => { // Nested tokens aren't typed for some reason... const children = token.tokens ?? []; switch (token.type) { case 'code': elements.push( /*#__PURE__*/ /* <MDX.pre key={counter} className={token.lang && `language-${token.lang}`}> {token.text} </MDX.pre>, */ jsxRuntime.jsx(CodeBlock__default.default, { language: token.lang, children: token.text }, counter)); break; case 'codespan': // Non-raw is escaped and doesn't work with JSX, so use the raw value // but remove the wrapping backticks! elements.push( /*#__PURE__*/jsxRuntime.jsx(MDX__default.default.code, { children: token.raw.slice(1, -1) }, counter)); break; case 'heading': { const Comp = MDX__default.default[`h${token.depth}`]; elements.push( /*#__PURE__*/jsxRuntime.jsx(Comp, { children: convertAstToElements(children) ?? token.text }, counter)); break; } case 'image': elements.push( /*#__PURE__*/jsxRuntime.jsx(MDX__default.default.img, { alt: token.title ?? '', src: token.href }, counter)); break; case 'link': elements.push( /*#__PURE__*/jsxRuntime.jsx(MDX__default.default.a, { href: token.href, title: token.title ?? '', children: convertAstToElements(children) ?? token.text }, counter)); break; case 'list': { const Comp = token.ordered ? 'ol' : 'ul'; elements.push( /*#__PURE__*/jsxRuntime.jsx(Comp, { children: convertAstToElements(token.items ?? children) }, counter)); break; } case 'list_item': elements.push( /*#__PURE__*/jsxRuntime.jsxs("li", { children: [token.task && /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("input", { checked: token.checked, type: "checkbox" }), ' '] }), convertAstToElements(children) ?? token.text] }, counter)); break; case 'space': elements.push(token.raw || ' '); break; case 'table': elements.push( /*#__PURE__*/jsxRuntime.jsxs("table", { children: [/*#__PURE__*/jsxRuntime.jsx("thead", { children: /*#__PURE__*/jsxRuntime.jsx("tr", { children: token.header.map((h, i) => /*#__PURE__*/jsxRuntime.jsx("th", { align: token.align[i] ?? 'left', children: convertAstToElements(h.tokens) }, i)) }) }), /*#__PURE__*/jsxRuntime.jsx("tbody", { children: token.rows.map((cells, i) => /*#__PURE__*/jsxRuntime.jsx("tr", { children: cells.map((c, i2) => /*#__PURE__*/jsxRuntime.jsx("td", { align: token.align[i] ?? 'left', children: convertAstToElements(c.tokens) }, i2)) }, i)) })] }, counter)); break; case 'text': elements.push(children.length === 0 ? token.text : /*#__PURE__*/jsxRuntime.jsx(react.Fragment, { children: convertAstToElements(children) }, counter)); break; case 'admonition': elements.push( /*#__PURE__*/jsxRuntime.jsx(MDX__default.default.admonition, { title: token.title, type: token.keyword, children: convertAstToElements(children) ?? token.text }, counter)); break; default: { const Comp = TOKEN_TO_TAG[token.type] || token.type; const innerText = 'text' in token ? token.text : ''; elements.push( /*#__PURE__*/jsxRuntime.jsx(Comp, { children: convertAstToElements(children) ?? innerText }, counter)); break; } } counter += 1; }); if (elements.length === 0) { return undefined; } return elements; } // Too bad we cant use `@mdx-js` here... function Markdown({ content }) { const reflections = useReflectionMap.useReflectionMap(); const version = client.useDocsVersion(); const docsData = client.useDocsData(version.pluginId); const _react$useState = react.useState(() => marked.marked.lexer(markdown.replaceLinkTokens(content, reflections, version, docsData.path))), _react$useState2 = _slicedToArray(_react$useState, 1), ast = _react$useState2[0]; if (!content) { return null; } return /*#__PURE__*/jsxRuntime.jsx("div", { className: "tsd-markdown markdown", children: convertAstToElements(ast) }); } exports.Markdown = Markdown; //# sourceMappingURL=Markdown.js.map