@@ -9,7 +9,7 @@ import rehypeAutolinkHeadings from 'rehype-autolink-headings'
99import rehypeStringify from 'rehype-stringify'
1010import { visit } from 'unist-util-visit'
1111import { toString } from 'hast-util-to-string'
12- import { rehypeParseCommentComponents , rehypeTransformCommentComponents } from '~/utils/markdown/plugins'
12+ import { rehypeCollectHeadings , rehypeParseCommentComponents , rehypeTransformCommentComponents } from '~/utils/markdown/plugins'
1313
1414export 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