mermaid-simple-validator 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/mermaid-validate.mjs +85 -0
- package/package.json +18 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import { dirname, join } from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
|
|
8
|
+
const DEPS_HELP = 'npm install mermaid-simple-validator';
|
|
9
|
+
|
|
10
|
+
let JSDOM;
|
|
11
|
+
try {
|
|
12
|
+
({ JSDOM } = await import('jsdom'));
|
|
13
|
+
} catch {
|
|
14
|
+
console.error(`Missing dependencies. Run: ${DEPS_HELP}`);
|
|
15
|
+
process.exit(2);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function extractBlocks(markdown) {
|
|
19
|
+
const blocks = [];
|
|
20
|
+
const re = /```mermaid\s*\n([\s\S]*?)```/g;
|
|
21
|
+
let m;
|
|
22
|
+
while ((m = re.exec(markdown)) !== null) {
|
|
23
|
+
const beforeFence = markdown.slice(0, m.index);
|
|
24
|
+
const line = (beforeFence.match(/\n/g) || []).length + 1;
|
|
25
|
+
blocks.push({ line, code: m[1].trim() });
|
|
26
|
+
}
|
|
27
|
+
return blocks;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const file = process.argv[2];
|
|
31
|
+
if (!file || file === '--help' || file === '-h') {
|
|
32
|
+
console.log(`\
|
|
33
|
+
Mermaid syntax validator — checks mermaid blocks inside Markdown files without a browser.
|
|
34
|
+
|
|
35
|
+
Usage: npx mermaid-simple-validate <file.md>
|
|
36
|
+
|
|
37
|
+
<file.md> Path to a Markdown file containing \`\`\`mermaid ... \`\`\` blocks
|
|
38
|
+
|
|
39
|
+
Output:
|
|
40
|
+
- Prints each block's line number and diagram type on success
|
|
41
|
+
- Prints the parse error message on failure
|
|
42
|
+
- Exits with code 1 if any block fails validation`);
|
|
43
|
+
process.exit(file ? 0 : 1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const md = readFileSync(file, 'utf8');
|
|
47
|
+
const blocks = extractBlocks(md);
|
|
48
|
+
|
|
49
|
+
if (blocks.length === 0) {
|
|
50
|
+
console.log('No mermaid blocks found.');
|
|
51
|
+
process.exit(0);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>');
|
|
55
|
+
globalThis.window = dom.window;
|
|
56
|
+
globalThis.document = dom.window.document;
|
|
57
|
+
|
|
58
|
+
let mermaid;
|
|
59
|
+
try {
|
|
60
|
+
const _r = createRequire(join(dirname(fileURLToPath(import.meta.url)), 'x.js'));
|
|
61
|
+
({ default: mermaid } = await import(_r.resolve('mermaid')));
|
|
62
|
+
} catch {
|
|
63
|
+
console.error(`Missing dependencies. Run: ${DEPS_HELP}`);
|
|
64
|
+
process.exit(2);
|
|
65
|
+
}
|
|
66
|
+
mermaid.initialize({ startOnLoad: false, securityLevel: 'loose' });
|
|
67
|
+
|
|
68
|
+
let errors = 0;
|
|
69
|
+
|
|
70
|
+
for (const [i, b] of blocks.entries()) {
|
|
71
|
+
try {
|
|
72
|
+
const r = await mermaid.parse(b.code);
|
|
73
|
+
console.log(` block ${i + 1} (line ${b.line}): valid (${r.diagramType})`);
|
|
74
|
+
} catch (e) {
|
|
75
|
+
errors++;
|
|
76
|
+
console.error(` block ${i + 1} (line ${b.line}): INVALID`);
|
|
77
|
+
console.error(` ${e.message.trim()}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (errors > 0) {
|
|
82
|
+
console.error(`\n${errors} error(s) found.`);
|
|
83
|
+
process.exit(1);
|
|
84
|
+
}
|
|
85
|
+
console.log(`\nAll ${blocks.length} block(s) valid.`);
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mermaid-simple-validator",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Validate mermaid syntax in Markdown files — no browser required",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "lamnguyenx",
|
|
7
|
+
"repository": "lamnguyenx/mermaid-simple-validator",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"bin": {
|
|
10
|
+
"mermaid-simple-validate": "./mermaid-validate.mjs"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"jsdom": "*",
|
|
14
|
+
"mermaid": "*"
|
|
15
|
+
},
|
|
16
|
+
"keywords": ["mermaid", "validate", "lint", "syntax", "cli"],
|
|
17
|
+
"files": ["mermaid-validate.mjs"]
|
|
18
|
+
}
|