UNPKG

svem

Version:

Svelte in Markdown preprocessor

31 lines (30 loc) 798 B
import { visit } from "unist-util-visit"; const remarkHeadingLink = (options) => { return (tree, file) => { const headings = []; visit(tree, "heading", (node) => { if (options.depths.includes(node.depth)) { const text = node.children.map((n) => { return n.value; }).join(""); const slug = slugify(text, options); headings.push({ text, slug, level: node.depth }); node.data = node.data || {}; node.data.hProperties = { id: slug }; } }); file.data.headings = headings; }; }; function slugify(text, options) { const words = text.match(/\w+/g); let slug = (words ?? []).join("-"); if (options?.lower ?? true) { slug = slug.toLowerCase(); } return slug; } export { remarkHeadingLink, slugify };