2021-08-14 23:11:18 +08:00
|
|
|
import { visit } from 'unist-util-visit'
|
2021-08-22 17:13:14 +08:00
|
|
|
import { slug } from 'github-slugger'
|
2021-11-26 21:34:26 +08:00
|
|
|
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) => {
|
2021-08-30 23:53:41 +08:00
|
|
|
const textContent = toString(node)
|
2021-08-06 22:13:30 +08:00
|
|
|
options.exportRef.push({
|
2021-08-30 23:53:41 +08:00
|
|
|
value: textContent,
|
|
|
|
url: '#' + slug(textContent),
|
2021-08-06 22:13:30 +08:00
|
|
|
depth: node.depth,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|