@@ -12,44 +12,52 @@ const remarkToc = (options = {}) => {
12
12
return ( tree ) => {
13
13
/** @type {{type: string, depth: number, children: []}[] }*/
14
14
const headings = [ ]
15
- visit ( tree , "heading" , ( /**@type {import("type-mdast").Node } */ node , _index ) => {
16
- /**@type {string } */
17
- let title = node . children [ 0 ] . value
18
- // Using others type when heading is not a text node
19
- if ( node . children [ 0 ] . type !== "text" && node . children [ 0 ] . children [ 0 ] ) {
20
- title = node . children [ 0 ] . children [ 0 ] . value
21
- }
22
- /**@type {number } */
23
- const depth = node . depth
24
- const children = [ { type : "text" , value : title } ]
25
- headings . push ( { type : "heading" , depth, children } )
26
- } )
15
+ visit (
16
+ tree ,
17
+ 'heading' ,
18
+ ( /**@type {import("type-mdast").Node } */ node , _index ) => {
19
+ /**@type {string } */
20
+ let title = node . children [ 0 ] . value
21
+ // Using others type when heading is not a text node
22
+ if ( node . children [ 0 ] . type !== 'text' && node . children [ 0 ] . children [ 0 ] ) {
23
+ title = node . children [ 0 ] . children [ 0 ] . value
24
+ }
25
+ /**@type {number } */
26
+ const depth = node . depth
27
+ const children = [ { type : 'text' , value : title } ]
28
+ headings . push ( { type : 'heading' , depth, children } )
29
+ } ,
30
+ )
27
31
28
- visit ( tree , "paragraph" , ( /**@type {import("type-mdast").Node } */ node , _index ) => {
29
- if ( node . children ?. [ 0 ] ?. value !== flag ) return
30
- const table = toc ( { type : "root" , children : headings } )
31
- // update toc node
32
- table . map . data = {
33
- hProperties : {
34
- class : "remark-toc" ,
35
- } ,
36
- }
37
- node . data = {
38
- hName : "details" ,
39
- hProperties : {
40
- class : "remark-toc-details" ,
41
- } ,
42
- }
43
- node . children = [
44
- {
45
- type : "html" ,
46
- value : "<summary class='remark-toc-summary'>目录</summary>" ,
47
- } ,
48
- table . map
49
- ]
50
- } )
32
+ visit (
33
+ tree ,
34
+ 'paragraph' ,
35
+ ( /**@type {import("type-mdast").Node } */ node , _index ) => {
36
+ if ( node . children ?. [ 0 ] ?. value !== flag ) return
37
+ const table = toc ( { type : 'root' , children : headings } )
38
+ // update toc node
39
+ table . map . data = {
40
+ hProperties : {
41
+ class : 'remark-toc' ,
42
+ } ,
43
+ }
44
+ node . data = {
45
+ hName : 'details' ,
46
+ hProperties : {
47
+ class : 'remark-toc-details' ,
48
+ } ,
49
+ }
50
+ node . children = [
51
+ {
52
+ type : 'html' ,
53
+ value : "<summary class='remark-toc-summary'>目录</summary>" ,
54
+ } ,
55
+ table . map ,
56
+ ]
57
+ } ,
58
+ )
51
59
return tree
52
60
}
53
61
}
54
62
55
- export default remarkToc
63
+ export default remarkToc
0 commit comments