#!/usr/bin/env node /* ========================================================================== 单章 / 单模块自检:给「正在写这一章」的人一个即时反馈,不必跑全站自检 用法: node tools/check-chapter.js modules/s1-01-case-design/03-equiv.html node tools/check-chapter.js modules/s1-01-case-design # 整个模块 node tools/check-chapter.js modules/s1-01-case-design/03-equiv.html --strict 参数可以是文件或目录(目录会检查其中所有 NN-*.html),路径相对本工具上一级。 与 tools/check-pages.js 的关系:这里只检查「单个页面自己能负责」的部分 (结构、格式、章号、锚点、链接),侧栏目录树一致性等跨页面项目仍然由 check-pages.js 在跑完 build-nav.js 之后统一检查。 --strict 额外要求:本章所有小节都在侧栏目录树里出现(写完并跑过 build-nav.js 之后用)。 ========================================================================== */ const fs = require("fs"); const path = require("path"); const root = path.resolve(__dirname, ".."); const args = process.argv.slice(2); const STRICT = args.includes("--strict"); const targets = args.filter(a => !a.startsWith("--")); if (!targets.length) { console.error("用法:node tools/check-chapter.js <章节页或模块目录> [--strict]"); process.exit(2); } const ALLOWED_BOX = new Set(["", "note", "tip", "warn", "danger", "ok", "case", "lab", "pit"]); const TAG_PAIRS = ["div", "section", "article", "aside", "nav", "main", "details", "table", "ul", "ol", "li", "thead", "tbody", "tr", "td", "th", "pre", "p", "span"]; const problems = []; const notes = []; function walkHtml(dir, out = []) { for (const e of fs.readdirSync(dir, { withFileTypes: true })) { const p = path.join(dir, e.name); if (e.isDirectory()) { if (e.name !== "assets" && e.name !== "tools") walkHtml(p, out); } else if (e.name.endsWith(".html")) out.push(p); } return out; } /* 站内 id 索引:用于校验锚点是否真实存在 */ const idIndex = new Map(); for (const p of walkHtml(root)) { const html = fs.readFileSync(p, "utf8"); const ids = new Set([...html.matchAll(/\sid="([^"]+)"/g)].map(m => m[1])); if (path.resolve(p) === path.resolve(root, "index.html")) { [...html.matchAll(/^\s*id:"([^"]+)"/gm)].forEach(m => ids.add(m[1])); } idIndex.set(path.resolve(p), ids); } /* 展开待检查文件 */ const files = []; for (const t of targets) { const p = path.resolve(root, t); if (!fs.existsSync(p)) { console.error(`路径不存在:${t}`); process.exit(2); } if (fs.statSync(p).isDirectory()) { const f = fs.readdirSync(p).filter(x => /^\d\d-.*\.html$/.test(x)).sort(); if (!f.length) { console.error(`${t} 下没有 NN-*.html`); process.exit(2); } f.forEach(x => files.push(path.join(p, x))); } else files.push(p); } for (const p of files) { const rel = path.relative(root, p).replace(/\\/g, "/"); const html = fs.readFileSync(p, "utf8"); const markup = html.replace(//g, ""); const must = (c, m) => { if (!c) problems.push(`${rel}: ${m}`); }; if (!/^modules\/[^/]+\/\d\d-/.test(rel)) { notes.push(`${rel}:不是章节页,只做结构检查`); } else { const chapNo = parseInt(path.basename(rel).slice(0, 2), 10); const modPath = path.dirname(p); const modFiles = fs.readdirSync(modPath).filter(f => /^\d\d-.*\.html$/.test(f)).sort(); must(html.includes("
"), "缺少 article"); must(html.includes('id="chapters"'), "缺少章节目录侧栏 #chapters"); must(/

(\d+)<\/b> \/ (\d+) 章/); if (!navPos) problems.push(`${rel}: chapnav 缺少「第 N / M 章」`); else { if (Number(navPos[1]) !== chapNo) problems.push(`${rel}: chapnav 章号 ${navPos[1]} ≠ 文件名 ${chapNo}`); if (Number(navPos[2]) !== modFiles.length) problems.push(`${rel}: chapnav 总章数 ${navPos[2]} ≠ 目录实际 ${modFiles.length} 章`); } const h3all = html.match(/]/g) || []; const h3withId = html.match(/