1
1
/**
2
2
* @typedef {import('unist').Node } Node
3
3
* @typedef {import('unist').Literal<string> } Literal
4
+ * @typedef {import('mdast').Root } Root
5
+ * @typedef {import('mdast').InlineCode } InlineCode
4
6
* @typedef {import('vfile').VFile } VFile
5
7
*/
6
8
@@ -11,16 +13,19 @@ import remark from 'remark'
11
13
import gfm from 'remark-gfm'
12
14
import frontmatter from 'remark-frontmatter'
13
15
import { toVFile as vfile } from 'to-vfile'
16
+ // @ts -expect-error: to do type.
14
17
import { ParseLatin } from 'parse-latin'
18
+ // @ts -expect-error: to do type.
15
19
import { ParseDutch } from 'parse-dutch'
20
+ // @ts -expect-error: to do type.
16
21
import { ParseEnglish } from 'parse-english'
17
22
import { isHidden } from 'is-hidden'
18
23
import { toNlcst } from '../index.js'
19
24
20
25
test ( 'mdast-util-to-nlcst' , ( t ) => {
21
26
t . throws (
22
27
( ) => {
23
- // @ts -ignore runtime.
28
+ // @ts -expect-error runtime.
24
29
toNlcst ( )
25
30
} ,
26
31
/ m d a s t - u t i l - t o - n l c s t e x p e c t e d n o d e / ,
@@ -29,7 +34,7 @@ test('mdast-util-to-nlcst', (t) => {
29
34
30
35
t . throws (
31
36
( ) => {
32
- // @ts -ignore runtime.
37
+ // @ts -expect-error runtime.
33
38
toNlcst ( { } )
34
39
} ,
35
40
/ m d a s t - u t i l - t o - n l c s t e x p e c t e d n o d e / ,
@@ -38,7 +43,7 @@ test('mdast-util-to-nlcst', (t) => {
38
43
39
44
t . throws (
40
45
( ) => {
41
- // @ts -ignore runtime.
46
+ // @ts -expect-error runtime.
42
47
toNlcst ( { type : 'foo' } )
43
48
} ,
44
49
/ m d a s t - u t i l - t o - n l c s t e x p e c t e d f i l e / ,
@@ -47,7 +52,7 @@ test('mdast-util-to-nlcst', (t) => {
47
52
48
53
t . throws (
49
54
( ) => {
50
- // @ts -ignore runtime.
55
+ // @ts -expect-error runtime.
51
56
toNlcst ( { type : 'foo' } )
52
57
} ,
53
58
/ m d a s t - u t i l - t o - n l c s t e x p e c t e d f i l e / ,
@@ -56,7 +61,7 @@ test('mdast-util-to-nlcst', (t) => {
56
61
57
62
t . throws (
58
63
( ) => {
59
- // @ts -ignore runtime.
64
+ // @ts -expect-error runtime.
60
65
toNlcst ( { type : 'text' , value : 'foo' } , { foo : 'bar' } )
61
66
} ,
62
67
/ m d a s t - u t i l - t o - n l c s t e x p e c t e d f i l e / ,
@@ -65,8 +70,11 @@ test('mdast-util-to-nlcst', (t) => {
65
70
66
71
t . throws (
67
72
( ) => {
68
- // @ts -ignore runtime.
69
- toNlcst ( { type : 'text' , value : 'foo' } , vfile ( { contents : 'foo' } ) )
73
+ // @ts -expect-error runtime.
74
+ toNlcst (
75
+ /** @type {Literal } */ ( { type : 'text' , value : 'foo' } ) ,
76
+ vfile ( { contents : 'foo' } )
77
+ )
70
78
} ,
71
79
/ m d a s t - u t i l - t o - n l c s t e x p e c t e d p a r s e r / ,
72
80
'should fail without parser'
@@ -114,7 +122,7 @@ test('mdast-util-to-nlcst', (t) => {
114
122
{
115
123
type : 'text' ,
116
124
value : 'foo' ,
117
- // @ts -ignore runtime.
125
+ // @ts -expect-error runtime.
118
126
position : { start : { } , end : { } }
119
127
} ,
120
128
vfile ( ) ,
@@ -125,6 +133,91 @@ test('mdast-util-to-nlcst', (t) => {
125
133
'should fail when not given positional information (#2)'
126
134
)
127
135
136
+ t . deepEqual (
137
+ toNlcst (
138
+ /** @type {Root } */ ( {
139
+ type : 'root' ,
140
+ children : [ { type : 'text' , value : 'foo' } ] ,
141
+ position : { start : { line : 1 , column : 1 } , end : { line : 1 , column : 4 } }
142
+ } ) ,
143
+ vfile ( ) ,
144
+ ParseLatin
145
+ ) ,
146
+ {
147
+ type : 'RootNode' ,
148
+ children : [
149
+ {
150
+ type : 'ParagraphNode' ,
151
+ children : [
152
+ {
153
+ type : 'SentenceNode' ,
154
+ children : [
155
+ {
156
+ type : 'WordNode' ,
157
+ children : [
158
+ { type : 'TextNode' , value : 'foo' , position : undefined }
159
+ ] ,
160
+ position : undefined
161
+ }
162
+ ]
163
+ }
164
+ ]
165
+ }
166
+ ]
167
+ } ,
168
+ 'should handle a node in the tree w/o positional information'
169
+ )
170
+
171
+ t . deepEqual (
172
+ toNlcst (
173
+ /** @type {Root } */ ( {
174
+ type : 'root' ,
175
+ children : [ { type : 'image' , alt : 'a' } ] ,
176
+ position : { start : { line : 1 , column : 1 } , end : { line : 1 , column : 4 } }
177
+ } ) ,
178
+ vfile ( ) ,
179
+ ParseLatin
180
+ ) ,
181
+ {
182
+ type : 'RootNode' ,
183
+ children : [
184
+ {
185
+ type : 'ParagraphNode' ,
186
+ children : [
187
+ {
188
+ type : 'SentenceNode' ,
189
+ children : [
190
+ {
191
+ type : 'WordNode' ,
192
+ children : [
193
+ { type : 'TextNode' , value : 'a' , position : undefined }
194
+ ] ,
195
+ position : undefined
196
+ }
197
+ ]
198
+ }
199
+ ]
200
+ }
201
+ ]
202
+ } ,
203
+ 'should handle an image in the tree w/o positional information'
204
+ )
205
+
206
+ t . deepEqual (
207
+ toNlcst (
208
+ /** @type {InlineCode } */ ( {
209
+ type : 'inlineCode' ,
210
+ value : 'a' ,
211
+ position : { start : { line : 1 , column : 1 } , end : { line : 1 , column : 4 } }
212
+ } ) ,
213
+ vfile ( ) ,
214
+ ParseLatin ,
215
+ { ignore : [ 'inlineCode' ] }
216
+ ) ,
217
+ { type : 'RootNode' , children : [ ] } ,
218
+ 'should handle an image in the tree w/o positional information'
219
+ )
220
+
128
221
t . end ( )
129
222
} )
130
223
@@ -140,7 +233,7 @@ test('Fixtures', (t) => {
140
233
let expected
141
234
/** @type {Node } */
142
235
let mdast
143
- /** @type {Object.<string, unknown> } */
236
+ /** @type {Object.<string, unknown>|undefined } */
144
237
let options
145
238
146
239
while ( ++ index < files . length ) {
@@ -161,10 +254,11 @@ test('Fixtures', (t) => {
161
254
options = undefined
162
255
}
163
256
164
- mdast = remark ( )
165
- . use ( options && options . useRemarkGfm ? gfm : undefined )
166
- . use ( options && options . useRemarkFrontmatter ? frontmatter : undefined )
167
- . parse ( input )
257
+ const processor = remark ( )
258
+ if ( options && options . useRemarkGfm ) processor . use ( gfm )
259
+ if ( options && options . useRemarkFrontmatter ) processor . use ( frontmatter )
260
+
261
+ mdast = processor . parse ( input )
168
262
169
263
t . deepEqual (
170
264
toNlcst ( mdast , input , ParseLatin , options ) ,
0 commit comments