forked from modelcontextprotocol/modelcontextprotocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-mdx-comments.ts
More file actions
executable file
·66 lines (53 loc) · 1.86 KB
/
check-mdx-comments.ts
File metadata and controls
executable file
·66 lines (53 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env tsx
// Checks for JS comments inside MDX ESM blocks (imports / exports) because they
// break Mintlify's production parser even though they work locally.
//
// Uses remark-parse + remark-mdx (the same parser MDX uses) to reliably detect
// comments.
import { readFile } from "fs/promises";
import { glob } from "glob";
import { dirname, join } from "path";
import remarkMdx from "remark-mdx";
import remarkParse from "remark-parse";
import { unified } from "unified";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
async function main() {
const mdxFiles = await glob("docs/**/*.mdx", { cwd: join(__dirname, "..") });
process.stdout.write(
`Checking ${mdxFiles.length} MDX files for JS comments in ESM blocks... `
);
const parser = unified().use(remarkParse).use(remarkMdx);
const promises = mdxFiles.map(async (file) => {
const content = await readFile(join(__dirname, "..", file), "utf8");
let tree;
try {
tree = parser.parse(content);
} catch {
return []; // Parse error -- let other checks catch it
}
const locations: string[] = [];
for (const node of tree.children) {
if (node.type === "mdxjsEsm") {
const comments = node.data?.estree?.comments || [];
for (const comment of comments) {
const line = comment.loc?.start?.line;
locations.push(line ? `${file}:${line}` : file);
}
}
}
return locations;
});
const commentLocations = (await Promise.all(promises)).flat();
if (commentLocations.length > 0) {
console.error("\nError: JS comments found in MDX ESM blocks:\n");
for (const loc of commentLocations) {
console.error(`- ${loc}`);
}
console.error("\nJS comments break Mintlify's production MDX parser.");
process.exit(1);
} else {
console.log("OK");
}
}
main();