content(go): 新增 Go 八股知识库(14 章 126 小节 151 段代码),登记门户

This commit is contained in:
2026-10-21 14:11:30 +08:00
parent 1d10065ebd
commit d9629e87a8
173 changed files with 19891 additions and 1 deletions

View File

@@ -0,0 +1,159 @@
#!/usr/bin/env node
/* ==========================================================================
Go 八股 · 页面自检
用法: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工程师\/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*Go 八股 · 第 \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*Go 八股知识库/.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} 个页面)`);