Skip to content

Commit b10dd2f

Browse files
committed
feat: integrate heading collection into rehype processor
1 parent f0953b8 commit b10dd2f

File tree

1 file changed

+2
-24
lines changed

1 file changed

+2
-24
lines changed

src/utils/markdown/processor.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import rehypeAutolinkHeadings from 'rehype-autolink-headings'
99
import rehypeStringify from 'rehype-stringify'
1010
import { visit } from 'unist-util-visit'
1111
import { toString } from 'hast-util-to-string'
12-
import { rehypeParseCommentComponents, rehypeTransformCommentComponents } from '~/utils/markdown/plugins'
12+
import { rehypeCollectHeadings, rehypeParseCommentComponents, rehypeTransformCommentComponents } from '~/utils/markdown/plugins'
1313

1414
export type MarkdownHeading = {
1515
id: string
@@ -70,29 +70,7 @@ export function renderMarkdown(content): MarkdownRenderResult {
7070
className: ['anchor-heading'],
7171
},
7272
})
73-
.use(() => (tree, file) => {
74-
visit(tree, 'element', (node) => {
75-
if (!('tagName' in node)) return
76-
if (!/^h[1-6]$/.test(String(node.tagName))) {
77-
return
78-
}
79-
80-
const tagName = String(node.tagName)
81-
const id =
82-
typeof node.properties?.id === 'string' ? node.properties.id : ''
83-
if (!id) {
84-
return
85-
}
86-
87-
headings.push({
88-
id,
89-
level: Number(tagName.substring(1)),
90-
text: toString(node).trim(),
91-
})
92-
})
93-
94-
file.data.headings = headings
95-
})
73+
.use((tree, file) => rehypeCollectHeadings(tree, file, headings))
9674

9775
const file = processor.use(rehypeStringify).processSync(content)
9876

0 commit comments

Comments
 (0)