UNPKG

single-page-markdown-website

Version:

Create a nice single-page documentation website from one or more Markdown files

68 lines 2.07 kB
import { toc } from 'mdast-util-toc'; import remarkParse from 'remark-parse'; import remarkStringify from 'remark-stringify'; import remarkStripBadges from 'remark-strip-badges'; import { unified } from 'unified'; export async function createMarkdownTocAsync(content, options) { const file = await unified() .use(remarkParse) .use(remarkStripBadges) .use(remarkExtractToc, { sections: options.sections }) .use(remarkStringify) .process(content); const result = file.toString(); return result === '' ? null : result; } const remarkExtractToc = function (options) { return function (node) { const { map } = toc(node, { tight: true }); if (map === null) { node.children = []; delete node.position; return; } if (options.sections === false) { node.children = [map]; delete node.position; return; } if (map.children.length > 1) { const { map } = toc(node, { maxDepth: 1, tight: true }); if (map === null) { node.children = []; delete node.position; return; } node.children = [map]; delete node.position; return; } const result = []; for (const node of map.children[0].children) { if (node.type === 'list') { for (const child of node.children) { result.push({ children: [child.children[0]], spread: false, type: 'listItem' }); } } } node.children = [ { children: result, ordered: false, spread: false, type: 'list' } ]; delete node.position; }; }; //# sourceMappingURL=create-markdown-toc-async.js.map