1
1
/**
2
- * @typedef {import('unist').Literal<string> } Literal
2
+ * @typedef {import('mdast').Text } Text
3
+ * @typedef {import('mdast').Emphasis } Emphasis
4
+ * @typedef {import('mdast').Root } Root
3
5
* @typedef {import('unist').Node } Node
4
6
*/
5
7
@@ -16,24 +18,26 @@ test('remove', async function (t) {
16
18
} )
17
19
18
20
await t . test ( 'should compare nodes by partial properties' , function ( ) {
19
- const leaf1 = u ( 'leaf ' , '1' )
20
- const leaf2 = u ( 'leaf ' , '2' )
21
+ const leaf1 = u ( 'text ' , '1' )
22
+ const leaf2 = u ( 'text ' , '2' )
21
23
const children = [ leaf1 , leaf2 ]
22
- const tree = u ( 'node' , children )
24
+ /** @type {Emphasis } */
25
+ const tree = u ( 'emphasis' , children )
23
26
24
27
const next = remove ( tree , { value : '2' } )
25
28
26
- assert . deepEqual ( tree , u ( 'node ' , [ leaf1 ] ) )
29
+ assert . deepEqual ( tree , u ( 'emphasis ' , [ leaf1 ] ) )
27
30
assert . equal ( next , tree )
28
31
assert . equal ( next . children , children )
29
32
assert . equal ( next . children [ 0 ] , leaf1 )
30
33
} )
31
34
32
35
await t . test ( 'should remove parent nodes' , function ( ) {
33
- const leaf1 = u ( 'leaf ' , '1' )
34
- const leaf2 = u ( 'leaf ' , '2' )
35
- const parent = u ( 'parent ' , [ leaf1 ] )
36
+ const leaf1 = u ( 'text ' , '1' )
37
+ const leaf2 = u ( 'text ' , '2' )
38
+ const parent = u ( 'emphasis ' , [ leaf1 ] )
36
39
const children = [ parent , leaf2 ]
40
+ /** @type {Root } */
37
41
const tree = u ( 'root' , children )
38
42
39
43
const next = remove ( tree , test )
@@ -55,103 +59,109 @@ test('remove', async function (t) {
55
59
await t . test (
56
60
'should return `undefined` if root node is removed' ,
57
61
function ( ) {
58
- const tree = u ( 'root' , [ u ( 'node' , [ u ( 'leaf' , '1' ) ] ) , u ( 'leaf' , '2' ) ] )
62
+ /** @type {Root } */
63
+ const tree = u ( 'root' , [ u ( 'emphasis' , [ u ( 'text' , '1' ) ] ) , u ( 'text' , '2' ) ] )
59
64
const next = remove ( tree , 'root' )
60
65
61
66
assert . equal ( next , undefined )
62
67
}
63
68
)
64
69
65
70
await t . test ( 'should cascade (remove) root nodes' , function ( ) {
66
- const tree = u ( 'root' , [ u ( 'node' , [ u ( 'leaf' , '1' ) ] ) , u ( 'leaf' , '2' ) ] )
67
- const next = remove ( tree , 'leaf' )
71
+ /** @type {Root } */
72
+ const tree = u ( 'root' , [ u ( 'emphasis' , [ u ( 'text' , '1' ) ] ) , u ( 'text' , '2' ) ] )
73
+ const next = remove ( tree , 'text' )
68
74
69
75
assert . equal ( next , undefined )
70
76
} )
71
77
72
78
await t . test (
73
79
'should not cascade (remove) nodes that were empty initially' ,
74
80
function ( ) {
75
- const tree = u ( 'node' , [ u ( 'node' , [ ] ) , u ( 'node' , [ u ( 'leaf' ) ] ) ] )
81
+ /** @type {Root } */
82
+ const tree = u ( 'root' , [
83
+ u ( 'emphasis' , [ ] ) ,
84
+ u ( 'emphasis' , [ u ( 'text' , 'x' ) ] )
85
+ ] )
76
86
77
- remove ( tree , 'leaf ' )
87
+ remove ( tree , 'text ' )
78
88
79
- assert . deepEqual ( tree , u ( 'node ' , [ u ( 'node ' , [ ] ) ] ) )
89
+ assert . deepEqual ( tree , u ( 'root ' , [ u ( 'emphasis ' , [ ] ) ] ) )
80
90
}
81
91
)
82
92
83
93
await t . test ( 'should support type tests' , function ( ) {
84
- const tree = u ( 'node' , [ u ( 'node' , [ u ( 'leaf' , '1' ) ] ) , u ( 'leaf' , '2' ) ] )
94
+ /** @type {Root } */
95
+ const tree = u ( 'root' , [ u ( 'emphasis' , [ u ( 'text' , '1' ) ] ) , u ( 'text' , '2' ) ] )
85
96
86
- remove ( tree , { cascade : false } , 'leaf ' )
97
+ remove ( tree , { cascade : false } , 'text ' )
87
98
88
- assert . deepEqual ( tree , u ( 'node ' , [ u ( 'node ' , [ ] ) ] ) )
99
+ assert . deepEqual ( tree , u ( 'root ' , [ u ( 'emphasis ' , [ ] ) ] ) )
89
100
} )
90
101
91
102
await t . test ( 'should support function tests' , function ( ) {
92
- const tree = u ( 'node' , [ u ( 'node' , [ u ( 'leaf' , '1' ) ] ) , u ( 'leaf' , '2' ) ] )
103
+ /** @type {Emphasis } */
104
+ const tree = u ( 'emphasis' , [
105
+ u ( 'emphasis' , [ u ( 'text' , '1' ) ] ) ,
106
+ u ( 'text' , '2' )
107
+ ] )
93
108
94
109
remove ( tree , { cascade : false } , test )
95
110
96
- assert . deepEqual ( tree , u ( 'node ' , [ u ( 'node ' , [ ] ) , u ( 'leaf ' , '2' ) ] ) )
111
+ assert . deepEqual ( tree , u ( 'emphasis ' , [ u ( 'emphasis ' , [ ] ) , u ( 'text ' , '2' ) ] ) )
97
112
98
113
/**
99
114
* @param {Node } node
100
115
* @returns {boolean }
101
116
*/
102
117
function test ( node ) {
103
- return literal ( node ) && node . value === '1'
118
+ return node . type === 'text' && 'value' in node && node . value === '1'
104
119
}
105
120
} )
106
121
107
122
await t . test ( 'should support `cascade = true`' , function ( ) {
108
- const tree = u ( 'root' , [ u ( 'node' , [ u ( 'leaf' , '1' ) ] ) , u ( 'leaf' , '2' ) ] )
109
- const next = remove ( tree , { cascade : true } , 'leaf' )
123
+ /** @type {Root } */
124
+ const tree = u ( 'root' , [ u ( 'emphasis' , [ u ( 'text' , '1' ) ] ) , u ( 'text' , '2' ) ] )
125
+ const next = remove ( tree , { cascade : true } , 'text' )
110
126
111
127
assert . equal ( next , undefined )
112
128
} )
113
129
114
130
await t . test ( 'should support `cascade = false`' , function ( ) {
115
- const leaf1 = u ( 'leaf ' , '1' )
116
- const leaf2 = u ( 'leaf ' , '2' )
131
+ const leaf1 = u ( 'text ' , '1' )
132
+ const leaf2 = u ( 'text ' , '2' )
117
133
const nodeChildren = [ leaf1 ]
118
- const node = u ( 'node ' , nodeChildren )
134
+ const node = u ( 'emphasis ' , nodeChildren )
119
135
const siblings = [ node , leaf2 ]
136
+ /** @type {Root } */
120
137
const tree = u ( 'root' , siblings )
121
138
122
- const next = remove ( tree , { cascade : false } , 'leaf ' )
139
+ const next = remove ( tree , { cascade : false } , 'text ' )
123
140
124
- assert . deepEqual ( tree , u ( 'root' , [ u ( 'node ' , [ ] ) ] ) )
141
+ assert . deepEqual ( tree , u ( 'root' , [ u ( 'emphasis ' , [ ] ) ] ) )
125
142
assert . equal ( next , tree )
126
143
assert . equal ( next . children , siblings )
127
144
assert . equal ( next . children [ 0 ] , node )
128
145
assert . equal ( next . children [ 0 ] . children , nodeChildren )
129
146
} )
130
147
131
148
await t . test ( 'should support the example from readme' , function ( ) {
149
+ /** @type {Root } */
132
150
const tree = u ( 'root' , [
133
- u ( 'leaf ' , '1' ) ,
134
- u ( 'node ' , [
135
- u ( 'leaf ' , '2' ) ,
136
- u ( 'node ' , [ u ( 'leaf ' , '3' ) , u ( 'other ' , '4' ) ] ) ,
137
- u ( 'node ' , [ u ( 'leaf ' , '5' ) ] )
151
+ u ( 'text ' , '1' ) ,
152
+ u ( 'emphasis ' , [
153
+ u ( 'text ' , '2' ) ,
154
+ u ( 'emphasis ' , [ u ( 'text ' , '3' ) , u ( 'inlineCode ' , '4' ) ] ) ,
155
+ u ( 'emphasis ' , [ u ( 'text ' , '5' ) ] )
138
156
] ) ,
139
- u ( 'leaf ' , '6' )
157
+ u ( 'text ' , '6' )
140
158
] )
141
159
142
- remove ( tree , 'leaf ' )
160
+ remove ( tree , 'text ' )
143
161
144
162
assert . deepEqual (
145
163
tree ,
146
- u ( 'root' , [ u ( 'node ' , [ u ( 'node ' , [ u ( 'other ' , '4' ) ] ) ] ) ] )
164
+ u ( 'root' , [ u ( 'emphasis ' , [ u ( 'emphasis ' , [ u ( 'inlineCode ' , '4' ) ] ) ] ) ] )
147
165
)
148
166
} )
149
167
} )
150
-
151
- /**
152
- * @param {Node } node
153
- * @returns {node is Literal }
154
- */
155
- function literal ( node ) {
156
- return 'value' in node
157
- }
0 commit comments