#!/usr/bin/env node /* ========================================================================== Nginx 专题 · 单章自检(写章节时用这个,比全站 check-pages.js 更聚焦) 用法:node tools/check-chapter.js 03-config.html node tools/check-chapter.js # 不给参数则检查所有 NN-*.html 只校验「作者需要负责」的东西,不校验侧栏/上下章/页脚(那些由 tools/build-nav.js 在收尾时统一生成,中途为空是正常的): - 模板结构:title / meta description / meta keywords / article / 四个占位块 / doc.css / doc.js / 主题按钮 / 不越界链接 - 代码块:data-lang 是否标注且在允许列表内、块内 < 是否转义 - 标签配对:div/section/article/aside/nav/main/details/table/ul/ol/li - 章节:h2 有 id、h3 全部有 id 且以 sec-章号- 开头、小节数 3–10 - 组件:至少 1 个 .box、至少 1 个表格或卡片组、至少 1 处实战落点 - 体量:至少 3 个代码块、正文不少于 2800 字 - 链接:跨章链接指向的文件若尚未生成会标为提示(可忽略),存在则校验锚点 ========================================================================== */ const fs = require("fs"); const path = require("path"); const root = path.resolve(__dirname, ".."); const problems = []; const hints = []; const LANGS = ["bash", "shell", "sh", "console", "text", "yaml", "yml", "json", "dockerfile", "go", "javascript", "js", "typescript", "ts", "python", "java", "sql", "nginx", "ini", "toml", "conf", "lua", "powershell", "http", "makefile", "dockerignore", "properties", "xml", "csv", "promql"]; const stripTags = s => s.replace(/<[^>]+>/g, ""); const norm = s => s.replace(/\s+/g, "").length; const args = process.argv.slice(2); const files = args.length ? args : fs.readdirSync(root).filter(f => /^\d\d-.*\.html$/.test(f)).sort(); if (!files.length) { console.error("没有找到章节文件。用法:node tools/check-chapter.js 03-config.html"); process.exit(1); } /* 已存在章节的 id 索引,用于跨章锚点校验 */ const idIndex = new Map(); for (const f of fs.readdirSync(root).filter(f => /^\d\d-.*\.html$/.test(f)).concat(["index.html"])) { if (!fs.existsSync(path.join(root, f))) continue; idIndex.set(f, new Set([...fs.readFileSync(path.join(root, f), "utf8") .matchAll(/\sid="([^"]+)"/g)].map(m => m[1]))); } for (const f of files) { const p = path.join(root, f); if (!fs.existsSync(p)) { problems.push(`${f}: 文件不存在`); continue; } if (!/^\d\d-.*\.html$/.test(f)) { problems.push(`${f}: 文件名必须是 NN-slug.html`); continue; } const html = fs.readFileSync(p, "utf8"); const markup = html.replace(//g, ""); const num = parseInt(f.slice(0, 2), 10); const own = []; /* ---------------- 结构 ---------------- */ if (!new RegExp(`[^<]*第 ${num} 章[^<]*`).test(html)) own.push("title 里应含「第 N 章」"); if (!/
/.test(html)) own.push("缺少
"); if (!html.includes('id="chapters"')) own.push("缺少侧栏