1
1
/**
2
- * @typedef {import('hast').Root } Root
3
- * @typedef {import('hast').Element } Element
4
2
* @typedef {import('../index.js').Options } Options
5
3
*/
6
4
7
- import fs from 'node:fs'
8
- import path from 'node:path'
5
+ import fs from 'node:fs/promises'
9
6
import test from 'tape'
10
- import { u } from 'unist-builder'
11
- import { h } from 'hastscript'
12
7
import { isHidden } from 'is-hidden'
13
- import { unified } from 'unified'
14
- import remarkParse from 'remark-parse'
15
- import remarkGfm from 'remark-gfm'
16
- import rehypeParse from 'rehype-parse'
17
- import remarkStringify from 'remark-stringify'
18
- import { assert } from 'mdast-util-assert'
8
+ import { h } from 'hastscript'
9
+ import { fromHtml } from 'hast-util-from-html'
10
+ import { assert as mdastAssert } from 'mdast-util-assert'
11
+ import { fromMarkdown } from 'mdast-util-from-markdown'
12
+ import { gfmFromMarkdown , gfmToMarkdown } from 'mdast-util-gfm'
13
+ import { toMarkdown } from 'mdast-util-to-markdown'
14
+ import { gfm } from 'micromark-extension-gfm'
15
+ import { u } from 'unist-builder'
19
16
import { removePosition } from 'unist-util-remove-position'
20
17
import { one , all , defaultHandlers , toMdast } from '../index.js'
21
18
import { wrapNeeded } from '../lib/util/wrap.js'
@@ -47,9 +44,9 @@ test('custom nodes', (t) => {
47
44
} )
48
45
49
46
test ( 'exports' , ( t ) => {
50
- t . assert ( one , 'should export `one`' )
51
- t . assert ( all , 'should export `all`' )
52
- t . assert ( defaultHandlers , 'should export `defaultHandlers`' )
47
+ t . ok ( one , 'should export `one`' )
48
+ t . ok ( all , 'should export `all`' )
49
+ t . ok ( defaultHandlers , 'should export `defaultHandlers`' )
53
50
t . end ( )
54
51
} )
55
52
@@ -190,54 +187,41 @@ test('core', (t) => {
190
187
t . end ( )
191
188
} )
192
189
193
- test ( 'fixtures' , ( t ) => {
194
- const fixtures = path . join ( 'test ', 'fixtures' )
195
- const remark = unified ( ) . use ( remarkParse ) . use ( remarkGfm ) . use ( remarkStringify )
190
+ test ( 'fixtures' , async ( t ) => {
191
+ const fixtures = new URL ( 'fixtures/ ', import . meta . url )
192
+ const folders = await fs . readdir ( fixtures )
196
193
197
- fs . readdirSync ( fixtures )
198
- . filter ( ( d ) => ! isHidden ( d ) )
199
- // eslint-disable-next-line unicorn/no-array-for-each
200
- . forEach ( ( d ) => check ( d ) )
201
-
202
- t . end ( )
194
+ for ( const folder of folders ) {
195
+ if ( isHidden ( folder ) ) {
196
+ continue
197
+ }
203
198
204
- function check ( /** @type {string } */ name ) {
205
- const ignore = / ^ b a s e \b / . test ( name )
199
+ const ignore = / ^ b a s e \b / . test ( folder )
206
200
207
- t . test ( name , ( st ) => {
201
+ t . test ( folder , async ( st ) => {
208
202
const input = String (
209
- fs . readFileSync ( path . join ( fixtures , name , ' index.html') )
203
+ await fs . readFile ( new URL ( folder + '/ index.html', fixtures ) )
210
204
)
211
- let output = String (
212
- fs . readFileSync ( path . join ( fixtures , name , ' index.md') )
205
+ const expected = String (
206
+ await fs . readFile ( new URL ( folder + '/ index.md', fixtures ) )
213
207
)
208
+ // Replace middots with spaces (useful for trailing spaces).
209
+ . replace ( / · / g, ' ' )
214
210
/** @type {({stringify?: boolean, tree?: boolean} & Options) | undefined } */
215
211
let config
216
212
217
213
try {
218
214
config = JSON . parse (
219
- String ( fs . readFileSync ( path . join ( fixtures , name , ' index.json') ) )
215
+ String ( await fs . readFile ( new URL ( folder + '/ index.json', fixtures ) ) )
220
216
)
221
217
} catch { }
222
218
223
- const fromHtml = unified ( )
224
- . use ( rehypeParse )
225
- // @ts -expect-error: turn into different tree..
226
- . use ( ( ) => {
227
- return transformer
228
- function transformer ( /** @type {Root } */ tree ) {
229
- return toMdast ( tree , config )
230
- }
231
- } )
232
- . use ( remarkStringify )
233
-
234
- const tree = removePosition ( fromHtml . runSync ( fromHtml . parse ( input ) ) , true )
235
-
236
- // Replace middots with spaces (useful for trailing spaces).
237
- output = output . replace ( / · / g, ' ' )
219
+ const hast = fromHtml ( input )
220
+ const mdast = toMdast ( hast , config )
221
+ removePosition ( mdast , true )
238
222
239
223
st . doesNotThrow ( ( ) => {
240
- assert ( tree )
224
+ mdastAssert ( mdast )
241
225
} , 'should produce valid mdast nodes' )
242
226
243
227
if ( ignore ) {
@@ -247,23 +231,30 @@ test('fixtures', (t) => {
247
231
248
232
if ( ! config || config . stringify !== false ) {
249
233
st . deepEqual (
250
- remark . stringify ( tree ) ,
251
- output ,
234
+ toMarkdown ( mdast , { extensions : [ gfmToMarkdown ( ) ] } ) ,
235
+ expected ,
252
236
'should produce the same documents'
253
237
)
254
238
}
255
239
256
240
if ( ! config || config . tree !== false ) {
241
+ const expectedMdast = fromMarkdown ( expected , {
242
+ extensions : [ gfm ( ) ] ,
243
+ mdastExtensions : [ gfmFromMarkdown ( ) ]
244
+ } )
245
+ removePosition ( expectedMdast , true )
257
246
st . deepEqual (
258
- tree ,
259
- removePosition ( remark . runSync ( remark . parse ( output ) ) , true ) ,
247
+ mdast ,
248
+ expectedMdast ,
260
249
'should produce the same tree as remark'
261
250
)
262
251
}
263
252
264
253
st . end ( )
265
254
} )
266
255
}
256
+
257
+ t . end ( )
267
258
} )
268
259
269
260
test ( 'handlers option' , ( t ) => {
0 commit comments