44
55import fs from 'node:fs/promises'
66import process from 'node:process'
7- import test from 'tape'
7+ import assert from 'node:assert/strict'
8+ import test from 'node:test'
89import { isHidden } from 'is-hidden'
910import { h } from 'hastscript'
1011import { fromHtml } from 'hast-util-from-html'
@@ -18,46 +19,46 @@ import {removePosition} from 'unist-util-remove-position'
1819import { toMdast } from '../index.js'
1920import * as mod from '../index.js'
2021
21- test ( 'core' , ( t ) => {
22- t . deepEqual (
22+ test ( 'core' , ( ) => {
23+ assert . deepEqual (
2324 Object . keys ( mod ) . sort ( ) ,
2425 [ 'defaultHandlers' , 'defaultNodeHandlers' , 'toMdast' ] ,
2526 'should expose the public api'
2627 )
2728
28- t . deepEqual (
29+ assert . deepEqual (
2930 toMdast ( u ( 'root' , [ h ( 'strong' , 'Alpha' ) ] ) ) ,
3031 u ( 'root' , [ u ( 'strong' , [ u ( 'text' , 'Alpha' ) ] ) ] ) ,
3132 'should transform hast to mdast'
3233 )
3334
34- t . deepEqual (
35+ assert . deepEqual (
3536 toMdast ( u ( 'doctype' , { name : 'html' } ) ) ,
3637 u ( 'root' , [ ] ) ,
3738 'should transform a node w/o mdast representation to an empty root'
3839 )
3940
40- t . deepEqual (
41+ assert . deepEqual (
4142 toMdast ( h ( 'q' , 'things' ) ) ,
4243 { type : 'root' , children : [ { type : 'text' , value : '"things"' } ] } ,
4344 'should transform a node w/o multiple representations to a root'
4445 )
4546
46- t . deepEqual (
47+ assert . deepEqual (
4748 // @ts -expect-error runtime.
4849 toMdast ( u ( 'root' , [ u ( 'unknown' , 'text' ) ] ) ) ,
4950 u ( 'root' , [ u ( 'text' , 'text' ) ] ) ,
5051 'should transform unknown texts to `text`'
5152 )
5253
53- t . deepEqual (
54+ assert . deepEqual (
5455 // @ts -expect-error runtime.
5556 toMdast ( u ( 'root' , [ u ( 'unknown' , [ h ( 'em' ) ] ) ] ) ) ,
5657 u ( 'root' , [ u ( 'emphasis' , [ ] ) ] ) ,
5758 'should unwrap unknown parents'
5859 )
5960
60- t . deepEqual (
61+ assert . deepEqual (
6162 // @ts -expect-error runtime.
6263 toMdast ( u ( 'root' , [ u ( 'unknown' ) ] ) ) ,
6364 u ( 'root' , [ ] ) ,
@@ -69,7 +70,7 @@ test('core', (t) => {
6970 end : { line : 1 , column : 6 , offset : 5 }
7071 }
7172
72- t . deepEqual (
73+ assert . deepEqual (
7374 toMdast ( {
7475 type : 'root' ,
7576 children : [
@@ -97,37 +98,37 @@ test('core', (t) => {
9798 'should support positional information'
9899 )
99100
100- t . deepEqual (
101+ assert . deepEqual (
101102 toMdast ( { type : 'element' , tagName : 'a' , children : [ ] } ) ,
102103 { type : 'link' , url : '' , title : null , children : [ ] } ,
103104 'should support an `a` w/o `properties`'
104105 )
105106
106- t . deepEqual (
107+ assert . deepEqual (
107108 toMdast ( { type : 'element' , tagName : 'iframe' , children : [ ] } ) ,
108109 { type : 'root' , children : [ ] } ,
109110 'should support an `iframe` w/o `properties`'
110111 )
111112
112- t . deepEqual (
113+ assert . deepEqual (
113114 toMdast ( { type : 'element' , tagName : 'img' , children : [ ] } ) ,
114115 { type : 'image' , url : '' , title : null , alt : '' } ,
115116 'should support an `img` w/o `properties`'
116117 )
117118
118- t . deepEqual (
119+ assert . deepEqual (
119120 toMdast ( { type : 'element' , tagName : 'input' , children : [ ] } ) ,
120121 { type : 'root' , children : [ ] } ,
121122 'should support an `input` w/o `properties`'
122123 )
123124
124- t . deepEqual (
125+ assert . deepEqual (
125126 toMdast ( { type : 'element' , tagName : 'select' , children : [ ] } ) ,
126127 { type : 'root' , children : [ ] } ,
127128 'should support a `select` w/o `properties`'
128129 )
129130
130- t . deepEqual (
131+ assert . deepEqual (
131132 toMdast ( {
132133 type : 'element' ,
133134 tagName : 'select' ,
@@ -138,27 +139,27 @@ test('core', (t) => {
138139 'should support an `option` w/o `properties`'
139140 )
140141
141- t . deepEqual (
142+ assert . deepEqual (
142143 toMdast ( { type : 'element' , tagName : 'video' , children : [ ] } ) ,
143144 { type : 'link' , title : null , url : '' , children : [ ] } ,
144145 'should support a `video` w/o `properties`'
145146 )
146147
147- t . deepEqual (
148+ assert . deepEqual (
148149 // @ts -expect-error: `children` missing.
149150 toMdast ( { type : 'root' } ) ,
150151 { type : 'root' , children : [ ] } ,
151152 'should support a `root` node w/o `children`'
152153 )
153154
154- t . deepEqual (
155+ assert . deepEqual (
155156 // @ts -expect-error: `children` missing.
156157 toMdast ( { type : 'element' , tagName : 'div' } ) ,
157158 { type : 'root' , children : [ ] } ,
158159 'should support an `element` node w/o `children`'
159160 )
160161
161- t . deepEqual (
162+ assert . deepEqual (
162163 toMdast ( h ( null , [ h ( 'div' , 'Alpha' ) ] ) , {
163164 handlers : {
164165 div ( ) {
@@ -170,13 +171,13 @@ test('core', (t) => {
170171 'should support `handlers`'
171172 )
172173
173- t . deepEqual (
174+ assert . deepEqual (
174175 toMdast ( h ( null , h ( 'p' , 'Alpha\nBeta' ) ) ) ,
175176 u ( 'root' , [ u ( 'paragraph' , [ u ( 'text' , 'Alpha Beta' ) ] ) ] ) ,
176177 'should collapse newline to a single space'
177178 )
178179
179- t . deepEqual (
180+ assert . deepEqual (
180181 toMdast ( h ( null , h ( 'p' , 'Alpha\nBeta' ) ) , { newlines : true } ) ,
181182 u ( 'root' , [ u ( 'paragraph' , [ u ( 'text' , 'Alpha\nBeta' ) ] ) ] ) ,
182183 'should support `newlines: true`'
@@ -189,7 +190,7 @@ test('core', (t) => {
189190 '.'
190191 ] )
191192
192- t . deepEqual (
193+ assert . deepEqual (
193194 toMdast ( phrasingTree ) ,
194195 u ( 'root' , [
195196 u ( 'strong' , [ u ( 'text' , 'Importance' ) ] ) ,
@@ -200,7 +201,7 @@ test('core', (t) => {
200201 'should infer document if not needed'
201202 )
202203
203- t . deepEqual (
204+ assert . deepEqual (
204205 toMdast ( phrasingTree , { document : true } ) ,
205206 u ( 'root' , [
206207 u ( 'paragraph' , [
@@ -240,7 +241,7 @@ test('core', (t) => {
240241
241242 // Reference check: `text`s are wrapped in `paragraph`s because the unknown
242243 // node is seen as “block”
243- t . deepEqual (
244+ assert . deepEqual (
244245 referenceTree . type === 'root' &&
245246 referenceTree . children . length === 3 &&
246247 referenceTree . children [ 0 ] . type === 'paragraph' ,
@@ -249,18 +250,16 @@ test('core', (t) => {
249250 )
250251
251252 // Actual check: no `paragraph` is added because `hName` is added.
252- t . deepEqual (
253+ assert . deepEqual (
253254 explicitTree . type === 'root' &&
254255 explicitTree . children . length === 3 &&
255256 explicitTree . children [ 0 ] . type !== 'paragraph' ,
256257 true ,
257258 'should support `node.data.hName` to infer phrasing (2)'
258259 )
259-
260- t . end ( )
261260} )
262261
263- test ( 'fixtures' , async ( t ) => {
262+ test ( 'fixtures' , async ( ) => {
264263 const fixtures = new URL ( 'fixtures/' , import . meta. url )
265264 const folders = await fs . readdir ( fixtures )
266265
@@ -269,72 +268,69 @@ test('fixtures', async (t) => {
269268 continue
270269 }
271270
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+ }
306296
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
311303
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!' )
316307 }
317308
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+ }
321314
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+ }
334322
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+ }
337335 }
338-
339- t . end ( )
340336} )
0 commit comments