jonbio/lib/remark-toc-headings.js

16 lines
437 B
JavaScript
Raw Normal View History

2021-08-14 23:11:18 +08:00
import { visit } from 'unist-util-visit'
import { slug } from 'github-slugger'
import { toString } from 'mdast-util-to-string'
2021-08-06 22:13:30 +08:00
2021-08-14 23:11:18 +08:00
export default function remarkTocHeadings(options) {
2021-08-06 22:13:30 +08:00
return (tree) =>
visit(tree, 'heading', (node, index, parent) => {
const textContent = toString(node)
2021-08-06 22:13:30 +08:00
options.exportRef.push({
value: textContent,
url: '#' + slug(textContent),
2021-08-06 22:13:30 +08:00
depth: node.depth,
})
})
}