|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import DOMPurify from 'dompurify'; |
| 4 | +import mermaidModule from 'mermaid'; |
| 5 | + |
| 6 | +const mermaid = mermaidModule.default ?? mermaidModule; |
| 7 | +const DOCS_ROOT = path.resolve('docs'); |
| 8 | +const MARKDOWN_EXTENSIONS = new Set(['.md', '.mdx']); |
| 9 | + |
| 10 | +function isMarkdownFile(filePath) { |
| 11 | + return MARKDOWN_EXTENSIONS.has(path.extname(filePath)); |
| 12 | +} |
| 13 | + |
| 14 | +async function walk(dir) { |
| 15 | + const entries = await fs.readdir(dir, { withFileTypes: true }); |
| 16 | + const results = []; |
| 17 | + for (const entry of entries) { |
| 18 | + const fullPath = path.join(dir, entry.name); |
| 19 | + if (entry.isDirectory()) { |
| 20 | + results.push(...(await walk(fullPath))); |
| 21 | + } else if (entry.isFile() && isMarkdownFile(fullPath)) { |
| 22 | + results.push(fullPath); |
| 23 | + } |
| 24 | + } |
| 25 | + return results; |
| 26 | +} |
| 27 | + |
| 28 | +function extractMermaidBlocks(content) { |
| 29 | + const lines = content.split(/\r?\n/); |
| 30 | + const blocks = []; |
| 31 | + let inBlock = false; |
| 32 | + let blockStartLine = 0; |
| 33 | + let blockLines = []; |
| 34 | + |
| 35 | + for (let i = 0; i < lines.length; i += 1) { |
| 36 | + const line = lines[i]; |
| 37 | + if (!inBlock) { |
| 38 | + if (/^\s*```mermaid\s*$/.test(line)) { |
| 39 | + inBlock = true; |
| 40 | + blockStartLine = i + 2; // first content line, 1-indexed |
| 41 | + blockLines = []; |
| 42 | + } |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + if (/^\s*```\s*$/.test(line)) { |
| 47 | + blocks.push({ |
| 48 | + startLine: blockStartLine, |
| 49 | + content: blockLines.join('\n'), |
| 50 | + }); |
| 51 | + inBlock = false; |
| 52 | + blockLines = []; |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + blockLines.push(line); |
| 57 | + } |
| 58 | + |
| 59 | + if (inBlock) { |
| 60 | + blocks.push({ |
| 61 | + startLine: blockStartLine, |
| 62 | + content: blockLines.join('\n'), |
| 63 | + unterminated: true, |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + return blocks; |
| 68 | +} |
| 69 | + |
| 70 | +function extractRelativeLineFromError(errorMessage) { |
| 71 | + const match = errorMessage.match(/line\s+(\d+)/i); |
| 72 | + return match ? Number(match[1]) : null; |
| 73 | +} |
| 74 | + |
| 75 | +async function lintFile(filePath) { |
| 76 | + const content = await fs.readFile(filePath, 'utf8'); |
| 77 | + const blocks = extractMermaidBlocks(content); |
| 78 | + const errors = []; |
| 79 | + |
| 80 | + for (const block of blocks) { |
| 81 | + if (block.unterminated) { |
| 82 | + errors.push({ |
| 83 | + filePath, |
| 84 | + line: block.startLine, |
| 85 | + message: 'Unterminated mermaid code fence.', |
| 86 | + }); |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if (!block.content.trim()) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + try { |
| 95 | + await mermaid.parse(block.content); |
| 96 | + } catch (error) { |
| 97 | + const message = error instanceof Error ? error.message : String(error); |
| 98 | + const relativeLine = extractRelativeLineFromError(message); |
| 99 | + const absoluteLine = relativeLine ? block.startLine + relativeLine - 1 : block.startLine; |
| 100 | + errors.push({ |
| 101 | + filePath, |
| 102 | + line: absoluteLine, |
| 103 | + message, |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return errors; |
| 109 | +} |
| 110 | + |
| 111 | +async function main() { |
| 112 | + // Mermaid's parser may call DOMPurify hooks for some diagram syntaxes. |
| 113 | + // In Node (without a browser window), dompurify can resolve to a minimal |
| 114 | + // implementation that lacks hook APIs; provide no-op hooks so syntax-only |
| 115 | + // parsing still works in CI. |
| 116 | + if (typeof DOMPurify.addHook !== 'function') { |
| 117 | + DOMPurify.addHook = () => {}; |
| 118 | + } |
| 119 | + if (typeof DOMPurify.removeHook !== 'function') { |
| 120 | + DOMPurify.removeHook = () => {}; |
| 121 | + } |
| 122 | + if (typeof DOMPurify.sanitize !== 'function') { |
| 123 | + DOMPurify.sanitize = (value) => value; |
| 124 | + } |
| 125 | + |
| 126 | + mermaid.initialize({ startOnLoad: false }); |
| 127 | + |
| 128 | + const markdownFiles = await walk(DOCS_ROOT); |
| 129 | + const allErrors = []; |
| 130 | + |
| 131 | + for (const filePath of markdownFiles) { |
| 132 | + const fileErrors = await lintFile(filePath); |
| 133 | + allErrors.push(...fileErrors); |
| 134 | + } |
| 135 | + |
| 136 | + if (allErrors.length > 0) { |
| 137 | + console.error(`Mermaid lint failed with ${allErrors.length} error(s):`); |
| 138 | + for (const err of allErrors) { |
| 139 | + const relPath = path.relative(process.cwd(), err.filePath); |
| 140 | + console.error(`- ${relPath}:${err.line} ${err.message}`); |
| 141 | + } |
| 142 | + process.exit(1); |
| 143 | + } |
| 144 | + |
| 145 | + console.log(`Mermaid lint passed. Checked ${markdownFiles.length} markdown files.`); |
| 146 | +} |
| 147 | + |
| 148 | +await main(); |
0 commit comments