Skip to content

Commit af4422c

Browse files
ethomsonnlf
authored andcommitted
docs: validate that the docs can be parsed by mdx
Although our documentation is rendered for the "in the box" docs by cmark-gfm, the upstream docs site (docs.npmjs.com) uses mdx, which is a much stricter parser. Update our docs generator site to ensure that mdx can parse our documentation as well, to ensure that we get fast feedback when it would fail. PR-URL: #2711 Credit: @ethomson Close: #2711 Reviewed-by: @darcyclarke
1 parent 38d87e7 commit af4422c

File tree

4 files changed

+2031
-33
lines changed

4 files changed

+2031
-33
lines changed

docs/dockhand.js

+36-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path');
44
const fs = require('fs');
55
const yaml = require('yaml');
66
const cmark = require('cmark-gfm');
7+
const mdx = require('@mdx-js/mdx');
78
const mkdirp = require('mkdirp');
89
const jsdom = require('jsdom');
910
const npm = require('../lib/npm.js')
@@ -16,25 +17,35 @@ const outputRoot = path.join(docsRoot, 'output');
1617

1718
const 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(/\.md$/)) {
@@ -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) {
225246
function 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+
}

node_modules/.gitignore

+83
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)