File tree Expand file tree Collapse file tree 3 files changed +22
-28
lines changed Expand file tree Collapse file tree 3 files changed +22
-28
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
- * @typedef {import('unist ').Node } Node
2
+ * @typedef {import('mdast ').Root|import('mdast').Content } Node
3
3
* @typedef {import('mdast').Heading } Heading
4
4
*/
5
5
@@ -23,24 +23,19 @@ export function normalizeHeadings(tree) {
23
23
/** @type {Heading|undefined } */
24
24
let title
25
25
26
- visit (
27
- tree ,
28
- 'heading' ,
29
- /** @type {import('unist-util-visit').Visitor<Heading> } */
30
- ( node ) => {
31
- if ( ! first ) {
32
- first = node
33
- }
26
+ visit ( tree , 'heading' , ( node ) => {
27
+ if ( ! first ) {
28
+ first = node
29
+ }
34
30
35
- if ( node . depth === 1 ) {
36
- if ( title ) {
37
- multiple = true
38
- } else {
39
- title = node
40
- }
31
+ if ( node . depth === 1 ) {
32
+ if ( title ) {
33
+ multiple = true
34
+ } else {
35
+ title = node
41
36
}
42
37
}
43
- )
38
+ } )
44
39
45
40
// If there are no titles, but there is a first heading.
46
41
if ( ! title && first ) {
@@ -49,16 +44,11 @@ export function normalizeHeadings(tree) {
49
44
50
45
// If there are multiple titles.
51
46
if ( multiple ) {
52
- visit (
53
- tree ,
54
- 'heading' ,
55
- /** @type {import('unist-util-visit').Visitor<Heading> } */
56
- ( node ) => {
57
- if ( node !== title && node . depth < max ) {
58
- node . depth ++
59
- }
47
+ visit ( tree , 'heading' , ( node ) => {
48
+ if ( node !== title && node . depth < max ) {
49
+ node . depth ++
60
50
}
61
- )
51
+ } )
62
52
}
63
53
64
54
return tree
Original file line number Diff line number Diff line change 38
38
],
39
39
"dependencies" : {
40
40
"@types/mdast" : " ^3.0.0" ,
41
- "@types/unist" : " ^2.0.0" ,
42
- "unist-util-visit" : " ^3.0.0"
41
+ "unist-util-visit" : " ^4.0.0"
43
42
},
44
43
"devDependencies" : {
45
44
"@types/tape" : " ^4.0.0" ,
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @typedef {import('mdast').Root } Root
3
+ */
4
+
1
5
import fs from 'node:fs'
2
6
import path from 'node:path'
3
7
import test from 'tape'
@@ -27,9 +31,10 @@ test('Level 7', (t) => {
27
31
function check ( t , test , message ) {
28
32
const input = fs . readFileSync ( path . join ( 'test' , 'fixture' , test + '.in' ) )
29
33
const output = fs . readFileSync ( path . join ( 'test' , 'fixture' , test + '.out' ) )
34
+ const root = /** @type {Root } */ ( remark ( ) . parse ( input ) )
30
35
31
36
t . deepEqual (
32
- removePosition ( normalizeHeadings ( remark ( ) . parse ( input ) ) , true ) ,
37
+ removePosition ( normalizeHeadings ( root ) , true ) ,
33
38
removePosition ( remark ( ) . parse ( output ) , true ) ,
34
39
message
35
40
)
You can’t perform that action at this time.
0 commit comments