UNPKG

docusaurus-plugin-typedoc-api

Version:

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

68 lines (65 loc) 3.84 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; } function splitLinkText(text) { let splitIndex = text.indexOf('|'); if (splitIndex === -1) { splitIndex = text.search(/\s/); } if (splitIndex !== -1) { return { caption: text.slice(splitIndex + 1).replace(/\n+/, ' ').trim(), target: text.slice(0, Math.max(0, splitIndex)).trim() }; } return { caption: text, target: text }; } function findReflectionWithMatchingTarget(reflections, symbol, member) { return Object.values(reflections).find(ref => { if (ref.name !== symbol) { return false; } return member ? ref.children?.some(child => child.name === member) : true; }); } function replaceApiLinks(reflections) { return (match, tagName, content) => { const _splitLinkText = splitLinkText(content), caption = _splitLinkText.caption, target = _splitLinkText.target; const _target$split = target.split('.'), _target$split2 = _slicedToArray(_target$split, 2), symbol = _target$split2[0], member = _target$split2[1]; const reflection = findReflectionWithMatchingTarget(reflections, symbol, member); const label = tagName === 'linkcode' ? `\`${caption}\`` : caption; if (!reflection?.permalink) { return label; } return `[${label}](${reflection.permalink}${member ? `#${member}` : ''})`; }; } function replaceDocLinks(currentVersion, baseUrl) { return (match, content) => { const _splitLinkText2 = splitLinkText(content), caption = _splitLinkText2.caption, target = _splitLinkText2.target; const version = currentVersion.version === 'current' ? 'next' : currentVersion.version; const url = currentVersion.isLast ? `${baseUrl === '/' ? '' : baseUrl}/${target}` : `${baseUrl === '/' ? '' : baseUrl}/${version}/${target}`; return `[${caption}](${url})`; }; } // TypeDoc JSON output does not replace links, so we need to do this manually. // @see https://github.com/TypeStrong/typedoc/blob/master/src/lib/output/plugins/MarkedLinksPlugin.ts function replaceLinkTokens(markdown, reflections, currentVersion, docsBaseUrl) { return markdown.replace(/{@(link|linkcode|linkplain|apilink)\s+([^}]+?)}/gi, replaceApiLinks(reflections)).replace(/{@doclink\s+([^}]+?)}/gi, replaceDocLinks(currentVersion, docsBaseUrl)); } exports.replaceLinkTokens = replaceLinkTokens; //# sourceMappingURL=markdown.js.map