mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-02 18:29:29 +08:00
28 lines
676 B
Text
28 lines
676 B
Text
/**
|
|
* @typedef {import('hast').Root} Root
|
|
*/
|
|
|
|
import Slugger from 'github-slugger'
|
|
import {hasProperty} from 'hast-util-has-property'
|
|
import {headingRank} from 'hast-util-heading-rank'
|
|
import {toString} from 'hast-util-to-string'
|
|
import {visit} from 'unist-util-visit'
|
|
|
|
const slugs = new Slugger()
|
|
|
|
/**
|
|
* Plugin to add `id`s to headings.
|
|
*
|
|
* @type {import('unified').Plugin<Array<void>, Root>}
|
|
*/
|
|
export default function rehypeSlug() {
|
|
return (tree) => {
|
|
slugs.reset()
|
|
|
|
visit(tree, 'element', (node) => {
|
|
if (headingRank(node) && node.properties && !hasProperty(node, 'id')) {
|
|
node.properties.id = slugs.slug(toString(node))
|
|
}
|
|
})
|
|
}
|
|
}
|