1
1
import assert from 'node:assert/strict'
2
- import fs from 'node:fs'
3
- import path from 'node:path'
2
+ import fs from 'node:fs/promises'
4
3
import test from 'tape'
5
4
import { slug } from 'github-slugger'
6
5
import { toHast } from 'mdast-util-to-hast'
@@ -11,55 +10,60 @@ import {gfm} from 'micromark-extension-gfm'
11
10
import { gfmFromMarkdown , gfmToMarkdown } from '../index.js'
12
11
import { spec } from './spec.js'
13
12
14
- test ( 'markdown -> mdast' , ( t ) => {
13
+ test ( 'markdown -> mdast' , async ( t ) => {
15
14
const files = spec . filter (
16
15
( example ) => ! / d i s a l l o w e d r a w h t m l / i. test ( example . category )
17
16
)
18
17
let index = - 1
19
18
20
19
while ( ++ index < files . length ) {
21
20
const example = files [ index ]
22
- const category = slug ( example . category )
23
- const name = index + '-' + category
24
- const fixtureHtmlPath = path . join ( 'test' , name + '.html' )
25
- const fixtureMarkdownPath = path . join ( 'test' , name + '.md' )
26
-
21
+ const name = index + '-' + slug ( example . category )
27
22
const mdast = fromMarkdown ( example . input , {
28
23
extensions : [ gfm ( ) ] ,
29
24
mdastExtensions : [ gfmFromMarkdown ( ) ]
30
25
} )
31
26
32
27
const hast = toHast ( mdast , { allowDangerousHtml : true } )
33
28
assert ( hast , 'expected node' )
34
-
35
- const html = toHtml ( hast , {
29
+ const actualHtml = toHtml ( hast , {
36
30
allowDangerousHtml : true ,
37
31
entities : { useNamedReferences : true } ,
38
32
closeSelfClosing : true
39
33
} )
40
34
41
35
/** @type {string } */
42
- let fixtureHtml
36
+ let expectedHtml
43
37
/** @type {string } */
44
- let fixtureMarkdown
38
+ let expectedMarkdown
39
+ const expectedUrl = new URL ( name + '.html' , import . meta. url )
40
+ const inputUrl = new URL ( name + '.md' , import . meta. url )
45
41
46
42
try {
47
- fixtureHtml = String ( fs . readFileSync ( fixtureHtmlPath ) )
43
+ expectedHtml = String ( await fs . readFile ( expectedUrl ) )
48
44
} catch {
49
- fixtureHtml = example . output . slice ( 0 , - 1 )
45
+ expectedHtml = example . output . slice ( 0 , - 1 )
50
46
}
51
47
52
- const md = toMarkdown ( mdast , { extensions : [ gfmToMarkdown ( ) ] } )
48
+ const actualMarkdown = toMarkdown ( mdast , { extensions : [ gfmToMarkdown ( ) ] } )
53
49
54
50
try {
55
- fixtureMarkdown = String ( fs . readFileSync ( fixtureMarkdownPath ) )
51
+ expectedMarkdown = String ( await fs . readFile ( inputUrl ) )
56
52
} catch {
57
- fixtureMarkdown = md
58
- fs . writeFileSync ( fixtureMarkdownPath , fixtureMarkdown )
53
+ expectedMarkdown = actualMarkdown
54
+ await fs . writeFile ( inputUrl , expectedMarkdown )
59
55
}
60
56
61
- t . deepEqual ( html , fixtureHtml , category + ' (' + index + ') -> html' )
62
- t . equal ( md , fixtureMarkdown , category + ' (' + index + ') -> md' )
57
+ t . deepEqual (
58
+ actualHtml ,
59
+ expectedHtml ,
60
+ example . category + ' (' + index + ') -> html'
61
+ )
62
+ t . equal (
63
+ actualMarkdown ,
64
+ expectedMarkdown ,
65
+ example . category + ' (' + index + ') -> md'
66
+ )
63
67
}
64
68
65
69
t . end ( )
0 commit comments