Skip to content

Commit 19ea231

Browse files
committed
refactor: update rehypeCollectHeadings to accept parameters and improve heading collection
1 parent a9127be commit 19ea231

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

src/utils/markdown/plugins/collectHeadings.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export type MarkdownHeading = {
1010
level: number
1111
}
1212

13-
export const rehypeCollectHeadings = (headings: MarkdownHeading[]) => {
14-
return (tree: Root, file) => {
13+
export async function rehypeCollectHeadings (tree, file, headings: MarkdownHeading[]) {
14+
return (tree) => {
1515
visit(tree, 'element', (node) => {
1616
if (!isHeading(node)) {
1717
return
@@ -29,6 +29,8 @@ export const rehypeCollectHeadings = (headings: MarkdownHeading[]) => {
2929
})
3030
})
3131

32-
file.data.headings = headings
32+
if (file) {
33+
file.data.headings = headings
34+
}
3335
}
3436
}

src/utils/markdown/processor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function renderMarkdown(content): MarkdownRenderResult {
6969
className: ['anchor-heading'],
7070
},
7171
})
72-
.use(rehypeCollectHeadings(headings))
72+
.use((node, file) => rehypeCollectHeadings(node, file, headings))
7373

7474
const file = processor.use(rehypeStringify).processSync(content)
7575

0 commit comments

Comments
 (0)