Files
materials/knowledge/docker/tools/check-pages.js
huanghaosheng 558eb9ae90 feat: 每个资料子页面加回导航页的返回入口;导航页与 Docker/Linux/Nginx/408 站点入库
一、子页面返回入口(本次改动)
- 新增 assets/back.js:在页面顶栏的站点标题之后挂「← 资料库」,点一下回根目录
  index.html 并直接落在本资料的详情路由(如 #/knowledge/docker);注册表之外的目录
  (408/)回首页;没有顶栏的页面(408 的 404)退化为左下角浮动按钮
- 层级由脚本自己的 src 反推(new URL('../', src)),任何目录深度都不必改 back.js;
  样式跟随各站点:描边 / 文字用 currentColor、主色用 var(--brand) / var(--accent)、
  圆角跟随 var(--btn-radius),深浅色主题都自适应,打印时自动隐藏;页面里已有
  index.html#/ 形式的返回链接时(interview/ 占位页)不再重复挂
- 新增 tools/add-back-links.js:按 assets/portal.data.js 反查路由与名称,给每个子页面
  注入 <script src="…assets/back.js" data-portal-back=… data-portal-label=…>。
  先删旧标签再按规则插回,位置写错也能自动纠正;--check 只检查(缺一个即退出码 1)、
  --dry 只预演、写入失败不会中断整轮
- 共注入 454 个页面,3 个有意跳过:根 index.html(它本身就是导航页)、
  interview/index.html(已有「← 返回导航」)、408/ds12-tree/og.html(分享预览图页面)
- 末尾是内联 <script> 的 7 个 index.html:标签插在内联脚本之前,保持
  </script>\s*</body> 的既有形态,各站点自检规则不受影响
- .sre-s23/gen.js 的两处模板同步带上该行,重新生成 SRE 模块页不会丢入口
- README 补「子页面怎么回到导航页」一节,并在新增资料流程里加上这一步

二、此前未入库的内容(一并提交)
- 导航页:index.html + assets/portal.css|portal.data.js|portal.js + tools/check-portal.js
- knowledge/docker(14 章)、knowledge/linux(17 章)、knowledge/nginx(17 章)
- 408/ds12-tree 知识卡片(app.js / styles.css / verify.mjs / favicon / og)
- interview 占位页、README.md

校验
- add-back-links --check:454 / 454 已就绪
- check-portal:3 个模块 / 6 份资料全部命中(无死链)
- check-pages:docker 15 页、linux 18 页、nginx 18 页、sre-learning-path 399 页 全部 OK
- 无头 Chrome 实测点击:docker 章节页 / SRE 模块页 / 场景题库 / 项目题库 / 408 卡片页
  都落到对应资料详情,404 的浮动按钮回导航首页;按钮颜色浅色 rgb(23,33,46) 深色
  rgb(231,237,246) 自适应,408 站点圆角保持 0px
- 受版本管理的 402 个页面每个只 +1 行(gen.js +2 行),没有整文件改写

说明:本机沙箱下 knowledge/sre-learning-path/**、408/、knowledge/scenario、project/、
interview/ 这些文件当时不可写(缺低完整性标记),注入是在一次完全权限授权下完成的。
2026-10-14 22:42:38 +08:00

160 lines
8.3 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
/* ==========================================================================
Docker 专题 · 页面自检
用法:node tools/check-pages.js [站点根目录,默认本文件的上一级]
检查项:
- 结构:title / article / #chapters / doc.css / doc.js / chapnav / pager / foot
- 代码块:是否标注 data-lang、块内 < 是否转义
- 标签配对:div/section/article/aside/nav/main/details/table/ul/ol/li
- 链接:站内文件是否存在、锚点是否真实可达
- 章节:h2 有 id、h3 全部有 id、小节 id 前缀与章号一致
- 目录树:每个页面都列出全部 14 章,当前章展开小节且数量与正文一致
- 体量:章节正文过短会提示(防止只写了标题)
========================================================================== */
const fs = require("fs");
const path = require("path");
const root = process.argv[2] || path.resolve(__dirname, "..");
const problems = [];
const info = [];
const must = (c, m) => { if (!c) problems.push(m); };
const read = p => fs.readFileSync(p, "utf8");
const stripTags = s => s.replace(/<[^>]+>/g, "");
const norm = s => s.replace(/\s+/g, "").length;
const chapterFiles = fs.readdirSync(root).filter(f => /^\d\d-.*\.html$/.test(f)).sort();
must(chapterFiles.length >= 5, "章节页数量异常:" + chapterFiles.length);
const pages = chapterFiles.concat(fs.existsSync(path.join(root, "index.html")) ? ["index.html"] : []);
const totalChapters = chapterFiles.length;
/* id 索引,供锚点校验 */
const idIndex = new Map();
for (const f of pages) {
const html = read(path.join(root, f));
idIndex.set(f, new Set([...html.matchAll(/\sid="([^"]+)"/g)].map(m => m[1])));
}
for (const f of pages) {
const html = read(path.join(root, f));
const markup = html.replace(/<script[\s\S]*?<\/script>/g, "");
const isIndex = f === "index.html";
must(/<title>[^<]+<\/title>/.test(html), `${f}: 缺少 title`);
must(html.includes("<article>"), `${f}: 缺少 <article>`);
must(html.includes('id="chapters"'), `${f}: 缺少侧栏 #chapters`);
must(html.includes('href="assets/doc.css"'), `${f}: doc.css 路径不对`);
must(html.includes('src="assets/doc.js"'), `${f}: doc.js 路径不对`);
must(html.includes('id="themeBtn"'), `${f}: 缺少主题切换按钮`);
must(!/href="\.\.\//.test(markup.replace(/href="\.\.\/sre-learning-path[^"]*"/g, "")),
`${f}: 站内链接不应跑出本资料目录`);
/* 代码块 */
const pres = markup.match(/<pre[\s\S]*?<\/pre>/g) || [];
pres.forEach((blk, i) => {
if (!/data-lang=/.test(blk)) problems.push(`${f}: 第 ${i + 1} 个 <pre> 未标注 data-lang`);
const body = blk.replace(/^<pre[^>]*>/, "").replace(/<\/pre>$/, "");
if (/</.test(body)) problems.push(`${f}: 第 ${i + 1} 个 <pre> 内有未转义的 <`);
});
/* 标签配对 */
for (const t of ["div", "section", "article", "aside", "nav", "main", "details", "table", "ul", "ol", "li"]) {
const o = (markup.match(new RegExp("<" + t + "(?=[\\s>])", "g")) || []).length;
const c = (markup.match(new RegExp("</" + t + ">", "g")) || []).length;
if (o !== c) problems.push(`${f}: <${t}> 开合不匹配 ${o}/${c}`);
}
/* 链接与锚点 */
for (const l of new Set([...markup.matchAll(/href="(?!https?:|mailto:|#)([^"]+)"/g)].map(m => m[1]))) {
const [file, hash] = l.split("#");
const target = path.resolve(root, file || f);
if (file && !fs.existsSync(target)) { problems.push(`${f}: 死链 ${l}`); continue; }
const key = file ? path.basename(file) : f;
if (hash && idIndex.has(key) && !idIndex.get(key).has(hash)) problems.push(`${f}: 锚点不存在 ${l}`);
}
for (const l of new Set([...markup.matchAll(/href="#([^"]+)"/g)].map(m => m[1]))) {
if (!idIndex.get(f).has(l)) problems.push(`${f}: 页内锚点不存在 #${l}`);
}
if (!isIndex) {
const num = parseInt(f.slice(0, 2), 10);
const h2 = html.match(/<h2 id="([^"]+)">([\s\S]*?)<\/h2>/);
/* 内容体量与组件构成 */
const desc = (html.match(/<meta name="description" content="([^"]*)"/) || [, ""])[1];
if (desc.trim().length < 20) problems.push(`${f}: meta description 太短或缺失(${desc.length} 字)`);
if (!/class="box /.test(html)) problems.push(`${f}: 没有任何 .box 提示框`);
if (!/class="tablewrap"/.test(html) && !/class="cards"/.test(html)) {
problems.push(`${f}: 既没有表格也没有卡片组`);
}
if (pres.length < 2) problems.push(`${f}: 代码块过少(${pres.length}),至少 2 个`);
const LANGS = ["bash", "shell", "sh", "console", "text", "yaml", "yml", "json", "dockerfile",
"go", "javascript", "js", "typescript", "ts", "python", "java", "sql", "nginx", "ini",
"toml", "conf", "powershell", "http", "makefile", "dockerignore", "properties", "xml", "csv", "promql"];
for (const m of markup.matchAll(/<pre data-lang="([^"]*)"/g)) {
if (!LANGS.includes(m[1])) problems.push(`${f}: data-lang="${m[1]}" 不在允许列表里`);
}
must(!!h2, `${f}: 缺少 h2 章节标题`);
must(html.includes('class="chapnav"'), `${f}: 缺少章节定位条 .chapnav`);
must(html.includes('class="pager"'), `${f}: 缺少上一/下一章 .pager`);
must(html.includes('class="foot"'), `${f}: 缺少页脚 .foot`);
must(html.includes('href="index.html"'), `${f}: 缺少返回总目录链接`);
must(/class="toc-a active"[^>]*aria-current="page"/.test(html), `${f}: 当前章未标记 active`);
const h3 = [...html.matchAll(/<h3 id="([^"]+)">([\s\S]*?)<\/h3>/g)];
const h3any = (html.match(/<h3[ >]/g) || []).length;
must(h3.length === h3any, `${f}: 有 h3 未补 id(${h3.length}/${h3any})`);
must(h3.length >= 3, `${f}: 小节过少(${h3.length}),至少 3 个`);
for (const m of h3) {
if (!m[1].startsWith(`sec-${num}-`)) problems.push(`${f}: 小节 id "${m[1]}" 应以 sec-${num}- 开头`);
}
/* 上下章导航必须是「上一章 / 下一章」,页脚必须是本章标题 */
const pager = html.match(/<nav class="pager">([\s\S]*?)<\/nav>/);
must(!!pager && /上一章|下一章/.test(pager[1]), `${f}: .pager 不是章节页的上一/下一章导航`);
must(!!pager && !/开始阅读/.test(pager[1]), `${f}: .pager 误用了目录页的「开始阅读」`);
must(/class="foot">\s*Docker 专题 · 第 \d+ 章/.test(html.replace(/\n/g, "")),
`${f}: 页脚不是本章标题(可能是目录页页脚)`);
/* 目录树里当前章的小节数量要与正文一致 */
const sub = html.match(/<div class="subnav current">([\s\S]*?)<\/div>/);
must(!!sub, `${f}: 侧栏缺少当前章小节`);
if (sub) {
const n = (sub[1].match(/class="toc-a lv3"/g) || []).length;
must(n === h3.length, `${f}: 侧栏小节数 ${n} ≠ 正文 h3 数 ${h3.length}`);
}
const body = norm(stripTags(markup));
if (body < 2200) problems.push(`${f}: 正文过短(约 ${body} 字),内容需要补充`);
}
/* 目录树必须列出全部章节 */
const nav = html.match(/<nav id="chapters">([\s\S]*?)<\/nav>/);
must(!!nav, `${f}: 缺少 #chapters 目录树`);
if (isIndex) {
const pg = html.match(/<nav class="pager">([\s\S]*?)<\/nav>/);
must(!!pg && /开始阅读/.test(pg[1]), "index.html: .pager 缺少「开始阅读」入口");
must(!!pg && !/上一章|下一章/.test(pg[1]), "index.html: .pager 误用了章节页的上一/下一章");
must(/class="foot">\s*Docker 专题知识库/.test(html.replace(/\n/g, "")), "index.html: 页脚不是目录页页脚");
}
if (nav) {
const rows = (nav[1].match(/class="toc-a( active)?"/g) || []).length;
must(rows === totalChapters, `${f}: 目录树章节数 ${rows} ≠ ${totalChapters}`);
for (const cf of chapterFiles) {
if (cf === f) continue;
must(nav[1].includes(`href="${cf}"`), `${f}: 目录树缺少章节 ${cf}`);
}
if (isIndex) must(!/class="subnav/.test(nav[1]), "index.html: 目录页不应展开任何章的小节");
}
info.push(`${f.padEnd(24)} ~${norm(stripTags(markup))} 字 · 代码块 ${pres.length}`);
}
info.sort();
console.log(info.join("\n"));
if (problems.length) {
console.error(`\n发现 ${problems.length} 个问题:`);
problems.slice(0, 60).forEach(x => console.error(" - " + x));
process.exit(1);
}
console.log(`\nOK 全部校验通过(${chapterFiles.length} 章 + 目录页,共 ${pages.length} 个页面)`);