Files
materials/knowledge/kubernetes/tools/check-pages.js

166 lines
8.3 KiB
JavaScript
Raw Permalink 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) {
// 附录页(appendix-*.html、index.html 等)不是章节,13 模块与字数/图/折叠那套规则不适用。
const isChapterLike = /^\d\d-.*\.html$/.test(file);
if (!isChapterLike) {
console.log(` · 非章节页(不在 chapters.json),跳过章节规则检查`);
continue;
}
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);
const minBash = (meta && meta.minBash) || 8;
if (bashBlocks < minBash) bad(file, `命令块 ${bashBlocks} 段,本章要求 ≥${minBash}`);
/* --- 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, " ")
.replace(/<aside[\s\S]*?<\/aside>/g, " ")
.replace(/<nav class="chapnav"[\s\S]*?<\/nav>/g, " ")
.replace(/<nav class="pager"[\s\S]*?<\/nav>/g, " ")
.replace(/<p class="foot"[\s\S]*?<\/p>/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. 常量表 --- */
/* 只查「自己造的资源落在哪个 ns」:系统 ns 与 default 允许出现(教学里要对照) */
const nsOk = ["shop", "platform", "kube-system", "kube-public", "kube-node-lease", "default"];
const nsBad = [...html.matchAll(/--namespace[= ]+([a-z0-9][a-z0-9-]*)/g)].map(m => m[1])
.filter(n => !nsOk.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);