11/**
2- * @typedef {import('hast').Root } Root
3- * @typedef {import('hast').Element } Element
42 * @typedef {import('../index.js').Options } Options
53 */
64
7- import fs from 'node:fs'
8- import path from 'node:path'
5+ import fs from 'node:fs/promises'
96import test from 'tape'
10- import { u } from 'unist-builder'
11- import { h } from 'hastscript'
127import { 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'
1916import { removePosition } from 'unist-util-remove-position'
2017import { one , all , defaultHandlers , toMdast } from '../index.js'
2118import { wrapNeeded } from '../lib/util/wrap.js'
@@ -47,9 +44,9 @@ test('custom nodes', (t) => {
4744} )
4845
4946test ( '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`' )
5350 t . end ( )
5451} )
5552
@@ -190,54 +187,41 @@ test('core', (t) => {
190187 t . end ( )
191188} )
192189
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 )
196193
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+ }
203198
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 )
206200
207- t . test ( name , ( st ) => {
201+ t . test ( folder , async ( st ) => {
208202 const input = String (
209- fs . readFileSync ( path . join ( fixtures , name , ' index.html') )
203+ await fs . readFile ( new URL ( folder + '/ index.html', fixtures ) )
210204 )
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 ) )
213207 )
208+ // Replace middots with spaces (useful for trailing spaces).
209+ . replace ( / · / g, ' ' )
214210 /** @type {({stringify?: boolean, tree?: boolean} & Options) | undefined } */
215211 let config
216212
217213 try {
218214 config = JSON . parse (
219- String ( fs . readFileSync ( path . join ( fixtures , name , ' index.json') ) )
215+ String ( await fs . readFile ( new URL ( folder + '/ index.json', fixtures ) ) )
220216 )
221217 } catch { }
222218
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 )
238222
239223 st . doesNotThrow ( ( ) => {
240- assert ( tree )
224+ mdastAssert ( mdast )
241225 } , 'should produce valid mdast nodes' )
242226
243227 if ( ignore ) {
@@ -247,23 +231,30 @@ test('fixtures', (t) => {
247231
248232 if ( ! config || config . stringify !== false ) {
249233 st . deepEqual (
250- remark . stringify ( tree ) ,
251- output ,
234+ toMarkdown ( mdast , { extensions : [ gfmToMarkdown ( ) ] } ) ,
235+ expected ,
252236 'should produce the same documents'
253237 )
254238 }
255239
256240 if ( ! config || config . tree !== false ) {
241+ const expectedMdast = fromMarkdown ( expected , {
242+ extensions : [ gfm ( ) ] ,
243+ mdastExtensions : [ gfmFromMarkdown ( ) ]
244+ } )
245+ removePosition ( expectedMdast , true )
257246 st . deepEqual (
258- tree ,
259- removePosition ( remark . runSync ( remark . parse ( output ) ) , true ) ,
247+ mdast ,
248+ expectedMdast ,
260249 'should produce the same tree as remark'
261250 )
262251 }
263252
264253 st . end ( )
265254 } )
266255 }
256+
257+ t . end ( )
267258} )
268259
269260test ( 'handlers option' , ( t ) => {
0 commit comments