1
+ /**
2
+ * @typedef {import('mdast').Root } Root
3
+ */
4
+
5
+ import assert from 'assert'
1
6
import test from 'tape'
2
7
import remark from 'remark'
3
8
import { headingStyle } from './index.js'
@@ -18,95 +23,101 @@ test('headingStyle', function (t) {
18
23
'should NOT fail on undetectable nodes'
19
24
)
20
25
21
- t . equal (
22
- headingStyle ( remark . parse ( '# ATX' ) . children [ 0 ] ) ,
23
- 'atx' ,
24
- 'should detect atx'
25
- )
26
+ t . equal ( headingStyle ( parseFirstNode ( '# ATX' ) ) , 'atx' , 'should detect atx' )
26
27
27
28
t . equal (
28
- headingStyle ( remark . parse ( '# ATX #' ) . children [ 0 ] ) ,
29
+ headingStyle ( parseFirstNode ( '# ATX #' ) ) ,
29
30
'atx-closed' ,
30
31
'should detect closed atx'
31
32
)
32
33
33
34
t . equal (
34
- headingStyle ( remark . parse ( 'ATX\n===' ) . children [ 0 ] ) ,
35
+ headingStyle ( parseFirstNode ( 'ATX\n===' ) ) ,
35
36
'setext' ,
36
37
'should detect closed setext'
37
38
)
38
39
39
40
t . equal (
40
- headingStyle ( remark . parse ( '### ATX' ) . children [ 0 ] ) ,
41
+ headingStyle ( parseFirstNode ( '### ATX' ) ) ,
41
42
null ,
42
43
'should work on ambiguous nodes'
43
44
)
44
45
45
46
t . equal (
46
- headingStyle ( remark . parse ( '### ATX' ) . children [ 0 ] , 'atx' ) ,
47
+ headingStyle ( parseFirstNode ( '### ATX' ) , 'atx' ) ,
47
48
'atx' ,
48
49
'should work on ambiguous nodes (preference to atx)'
49
50
)
50
51
51
52
t . equal (
52
- headingStyle ( remark . parse ( '### ATX' ) . children [ 0 ] , 'setext' ) ,
53
+ headingStyle ( parseFirstNode ( '### ATX' ) , 'setext' ) ,
53
54
'setext' ,
54
55
'should work on ambiguous nodes (preference to setext)'
55
56
)
56
57
57
58
t . equal (
58
- headingStyle ( remark . parse ( '###### ######' ) . children [ 0 ] ) ,
59
+ headingStyle ( parseFirstNode ( '###### ######' ) ) ,
59
60
'atx-closed' ,
60
61
'should work on empty nodes (#1)'
61
62
)
62
63
63
64
t . equal (
64
- headingStyle ( remark . parse ( '### ###' ) . children [ 0 ] ) ,
65
+ headingStyle ( parseFirstNode ( '### ###' ) ) ,
65
66
'atx-closed' ,
66
67
'should work on empty nodes (#2)'
67
68
)
68
69
69
70
t . equal (
70
- headingStyle ( remark . parse ( '# #' ) . children [ 0 ] ) ,
71
+ headingStyle ( parseFirstNode ( '# #' ) ) ,
71
72
'atx-closed' ,
72
73
'should work on empty nodes (#3)'
73
74
)
74
75
75
76
t . equal (
76
- headingStyle ( remark . parse ( '###### ' ) . children [ 0 ] , 'atx' ) ,
77
+ headingStyle ( parseFirstNode ( '###### ' ) , 'atx' ) ,
77
78
'atx' ,
78
79
'should work on empty nodes (#4)'
79
80
)
80
81
81
82
t . equal (
82
- headingStyle ( remark . parse ( '### ' ) . children [ 0 ] , 'atx' ) ,
83
+ headingStyle ( parseFirstNode ( '### ' ) , 'atx' ) ,
83
84
'atx' ,
84
85
'should work on empty nodes (#5)'
85
86
)
86
87
87
88
t . equal (
88
- headingStyle ( remark . parse ( '## ' ) . children [ 0 ] ) ,
89
+ headingStyle ( parseFirstNode ( '## ' ) ) ,
89
90
'atx' ,
90
91
'should work on empty nodes (#6)'
91
92
)
92
93
93
94
t . equal (
94
- headingStyle ( remark . parse ( '###### ' ) . children [ 0 ] , 'setext' ) ,
95
+ headingStyle ( parseFirstNode ( '###### ' ) , 'setext' ) ,
95
96
'setext' ,
96
97
'should work on empty nodes (#7)'
97
98
)
98
99
99
100
t . equal (
100
- headingStyle ( remark . parse ( '### ' ) . children [ 0 ] , 'setext' ) ,
101
+ headingStyle ( parseFirstNode ( '### ' ) , 'setext' ) ,
101
102
'setext' ,
102
103
'should work on empty nodes (#8)'
103
104
)
104
105
105
106
t . equal (
106
- headingStyle ( remark . parse ( '## ' ) . children [ 0 ] , 'setext' ) ,
107
+ headingStyle ( parseFirstNode ( '## ' ) , 'setext' ) ,
107
108
'atx' ,
108
109
'should work on empty nodes (#9)'
109
110
)
110
111
111
112
t . end ( )
112
113
} )
114
+
115
+ /**
116
+ * @param {string } doc
117
+ */
118
+ function parseFirstNode ( doc ) {
119
+ const tree = /** @type {Root } */ ( remark . parse ( doc ) )
120
+ const head = tree . children [ 0 ]
121
+ assert ( head . type === 'heading' )
122
+ return head
123
+ }
0 commit comments