2
2
* @typedef {import('mdast').Root } Root
3
3
*/
4
4
5
+ import assert from 'node:assert/strict'
5
6
import { Buffer } from 'node:buffer'
6
- import fs from 'fs'
7
- import path from 'path'
8
- import assert from 'assert'
7
+ import fs from 'node:fs/promises'
9
8
import test from 'tape'
10
- import { unified } from 'unified'
11
- import rehypeParse from 'rehype-parse'
12
- import rehypeStringify from 'rehype-stringify'
13
9
import { toHast } from 'mdast-util-to-hast'
14
10
import { toString } from 'mdast-util-to-string'
11
+ import { fromHtml } from 'hast-util-from-html'
15
12
import { toHtml } from 'hast-util-to-html'
16
13
import { commonmark } from 'commonmark.json'
17
14
import { fromMarkdown } from '../dev/index.js'
18
15
19
- const join = path . join
20
-
21
16
test ( 'mdast-util-from-markdown' , ( t ) => {
22
17
t . equal ( typeof fromMarkdown , 'function' , 'should expose a function' )
23
18
@@ -1040,26 +1035,32 @@ test('mdast-util-from-markdown', (t) => {
1040
1035
t . end ( )
1041
1036
} )
1042
1037
1043
- test ( 'fixtures' , ( t ) => {
1044
- const base = join ( 'test' , 'fixtures' )
1045
- const files = fs . readdirSync ( base ) . filter ( ( d ) => path . extname ( d ) === '.md' )
1038
+ test ( 'fixtures' , async ( t ) => {
1039
+ const base = new URL ( 'fixtures/' , import . meta. url )
1040
+
1041
+ const files = await fs . readdir ( base )
1046
1042
let index = - 1
1047
1043
1048
1044
while ( ++ index < files . length ) {
1049
1045
const file = files [ index ]
1050
- const stem = path . basename ( file , path . extname ( file ) )
1051
- const fp = join ( base , stem + '.json' )
1052
- const doc = fs . readFileSync ( join ( base , stem + '.md' ) )
1046
+
1047
+ if ( ! / \. m d $ / i. test ( file ) ) {
1048
+ continue
1049
+ }
1050
+
1051
+ const stem = file . split ( '.' ) . slice ( 0 , - 1 ) . join ( '.' )
1052
+ const fp = new URL ( stem + '.json' , base )
1053
+ const doc = await fs . readFile ( new URL ( file , base ) )
1053
1054
const actual = fromMarkdown ( doc )
1054
1055
/** @type {Root } */
1055
1056
let expected
1056
1057
1057
1058
try {
1058
- expected = JSON . parse ( String ( fs . readFileSync ( fp ) ) )
1059
+ expected = JSON . parse ( String ( await fs . readFile ( fp ) ) )
1059
1060
} catch {
1060
1061
// New fixture.
1061
1062
expected = actual
1062
- fs . writeFileSync ( fp , JSON . stringify ( actual , null , 2 ) + '\n' )
1063
+ await fs . writeFile ( fp , JSON . stringify ( actual , null , 2 ) + '\n' )
1063
1064
}
1064
1065
1065
1066
t . deepEqual ( actual , expected , stem )
@@ -1070,6 +1071,8 @@ test('fixtures', (t) => {
1070
1071
1071
1072
test ( 'commonmark' , ( t ) => {
1072
1073
let index = - 1
1074
+
1075
+ // To do: update micromark.
1073
1076
// Changes in living version of CommonMark.
1074
1077
const skip = new Set ( [ 623 , 624 ] )
1075
1078
@@ -1079,23 +1082,19 @@ test('commonmark', (t) => {
1079
1082
}
1080
1083
1081
1084
const example = commonmark [ index ]
1082
- const root = fromMarkdown ( example . markdown . slice ( 0 , - 1 ) )
1083
- const hast = toHast ( root , { allowDangerousHtml : true } )
1084
- assert ( hast && hast . type === 'root' , 'expected `root`' )
1085
- const html = toHtml ( hast , {
1086
- allowDangerousHtml : true ,
1087
- entities : { useNamedReferences : true } ,
1088
- closeSelfClosing : true
1089
- } )
1085
+ const input = example . markdown . slice ( 0 , - 1 )
1086
+ const output = example . html . slice ( 0 , - 1 )
1090
1087
1091
- const reformat = unified ( )
1092
- . use ( rehypeParse , { fragment : true } )
1093
- . use ( rehypeStringify )
1094
-
1095
- const actual = reformat . processSync ( html ) . toString ( )
1096
- const expected = reformat . processSync ( example . html . slice ( 0 , - 1 ) ) . toString ( )
1088
+ const mdast = fromMarkdown ( input )
1089
+ const hast = toHast ( mdast , { allowDangerousHtml : true } )
1090
+ assert ( hast && hast . type === 'root' , 'expected `root`' )
1091
+ const actual = toHtml ( hast , { allowDangerousHtml : true } )
1097
1092
1098
- t . deepLooseEqual ( actual , expected , example . section + ' (' + index + ')' )
1093
+ t . deepLooseEqual (
1094
+ toHtml ( fromHtml ( actual , { fragment : true } ) ) ,
1095
+ toHtml ( fromHtml ( output , { fragment : true } ) ) ,
1096
+ example . section + ' (' + index + ')'
1097
+ )
1099
1098
}
1100
1099
1101
1100
t . end ( )
0 commit comments