Files
materials/knowledge/kubernetes/tools/check-pages.js
huanghaosheng 9959f566a8 feat(kubernetes): 站点骨架与第 8 章页面,与 knowledge/ 其它专题同款样式
- index.html:知识地图(6 阶段)/ 三条读法 / 26 章卡片 / 8 个高频坑
- 08-storage.html:第 8 章按 tools/SPEC.md 落地,13 小节 2 图 5 表 30 段代码
- assets/:复用 doc.css/doc.js,注入 K8s 蓝,补阶段分组样式
- tools/chapters.json + build-nav.js + check-pages.js + SPEC.md:清单驱动的导航与自检
- 06 §13 换成与真实页面核对过的组件映射;01 §6 站点 IA 改成扁平静态站;05 §0.5 改为三步循环
- portal.data.js 登记知识库入口
2026-11-06 15:52:48 +08:00

150 lines
7.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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(/<!--[\s\S]*?-->/g, " ")
.replace(/<pre[\s\S]*?<\/pre>/g, " ")
.replace(/<script[\s\S]*?<\/script>/g, " ")
.replace(/<style[\s\S]*?<\/style>/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 [
[/<nav id="chapters">[\s\S]*?<\/nav>/, "<nav id=\"chapters\"></nav>"],
[/<p class="chapnav">[\s\S]*?<\/p>/, "<p class=\"chapnav\"></p>"],
[/<nav class="pager">[\s\S]*?<\/nav>/, "<nav class=\"pager\"></nav>"],
[/<p class="foot">[\s\S]*?<\/p>/, "<p class=\"foot\"></p>"]
]) if (!re.test(html)) bad(file, `缺少占位块 ${name}(跑一次 build-nav.js 前必须先有它)`);
/* --- 2. 章标题 / meta --- */
const chm = html.match(/<h2 id="ch(\d+)"><span class="n">(\d+)<\/span>/);
if (!chm) bad(file, `章标题必须是 <h2 id="chN"><span class="n">N</span>标题</h2>`);
else if (meta && Number(chm[1]) !== meta.num) bad(file, `h2 id="ch${chm[1]}" 与清单章号 ${meta.num} 不一致`);
const desc = (html.match(/<meta name="description" content="([^"]*)"/) || [, ""])[1];
const dl = cjk(desc);
if (!desc) bad(file, "缺少 meta description");
else if (dl < 40 || dl > 80) bad(file, `meta description ${dl} 汉字,要求 40–80`);
/* --- 3. h3 的 id 与编号 --- */
const h3s = [...html.matchAll(/<h3([^>]*)>([\s\S]*?)<\/h3>/g)];
if (h3s.length < 8 || h3s.length > 13) bad(file, `小节 ${h3s.length} 个,要求 8–13`);
for (const [, attrs, inner] of h3s) {
const id = (attrs.match(/id="([^"]+)"/) || [, ""])[1];
const label = strip(inner).trim().replace(/\s+/g, " ");
if (!id) bad(file, `小节缺 id:${label.slice(0, 24)}`);
else if (meta && !new RegExp(`^sec-${meta.num}-\\d+$`).test(id)) bad(file, `小节 id「${id}」应为 sec-${meta.num}-N`);
if (meta && /^\d+\.\d+/.test(label) === false) soft(file, `小节标题没带 N.M 编号:${label.slice(0, 24)}`);
}
/* --- 4. 标签平衡 --- */
for (const t of ["div", "article", "section", "aside", "nav", "main", "table", "ul", "ol", "li", "details", "figure", "pre", "p"]) {
const open = count(html, new RegExp(`<${t}(?:\\s|>)`, "g"));
const close = count(html, new RegExp(`</${t}>`, "g"));
if (open !== close) bad(file, `<${t}> 开 ${open} 合 ${close},数量不一致`);
}
/* --- 5. 代码块 --- */
const pres = [...html.matchAll(/<pre([^>]*)>([\s\S]*?)<\/pre>/g)];
for (const [, attrs, body] of pres) {
if (!/data-lang="([a-z]+)"/.test(attrs)) bad(file, "存在没有 data-lang 的 <pre>");
if (/<(?!\/?(code|mark|strong|span|em|b|i|a|br)\b)/.test(body.replace(/&lt;/g, "").replace(/&gt;/g, "")))
bad(file, "代码块里有未转义的尖括号(应写 &lt; / &gt;)");
}
const bashBlocks = count(html, /data-lang="(bash|powershell)"/g);
if (bashBlocks < 8) bad(file, `命令块 ${bashBlocks} 段,06 要求 ≥8`);
/* --- 6. 站内链接 --- */
for (const [, href] of html.matchAll(/href="([^"]+)"/g)) {
if (/^(https?:|mailto:|#)/.test(href)) {
if (href.startsWith("#")) {
const id = href.slice(1);
if (id && !new RegExp(`id="${id}"`).test(html)) bad(file, `指向不存在的锚点 ${href}`);
}
continue;
}
if (/^(assets\/|\.\.\/\.\.\/assets\/)/.test(href)) continue;
const [f0, hash] = href.split("#");
if (!fs.existsSync(path.join(root, f0))) {
if (byFile.has(f0)) soft(file, `链接的章节还没写:${f0}(清单里有,先留链接)`);
else bad(file, `链接指向不存在的文件 ${f0}`);
}
else if (hash) {
const t = fs.readFileSync(path.join(root, f0), "utf8");
if (!new RegExp(`id="${hash}"`).test(t)) bad(file, `链接锚点不存在:${href}`);
}
}
/* --- 7. 篇幅(口径同 06 §1:不含代码块与表格单元格) --- */
const text = strip(html.replace(/<div class="tablewrap">[\s\S]*?<\/div>/g, " "));
const chars = cjk(text);
if (chars < 1800) bad(file, `正文 ${chars} 汉字,06 要求 ≥1800`);
if (chars > 3200) soft(file, `正文 ${chars} 汉字,超过 06 的上限 3200,考虑拆小节或删冗余`);
/* --- 8. 组件数量(对应 06 的度量区间) --- */
const figs = count(html, /<figure class="figure"/g);
const figsWithCap = count(html, /<div class="figcap">/g);
const refs = count(html, /见图\s*\d/g);
if (figs < 2) bad(file, `图 ${figs} 张,要求 2–5`);
if (figs !== figsWithCap) bad(file, `图 ${figs} 张但有图注 ${figsWithCap} 个`);
if (refs < 1) bad(file, "正文没有「见图 N」引用");
if (figs && count(html, /<svg /g) < figs) bad(file, "有 figure 但没有内联 svg(不允许 mermaid / 外链图片)");
const takes = count(html, /<p class="take">/g);
if (takes < 3) bad(file, `「一句话」${takes} 处,要求 3–6`);
const details = count(html, /<details>/g);
if (details < 2) bad(file, `可跳过折叠 ${details} 处,要求 2–4`);
const warns = count(html, /<div class="box warn"/g);
if (warns < 3) bad(file, `易错点(box warn)${warns} 处,要求 3–5`);
const tables = count(html, /<div class="tablewrap">/g);
if (tables < 5) bad(file, `表格 ${tables} 张,要求 ≥5`);
/* --- 9. 常量表 --- */
const nsBad = [...html.matchAll(/--namespace[= ](\S+)/g)].map(m => m[1].replace(/["']/g, ""))
.filter(n => !["shop", "platform", "kube-system"].includes(n));
if (nsBad.length) bad(file, `命名空间越界:${[...new Set(nsBad)].join(", ")}`);
if (!fail) console.log(` ✔ ${pres.length} 段代码 · ${chars} 汉字 · ${figs} 图 · ${tables} 表 · ${takes} 句话 · ${details} 折叠 · ${warns} 易错点`);
}
console.log(`\n${fail ? "✘" : "✔"} ${targets.length} 个页面:${fail} 处不合格${warn ? `,${warn} 处提醒` : ""}`);
process.exit(fail ? 1 : 0);