1
+ /**
2
+ * @typedef {import('mdast').Root } Root
3
+ * @typedef {import('mdast').Content } Content
4
+ * @typedef {import('mdast').BlockContent } BlockContent
5
+ * @typedef {import('./index.js').Options } Options
6
+ */
7
+
1
8
import test from 'tape'
2
9
import { unified } from 'unified'
3
10
import { gfmToMarkdown } from 'mdast-util-gfm'
@@ -19,6 +26,7 @@ test('remarkStringify', (t) => {
19
26
20
27
t . throws (
21
28
( ) => {
29
+ // @ts -expect-error: not a node.
22
30
unified ( ) . use ( remarkStringify ) . stringify ( false )
23
31
} ,
24
32
/ f a l s e / ,
@@ -27,6 +35,7 @@ test('remarkStringify', (t) => {
27
35
28
36
t . throws (
29
37
( ) => {
38
+ // @ts -expect-error: not a known node.
30
39
unified ( ) . use ( remarkStringify ) . stringify ( { type : 'unicorn' } )
31
40
} ,
32
41
/ u n i c o r n / ,
@@ -38,7 +47,12 @@ test('remarkStringify', (t) => {
38
47
unified ( )
39
48
. use ( remarkStringify )
40
49
. data ( 'settings' , { bullet : true } )
41
- . stringify ( { type : 'listItem' } )
50
+ . stringify ( {
51
+ type : 'root' ,
52
+ children : [
53
+ { type : 'list' , children : [ { type : 'listItem' , children : [ ] } ] }
54
+ ]
55
+ } )
42
56
} ,
43
57
/ o p t i o n s \. b u l l e t / ,
44
58
'should throw when `options.bullet` is not a valid list bullet'
@@ -49,7 +63,12 @@ test('remarkStringify', (t) => {
49
63
unified ( )
50
64
. use ( remarkStringify )
51
65
. data ( 'settings' , { listItemIndent : 'foo' } )
52
- . stringify ( { type : 'listItem' } )
66
+ . stringify ( {
67
+ type : 'root' ,
68
+ children : [
69
+ { type : 'list' , children : [ { type : 'listItem' , children : [ ] } ] }
70
+ ]
71
+ } )
53
72
} ,
54
73
/ o p t i o n s \. l i s t I t e m I n d e n t / ,
55
74
'should throw when `options.listItemIndent` is not a valid constant'
@@ -60,7 +79,7 @@ test('remarkStringify', (t) => {
60
79
unified ( )
61
80
. use ( remarkStringify )
62
81
. data ( 'settings' , { rule : true } )
63
- . stringify ( { type : 'thematicBreak' } )
82
+ . stringify ( { type : 'root' , children : [ { type : ' thematicBreak'} ] } )
64
83
} ,
65
84
/ o p t i o n s \. r u l e / ,
66
85
'should throw when `options.rule` is not a valid horizontal rule bullet'
@@ -71,7 +90,7 @@ test('remarkStringify', (t) => {
71
90
unified ( )
72
91
. use ( remarkStringify )
73
92
. data ( 'settings' , { ruleRepetition : 1 } )
74
- . stringify ( { type : 'thematicBreak' } )
93
+ . stringify ( { type : 'root' , children : [ { type : ' thematicBreak'} ] } )
75
94
} ,
76
95
/ o p t i o n s \. r u l e R e p e t i t i o n / ,
77
96
'should throw when `options.ruleRepetition` is too low'
@@ -82,7 +101,7 @@ test('remarkStringify', (t) => {
82
101
unified ( )
83
102
. use ( remarkStringify )
84
103
. data ( 'settings' , { ruleRepetition : true } )
85
- . stringify ( { type : 'thematicBreak' } )
104
+ . stringify ( { type : 'root' , children : [ { type : ' thematicBreak'} ] } )
86
105
} ,
87
106
/ o p t i o n s \. r u l e R e p e t i t i o n / ,
88
107
'should throw when `options.ruleRepetition` is not a number'
@@ -93,7 +112,7 @@ test('remarkStringify', (t) => {
93
112
unified ( )
94
113
. use ( remarkStringify )
95
114
. data ( 'settings' , { emphasis : '-' } )
96
- . stringify ( { type : 'emphasis' } )
115
+ . stringify ( { type : 'root' , children : [ { type : ' emphasis', children : [ ] } ] } )
97
116
} ,
98
117
/ o p t i o n s \. e m p h a s i s / ,
99
118
'should throw when `options.emphasis` is not a valid emphasis marker'
@@ -104,7 +123,7 @@ test('remarkStringify', (t) => {
104
123
unified ( )
105
124
. use ( remarkStringify )
106
125
. data ( 'settings' , { strong : '-' } )
107
- . stringify ( { type : 'strong' } )
126
+ . stringify ( { type : 'root' , children : [ { type : ' strong', children : [ ] } ] } )
108
127
} ,
109
128
/ o p t i o n s \. s t r o n g / ,
110
129
'should throw when `options.strong` is not a valid emphasis marker'
@@ -115,7 +134,7 @@ test('remarkStringify', (t) => {
115
134
unified ( )
116
135
. use ( remarkStringify )
117
136
. data ( 'settings' , { fence : '-' } )
118
- . stringify ( { type : 'code' } )
137
+ . stringify ( { type : 'root' , children : [ { type : ' code', value : '' } ] } )
119
138
} ,
120
139
/ o p t i o n s \. f e n c e / ,
121
140
'should throw when `options.fence` is not a valid fence marker'
@@ -248,13 +267,10 @@ test('remarkStringify', (t) => {
248
267
)
249
268
250
269
t . end ( )
251
-
252
- function toString ( value ) {
253
- return String ( unified ( ) . use ( remarkStringify ) . stringify ( value ) )
254
- }
255
270
} )
256
271
257
272
t . test ( 'should support optional list item fields' , ( t ) => {
273
+ /** @type {BlockContent[] } */
258
274
const children = [
259
275
{ type : 'paragraph' , children : [ { type : 'text' , value : 'alpha' } ] } ,
260
276
{
@@ -284,20 +300,12 @@ test('remarkStringify', (t) => {
284
300
)
285
301
286
302
t . end ( )
287
-
288
- function toString ( value ) {
289
- return String ( unified ( ) . use ( remarkStringify ) . stringify ( value ) )
290
- }
291
303
} )
292
304
293
305
t . test ( 'should support empty list items' , ( t ) => {
294
306
t . equal ( toString ( { type : 'listItem' , children : [ ] } ) , '*\n' )
295
307
296
308
t . end ( )
297
-
298
- function toString ( value ) {
299
- return String ( unified ( ) . use ( remarkStringify ) . stringify ( value ) )
300
- }
301
309
} )
302
310
303
311
t . test ( 'should process references with casing properly' , ( t ) => {
@@ -340,23 +348,25 @@ test('remarkStringify', (t) => {
340
348
toString ( {
341
349
type : 'linkReference' ,
342
350
identifier : 'a' ,
351
+ referenceType : 'full' ,
343
352
children : [ { type : 'text' , value : 'b' } ]
344
353
} ) ,
345
354
'[b][a]\n' ,
346
355
'link reference'
347
356
)
348
357
349
358
t . equal (
350
- toString ( { type : 'imageReference' , identifier : 'a' , alt : 'b' } ) ,
359
+ toString ( {
360
+ type : 'imageReference' ,
361
+ referenceType : 'full' ,
362
+ identifier : 'a' ,
363
+ alt : 'b'
364
+ } ) ,
351
365
'![b][a]\n' ,
352
366
'image reference'
353
367
)
354
368
355
369
t . end ( )
356
-
357
- function toString ( value ) {
358
- return String ( unified ( ) . use ( remarkStringify ) . stringify ( value ) )
359
- }
360
370
} )
361
371
362
372
t . test ( 'should stringify mailto links properly' , ( t ) => {
@@ -407,12 +417,12 @@ test('stringify escapes', (t) => {
407
417
t . equal ( toString ( 'a&b' ) , 'a\\&b\n' , 'entities' )
408
418
t . equal ( toString ( 'a]b' ) , 'a]b\n' , '`]`' )
409
419
t . equal (
410
- toString ( { type : 'link' , children : [ { type : 'text' , value : 'a]b' } ] } ) ,
420
+ toString ( { type : 'link' , url : '' , children : [ { type : 'text' , value : 'a]b' } ] } ) ,
411
421
'[a\\]b]()\n' ,
412
422
'`]` (in links)'
413
423
)
414
424
t . equal (
415
- toString ( { type : 'image' , alt : 'a]b' } ) ,
425
+ toString ( { type : 'image' , url : '' , alt : 'a]b' } ) ,
416
426
'![a\\]b]()\n' ,
417
427
'`]` (in images)'
418
428
)
@@ -456,7 +466,7 @@ test('stringify escapes', (t) => {
456
466
type : 'paragraph' ,
457
467
children : [
458
468
{ type : 'text' , value : '!' } ,
459
- { type : 'link' , children : [ { type : 'text' , value : 'a' } ] }
469
+ { type : 'link' , url : '' , children : [ { type : 'text' , value : 'a' } ] }
460
470
]
461
471
} ) ,
462
472
'\\![a]()\n' ,
@@ -549,7 +559,7 @@ test('extensions', (t) => {
549
559
{
550
560
type : 'list' ,
551
561
ordered : false ,
552
- start : null ,
562
+ start : undefined ,
553
563
spread : false ,
554
564
children : [
555
565
{
@@ -602,11 +612,19 @@ test('extensions', (t) => {
602
612
t . end ( )
603
613
} )
604
614
615
+ /**
616
+ * @param {Root|Content|string } value
617
+ * @param {Options } [options]
618
+ * @returns {string }
619
+ */
605
620
function toString ( value , options ) {
606
- const tree =
607
- typeof value === 'string'
608
- ? { type : 'paragraph' , children : [ { type : 'text' , value} ] }
609
- : value
621
+ if ( typeof value === 'string' ) {
622
+ value = { type : 'paragraph' , children : [ { type : 'text' , value} ] }
623
+ }
624
+
625
+ if ( value . type !== 'root' ) {
626
+ value = { type : 'root' , children : [ value ] }
627
+ }
610
628
611
- return unified ( ) . use ( remarkStringify , options ) . stringify ( tree )
629
+ return unified ( ) . use ( remarkStringify , options ) . stringify ( value )
612
630
}
0 commit comments