4
4
5
5
import fs from 'node:fs/promises'
6
6
import process from 'node:process'
7
- import test from 'tape'
7
+ import assert from 'node:assert/strict'
8
+ import test from 'node:test'
8
9
import { isHidden } from 'is-hidden'
9
10
import { h } from 'hastscript'
10
11
import { fromHtml } from 'hast-util-from-html'
@@ -18,46 +19,46 @@ import {removePosition} from 'unist-util-remove-position'
18
19
import { toMdast } from '../index.js'
19
20
import * as mod from '../index.js'
20
21
21
- test ( 'core' , ( t ) => {
22
- t . deepEqual (
22
+ test ( 'core' , ( ) => {
23
+ assert . deepEqual (
23
24
Object . keys ( mod ) . sort ( ) ,
24
25
[ 'defaultHandlers' , 'defaultNodeHandlers' , 'toMdast' ] ,
25
26
'should expose the public api'
26
27
)
27
28
28
- t . deepEqual (
29
+ assert . deepEqual (
29
30
toMdast ( u ( 'root' , [ h ( 'strong' , 'Alpha' ) ] ) ) ,
30
31
u ( 'root' , [ u ( 'strong' , [ u ( 'text' , 'Alpha' ) ] ) ] ) ,
31
32
'should transform hast to mdast'
32
33
)
33
34
34
- t . deepEqual (
35
+ assert . deepEqual (
35
36
toMdast ( u ( 'doctype' , { name : 'html' } ) ) ,
36
37
u ( 'root' , [ ] ) ,
37
38
'should transform a node w/o mdast representation to an empty root'
38
39
)
39
40
40
- t . deepEqual (
41
+ assert . deepEqual (
41
42
toMdast ( h ( 'q' , 'things' ) ) ,
42
43
{ type : 'root' , children : [ { type : 'text' , value : '"things"' } ] } ,
43
44
'should transform a node w/o multiple representations to a root'
44
45
)
45
46
46
- t . deepEqual (
47
+ assert . deepEqual (
47
48
// @ts -expect-error runtime.
48
49
toMdast ( u ( 'root' , [ u ( 'unknown' , 'text' ) ] ) ) ,
49
50
u ( 'root' , [ u ( 'text' , 'text' ) ] ) ,
50
51
'should transform unknown texts to `text`'
51
52
)
52
53
53
- t . deepEqual (
54
+ assert . deepEqual (
54
55
// @ts -expect-error runtime.
55
56
toMdast ( u ( 'root' , [ u ( 'unknown' , [ h ( 'em' ) ] ) ] ) ) ,
56
57
u ( 'root' , [ u ( 'emphasis' , [ ] ) ] ) ,
57
58
'should unwrap unknown parents'
58
59
)
59
60
60
- t . deepEqual (
61
+ assert . deepEqual (
61
62
// @ts -expect-error runtime.
62
63
toMdast ( u ( 'root' , [ u ( 'unknown' ) ] ) ) ,
63
64
u ( 'root' , [ ] ) ,
@@ -69,7 +70,7 @@ test('core', (t) => {
69
70
end : { line : 1 , column : 6 , offset : 5 }
70
71
}
71
72
72
- t . deepEqual (
73
+ assert . deepEqual (
73
74
toMdast ( {
74
75
type : 'root' ,
75
76
children : [
@@ -97,37 +98,37 @@ test('core', (t) => {
97
98
'should support positional information'
98
99
)
99
100
100
- t . deepEqual (
101
+ assert . deepEqual (
101
102
toMdast ( { type : 'element' , tagName : 'a' , children : [ ] } ) ,
102
103
{ type : 'link' , url : '' , title : null , children : [ ] } ,
103
104
'should support an `a` w/o `properties`'
104
105
)
105
106
106
- t . deepEqual (
107
+ assert . deepEqual (
107
108
toMdast ( { type : 'element' , tagName : 'iframe' , children : [ ] } ) ,
108
109
{ type : 'root' , children : [ ] } ,
109
110
'should support an `iframe` w/o `properties`'
110
111
)
111
112
112
- t . deepEqual (
113
+ assert . deepEqual (
113
114
toMdast ( { type : 'element' , tagName : 'img' , children : [ ] } ) ,
114
115
{ type : 'image' , url : '' , title : null , alt : '' } ,
115
116
'should support an `img` w/o `properties`'
116
117
)
117
118
118
- t . deepEqual (
119
+ assert . deepEqual (
119
120
toMdast ( { type : 'element' , tagName : 'input' , children : [ ] } ) ,
120
121
{ type : 'root' , children : [ ] } ,
121
122
'should support an `input` w/o `properties`'
122
123
)
123
124
124
- t . deepEqual (
125
+ assert . deepEqual (
125
126
toMdast ( { type : 'element' , tagName : 'select' , children : [ ] } ) ,
126
127
{ type : 'root' , children : [ ] } ,
127
128
'should support a `select` w/o `properties`'
128
129
)
129
130
130
- t . deepEqual (
131
+ assert . deepEqual (
131
132
toMdast ( {
132
133
type : 'element' ,
133
134
tagName : 'select' ,
@@ -138,27 +139,27 @@ test('core', (t) => {
138
139
'should support an `option` w/o `properties`'
139
140
)
140
141
141
- t . deepEqual (
142
+ assert . deepEqual (
142
143
toMdast ( { type : 'element' , tagName : 'video' , children : [ ] } ) ,
143
144
{ type : 'link' , title : null , url : '' , children : [ ] } ,
144
145
'should support a `video` w/o `properties`'
145
146
)
146
147
147
- t . deepEqual (
148
+ assert . deepEqual (
148
149
// @ts -expect-error: `children` missing.
149
150
toMdast ( { type : 'root' } ) ,
150
151
{ type : 'root' , children : [ ] } ,
151
152
'should support a `root` node w/o `children`'
152
153
)
153
154
154
- t . deepEqual (
155
+ assert . deepEqual (
155
156
// @ts -expect-error: `children` missing.
156
157
toMdast ( { type : 'element' , tagName : 'div' } ) ,
157
158
{ type : 'root' , children : [ ] } ,
158
159
'should support an `element` node w/o `children`'
159
160
)
160
161
161
- t . deepEqual (
162
+ assert . deepEqual (
162
163
toMdast ( h ( null , [ h ( 'div' , 'Alpha' ) ] ) , {
163
164
handlers : {
164
165
div ( ) {
@@ -170,13 +171,13 @@ test('core', (t) => {
170
171
'should support `handlers`'
171
172
)
172
173
173
- t . deepEqual (
174
+ assert . deepEqual (
174
175
toMdast ( h ( null , h ( 'p' , 'Alpha\nBeta' ) ) ) ,
175
176
u ( 'root' , [ u ( 'paragraph' , [ u ( 'text' , 'Alpha Beta' ) ] ) ] ) ,
176
177
'should collapse newline to a single space'
177
178
)
178
179
179
- t . deepEqual (
180
+ assert . deepEqual (
180
181
toMdast ( h ( null , h ( 'p' , 'Alpha\nBeta' ) ) , { newlines : true } ) ,
181
182
u ( 'root' , [ u ( 'paragraph' , [ u ( 'text' , 'Alpha\nBeta' ) ] ) ] ) ,
182
183
'should support `newlines: true`'
@@ -189,7 +190,7 @@ test('core', (t) => {
189
190
'.'
190
191
] )
191
192
192
- t . deepEqual (
193
+ assert . deepEqual (
193
194
toMdast ( phrasingTree ) ,
194
195
u ( 'root' , [
195
196
u ( 'strong' , [ u ( 'text' , 'Importance' ) ] ) ,
@@ -200,7 +201,7 @@ test('core', (t) => {
200
201
'should infer document if not needed'
201
202
)
202
203
203
- t . deepEqual (
204
+ assert . deepEqual (
204
205
toMdast ( phrasingTree , { document : true } ) ,
205
206
u ( 'root' , [
206
207
u ( 'paragraph' , [
@@ -240,7 +241,7 @@ test('core', (t) => {
240
241
241
242
// Reference check: `text`s are wrapped in `paragraph`s because the unknown
242
243
// node is seen as “block”
243
- t . deepEqual (
244
+ assert . deepEqual (
244
245
referenceTree . type === 'root' &&
245
246
referenceTree . children . length === 3 &&
246
247
referenceTree . children [ 0 ] . type === 'paragraph' ,
@@ -249,18 +250,16 @@ test('core', (t) => {
249
250
)
250
251
251
252
// Actual check: no `paragraph` is added because `hName` is added.
252
- t . deepEqual (
253
+ assert . deepEqual (
253
254
explicitTree . type === 'root' &&
254
255
explicitTree . children . length === 3 &&
255
256
explicitTree . children [ 0 ] . type !== 'paragraph' ,
256
257
true ,
257
258
'should support `node.data.hName` to infer phrasing (2)'
258
259
)
259
-
260
- t . end ( )
261
260
} )
262
261
263
- test ( 'fixtures' , async ( t ) => {
262
+ test ( 'fixtures' , async ( ) => {
264
263
const fixtures = new URL ( 'fixtures/' , import . meta. url )
265
264
const folders = await fs . readdir ( fixtures )
266
265
@@ -269,72 +268,69 @@ test('fixtures', async (t) => {
269
268
continue
270
269
}
271
270
272
- t . test ( folder , async ( t ) => {
273
- const configUrl = new URL ( folder + '/index.json' , fixtures )
274
- const inputUrl = new URL ( folder + '/index.html' , fixtures )
275
- const expectedUrl = new URL ( folder + '/index.md' , fixtures )
276
- const input = String ( await fs . readFile ( inputUrl ) )
277
- // Replace middots with spaces (useful for trailing spaces).
278
- . replace ( / · / g, ' ' )
279
- /** @type {({stringify?: boolean, tree?: boolean} & Options) | undefined } */
280
- let config
281
-
282
- try {
283
- config = JSON . parse ( String ( await fs . readFile ( configUrl ) ) )
284
- } catch { }
285
-
286
- const hast = fromHtml ( input )
287
- const mdast = toMdast ( hast , config )
288
- removePosition ( mdast , true )
289
-
290
- t . doesNotThrow ( ( ) => {
291
- mdastAssert ( mdast )
292
- } , 'should produce valid mdast nodes' )
293
-
294
- // Ignore the invalid base test.
295
- if ( folder === 'base-invalid' ) {
296
- t . end ( )
297
- return
298
- }
299
-
300
- const actual = toMarkdown ( mdast , {
301
- extensions : [ gfmToMarkdown ( ) ] ,
302
- fences : true
303
- } )
304
- /** @type {string } */
305
- let expected
271
+ const configUrl = new URL ( folder + '/index.json' , fixtures )
272
+ const inputUrl = new URL ( folder + '/index.html' , fixtures )
273
+ const expectedUrl = new URL ( folder + '/index.md' , fixtures )
274
+ const input = String ( await fs . readFile ( inputUrl ) )
275
+ // Replace middots with spaces (useful for trailing spaces).
276
+ . replace ( / · / g, ' ' )
277
+ /** @type {({stringify?: boolean, tree?: boolean} & Options) | undefined } */
278
+ let config
279
+
280
+ try {
281
+ config = JSON . parse ( String ( await fs . readFile ( configUrl ) ) )
282
+ } catch { }
283
+
284
+ const hast = fromHtml ( input )
285
+ const mdast = toMdast ( hast , config )
286
+ removePosition ( mdast , true )
287
+
288
+ assert . doesNotThrow ( ( ) => {
289
+ mdastAssert ( mdast )
290
+ } , folder + ': should produce valid mdast nodes' )
291
+
292
+ // Ignore the invalid base test.
293
+ if ( folder === 'base-invalid' ) {
294
+ continue
295
+ }
306
296
307
- try {
308
- if ( 'UPDATE' in process . env ) {
309
- throw new Error ( 'Update!' )
310
- }
297
+ const actual = toMarkdown ( mdast , {
298
+ extensions : [ gfmToMarkdown ( ) ] ,
299
+ fences : true
300
+ } )
301
+ /** @type {string } */
302
+ let expected
311
303
312
- expected = String ( await fs . readFile ( expectedUrl ) )
313
- } catch {
314
- expected = actual
315
- await fs . writeFile ( expectedUrl , actual )
304
+ try {
305
+ if ( 'UPDATE' in process . env ) {
306
+ throw new Error ( 'Update!' )
316
307
}
317
308
318
- if ( ! config || config . stringify !== false ) {
319
- t . deepEqual ( actual , expected , 'should produce the same documents' )
320
- }
309
+ expected = String ( await fs . readFile ( expectedUrl ) )
310
+ } catch {
311
+ expected = actual
312
+ await fs . writeFile ( expectedUrl , actual )
313
+ }
321
314
322
- if ( ! config || config . tree !== false ) {
323
- const expectedMdast = fromMarkdown ( expected , {
324
- extensions : [ gfm ( ) ] ,
325
- mdastExtensions : [ gfmFromMarkdown ( ) ]
326
- } )
327
- removePosition ( expectedMdast , true )
328
- t . deepEqual (
329
- mdast ,
330
- expectedMdast ,
331
- 'should produce the same tree as remark'
332
- )
333
- }
315
+ if ( ! config || config . stringify !== false ) {
316
+ assert . deepEqual (
317
+ actual ,
318
+ expected ,
319
+ folder + ': should produce the same documents'
320
+ )
321
+ }
334
322
335
- t . end ( )
336
- } )
323
+ if ( ! config || config . tree !== false ) {
324
+ const expectedMdast = fromMarkdown ( expected , {
325
+ extensions : [ gfm ( ) ] ,
326
+ mdastExtensions : [ gfmFromMarkdown ( ) ]
327
+ } )
328
+ removePosition ( expectedMdast , true )
329
+ assert . deepEqual (
330
+ mdast ,
331
+ expectedMdast ,
332
+ folder + ': should produce the same tree as remark'
333
+ )
334
+ }
337
335
}
338
-
339
- t . end ( )
340
336
} )
0 commit comments