#!/usr/bin/env node /* ========================================================================== Kubernetes 专题 · 页面自检(与其它专题同一套校验思路) 用法:node tools/check-pages.js [章节文件…] 不带参数 = 检查 tools/chapters.json 里所有已存在的章节页 只做机器能判定的部分(结构 / 链接 / 转义 / 篇幅 / 组件数量)。 「讲得对不对」「生产对照是否可信」由人检,见 06-最终契约 §9 的标人检项。 ========================================================================== */ const fs = require("fs"); const path = require("path"); const root = path.resolve(__dirname, ".."); const man = JSON.parse(fs.readFileSync(path.join(root, "tools/chapters.json"), "utf8")); const byFile = new Map(man.chapters.map(c => [c.file, c])); const args = process.argv.slice(2).filter(a => !a.startsWith("-")); const targets = args.length ? args.map(a => path.basename(a)) : man.chapters.map(c => c.file).filter(f => fs.existsSync(path.join(root, f))); if (!targets.length) { console.log("没有可检查的章节页(tools/chapters.json 里的文件都还不存在)"); process.exit(0); } const strip = h => h .replace(//g, " ") .replace(//g, " ") .replace(//g, " ") .replace(//g, " ") .replace(/<[^>]+>/g, " "); const cjk = s => (s.match(/[\u4e00-\u9fa5]/g) || []).length; const count = (h, re) => (h.match(re) || []).length; let fail = 0, warn = 0; const bad = (f, msg) => { fail++; console.log(` ✘ [${f}] ${msg}`); }; const soft = (f, msg) => { warn++; console.log(` · [${f}] ${msg}`); }; for (const file of targets) { const p = path.join(root, file); if (!fs.existsSync(p)) { bad(file, "文件不存在"); continue; } const html = fs.readFileSync(p, "utf8"); const meta = byFile.get(file); console.log(`\n${file}${meta ? ` (第 ${meta.num} 章 · ${meta.stage})` : ""}`); if (!meta) soft(file, "不在 tools/chapters.json 清单里"); /* --- 1. 四个占位块 --- */ for (const [re, name] of [ [/