remark-smart-toc
Version:
Implementing intelligent Markdown directory syntax, better than existing solutions
26 lines • 885 B
JavaScript
// src/index.ts
import { toc } from "mdast-util-toc";
var src_default = (depth) => {
return () => (tree, file) => {
const tocNode = tree.children.find(
(node) => node.type === "paragraph" && node.children.length === 1 && node.children[0].type === "text" && /^(?:\[?\[[Tt][Oo][Cc]\]\]?|目录|[Cc]ontents?)$/.test(node.children[0].value)
);
if (tocNode) {
const hasHeadings = tree.children.some((node) => /^heading/.test(node.type));
const generatedToc = toc(tree, { maxDepth: depth }).map;
const ulNode = {
type: "list",
ordered: false,
start: null,
loose: false,
children: hasHeadings ? generatedToc.children : []
};
const index = tree.children.indexOf(tocNode);
tree.children.splice(index, 1, ulNode);
}
};
};
export {
src_default as default
};
//# sourceMappingURL=index.mjs.map