@@ -4,6 +4,7 @@ const path = require('path');
44const fs = require ( 'fs' ) ;
55const yaml = require ( 'yaml' ) ;
66const cmark = require ( 'cmark-gfm' ) ;
7+ const mdx = require ( '@mdx-js/mdx' ) ;
78const mkdirp = require ( 'mkdirp' ) ;
89const jsdom = require ( 'jsdom' ) ;
910const npm = require ( '../lib/npm.js' )
@@ -16,25 +17,35 @@ const outputRoot = path.join(docsRoot, 'output');
1617
1718const template = fs . readFileSync ( 'template.html' ) . toString ( ) ;
1819
19- walk ( inputRoot ) ;
20+ const run = async function ( ) {
21+ try {
22+ await walk ( inputRoot ) ;
23+ }
24+ catch ( error ) {
25+ console . error ( error ) ;
26+ }
27+ }
2028
21- function walk ( root , dirRelative ) {
29+ run ( ) ;
30+
31+ async function walk ( root , dirRelative ) {
2232 const dirPath = dirRelative ? path . join ( root , dirRelative ) : root ;
33+ const children = fs . readdirSync ( dirPath ) ;
2334
24- fs . readdirSync ( dirPath ) . forEach ( ( childFilename ) => {
35+ for ( const childFilename of children ) {
2536 const childRelative = dirRelative ? path . join ( dirRelative , childFilename ) : childFilename ;
2637 const childPath = path . join ( root , childRelative ) ;
2738
2839 if ( fs . lstatSync ( childPath ) . isDirectory ( ) ) {
29- walk ( root , childRelative ) ;
40+ await walk ( root , childRelative ) ;
3041 }
3142 else {
32- translate ( childRelative ) ;
43+ await translate ( childRelative ) ;
3344 }
34- } ) ;
45+ }
3546}
3647
37- function translate ( childPath ) {
48+ async function translate ( childPath ) {
3849 const inputPath = path . join ( inputRoot , childPath ) ;
3950
4051 if ( ! inputPath . match ( / \. m d $ / ) ) {
@@ -70,6 +81,16 @@ function translate(childPath) {
7081 }
7182 } ) ;
7283
84+ // Test that mdx can parse this markdown file. We don't actually
85+ // use the output, it's just to ensure that the upstream docs
86+ // site (docs.npmjs.com) can parse it when this file gets there.
87+ try {
88+ await mdx ( md , { skipExport : true } ) ;
89+ }
90+ catch ( error ) {
91+ throw new MarkdownError ( childPath , error ) ;
92+ }
93+
7394 // Inject this data into the template, using a mustache-like
7495 // replacement scheme.
7596 const html = template . replace ( / \{ \{ \s * ( [ \w \. ] + ) \s * \} \} / g, ( token , key ) => {
@@ -225,3 +246,11 @@ function headerLevel(node) {
225246function debug ( str ) {
226247 console . log ( str ) ;
227248}
249+
250+ class MarkdownError extends Error {
251+ constructor ( file , inner ) {
252+ super ( `failed to parse ${ file } ` ) ;
253+ this . file = file ;
254+ this . inner = inner ;
255+ }
256+ }
0 commit comments