refactor: use rehype-slug and rehype-autolink-headings

This commit is contained in:
Timothy Lin
2021-08-22 17:13:14 +08:00
parent 51feadc0db
commit 0e34d80287
4 changed files with 43 additions and 27 deletions

View File

@@ -1,11 +1,12 @@
import { visit } from 'unist-util-visit'
import { slug } from 'github-slugger'
export default function remarkTocHeadings(options) {
return (tree) =>
visit(tree, 'heading', (node, index, parent) => {
options.exportRef.push({
value: node.children[0].value || node.children[1].value,
url: node.children[0].url || node.children[1].url,
value: node.children[0].value,
url: '#' + slug(node.children[0].value),
depth: node.depth,
})
})