init: SRE 学习路径站点(阶段 0-3 详解)与项目资料

站点
- knowledge/sre-learning-path/:离线静态站,无外部依赖、无需构建
- 目录页 index.html 按 7 个阶段组织,阶段 0-3 共 15 个模块已上线详解
- 阶段 0 Linux/网络/编程 3 模块、阶段 1 Nginx/部署/容器/CI-CD 4 模块
- 阶段 2 指标/日志/链路/看板 4 模块、阶段 3 SLO/高可用/容量/混沌 4 模块
- 共 203 个页面(105 章 / 317 小节),每章含场景推演与可复制实操

结构
- 模块目录 modules/<dir>/index.html + NN-<slug>.html 章节页
- 共享资源 assets/doc.css、doc.js(目录滚动高亮、代码块增强、主题切换)
- 静态侧边导航由 tools/build-nav.js 生成,tools/check-pages.js 全站校验

内容生成器 .sre-s23/(站点外,供阶段 4-6 复用)
- SPEC.md 撰写规范、README.md 流程说明
- gen.js 由 content/*.js 生成页面骨架,保证各模块样式一致
- check-pre.js / verify-template.js / fix-esc.js / coverage.js 校验工具

其它
- project/:AI Agent 面试题库、场景题集
- 增加 .gitattributes 统一 LF 行尾,忽略沙箱诊断产物
This commit is contained in:
2026-10-08 16:48:05 +08:00
commit 1eea6f4687
234 changed files with 64653 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
#!/usr/bin/env node
/* ==========================================================================
侧栏目录树生成器
用法:node tools/build-nav.js [站点根目录,默认本文件的上一级]
作用:扫描每个模块目录下的 NN-*.html,采集「章节标题 + 小节(h3)」,
然后重写所有页面侧栏里的 <nav id="chapters">,生成默认全部展开的目录树:
1 为什么 SRE 的第一个战场是 Linux
2 文件系统:路径、inode 与链接
· Linux 的目录职责(FHS)
· inode:文件真正的身份
3 权限模型:从 rwx 到 ACL
…
当前章标为 active(其子导航加 current,供 doc.js 做滚动高亮),
本章小节用页内锚点,其它章的小节链接到「章节页 + 锚点」。
新增 / 删除 / 重排章节后重新执行本脚本即可,幂等可重复运行。
========================================================================== */
const fs = require("fs");
const path = require("path");
const root = process.argv[2] || path.resolve(__dirname, "..");
const MODDIR = path.join(root, "modules");
const esc = s => String(s).replace(/&/g, "&amp;").replace(/</g, "&lt;")
.replace(/>/g, "&gt;").replace(/"/g, "&quot;");
const stripTags = s => s.replace(/<[^>]+>/g, "").replace(/\s+/g, " ").trim();
const decode = s => s.replace(/&lt;/g, "<").replace(/&gt;/g, ">")
.replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&nbsp;/g, " ").replace(/&amp;/g, "&");
const plain = s => decode(stripTags(s));
const stripNo = s => s.replace(/^\s*\d+(?:\.\d+)*\s*[\.、]?\s*/, "").trim();
let pages = 0;
for (const dir of fs.readdirSync(MODDIR, { withFileTypes: true })) {
if (!dir.isDirectory()) continue;
const modPath = path.join(MODDIR, dir.name);
const files = fs.readdirSync(modPath).filter(f => /^\d\d-.*\.html$/.test(f)).sort();
if (!files.length) continue;
/* ---------- 1. 采集每一章的标题与小节 ---------- */
const chapters = files.map(file => {
const html = fs.readFileSync(path.join(modPath, file), "utf8");
const num = parseInt(file.slice(0, 2), 10);
const h2 = html.match(/<h2 id="[^"]*">([\s\S]*?)<\/h2>/);
if (!h2) throw new Error(`${dir.name}/${file}: 找不到 h2 章节标题`);
const title = plain(h2[1].replace(/<span class="n">\d+<\/span>/, ""));
const sections = [...html.matchAll(/<h3 id="([^"]+)">([\s\S]*?)<\/h3>/g)]
.map(m => ({ id: m[1], label: stripNo(plain(m[2])) }));
return { num, file, title, sections };
});
/* ---------- 2. 生成目录树 ---------- */
const buildNav = curNum => chapters.map(c => {
const row = c.num === curNum
? `<span class="toc-a active" aria-current="page"><i class="cn">${c.num}</i>${esc(c.title)}</span>`
: `<a class="toc-a" href="${c.file}"><i class="cn">${c.num}</i>${esc(c.title)}</a>`;
if (!c.sections.length) return row;
const sub = c.sections.map(s => {
const href = c.num === curNum ? `#${s.id}` : `${c.file}#${s.id}`;
return `<a class="toc-a lv3" href="${href}">${esc(s.label)}</a>`;
}).join("");
return row + `<div class="subnav${c.num === curNum ? " current" : ""}">${sub}</div>`;
}).join("");
/* ---------- 3. 写回每个章节页 ---------- */
const NAV_RE = /<nav id="chapters">[\s\S]*?<\/nav>/;
for (const c of chapters) {
const p = path.join(modPath, c.file);
const before = fs.readFileSync(p, "utf8");
if (!NAV_RE.test(before)) throw new Error(`${dir.name}/${c.file}: 未找到 #chapters,无法替换`);
fs.writeFileSync(p, before.replace(NAV_RE, `<nav id="chapters">${buildNav(c.num)}</nav>`), "utf8");
pages++;
}
/* ---------- 4. 写回模块目录页(没有当前章) ---------- */
const idx = path.join(modPath, "index.html");
if (fs.existsSync(idx)) {
const before = fs.readFileSync(idx, "utf8");
if (!NAV_RE.test(before)) throw new Error(`${dir.name}/index.html: 未找到 #chapters,无法替换`);
fs.writeFileSync(idx, before.replace(NAV_RE, `<nav id="chapters">${buildNav(0)}</nav>`), "utf8");
pages++;
}
const secCount = chapters.reduce((n, c) => n + c.sections.length, 0);
console.log(`modules/${dir.name}:${chapters.length} 章 / ${secCount} 个小节已展开`);
}
console.log(`\n共更新 ${pages} 个页面。`);

View File

@@ -0,0 +1,161 @@
#!/usr/bin/env node
/* ==========================================================================
页面自检工具:校验目录页与所有模块章节页
用法:node tools/check-pages.js [站点根目录,默认本文件的上一级]
检查项:
- 每个页面的 title / article / 标签配对
- 代码块是否标注 data-lang、内部 < 是否转义
- 所有站内链接是否存在、锚点是否真实可达
- 章节页是否含章节导航、小节导航、定位条、上一/下一章
- 目录页 routin 表指向的文件是否存在
========================================================================== */
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, "");
function walk(dir, out = []) {
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, e.name);
if (e.isDirectory()) { if (e.name !== "assets" && e.name !== "tools") walk(p, out); }
else if (e.name.endsWith(".html")) out.push(p);
}
return out;
}
const pages = walk(root).filter(p => !path.basename(p).startsWith("."));
must(pages.length > 5, "页面数量异常:" + pages.length);
/* 建立 id 索引,供锚点校验 */
const idIndex = new Map();
for (const p of pages) {
const html = read(p);
const ids = new Set([...html.matchAll(/\sid="([^"]+)"/g)].map(m => m[1]));
// 目录页的阶段锚点由 JS 生成,从 STAGES 数据里补上
if (path.resolve(p) === path.resolve(root, "index.html")) {
[...html.matchAll(/^\s*id:"([^"]+)"/gm)].forEach(m => ids.add(m[1]));
}
idIndex.set(path.resolve(p), ids);
}
for (const p of pages) {
const rel = path.relative(root, p).replace(/\\/g, "/");
const html = read(p);
const markup = html.replace(/<script[\s\S]*?<\/script>/g, ""); // 去掉脚本,避免把 JS 模板当链接
const isRootIndex = rel === "index.html";
must(/<title>[^<]+<\/title>/.test(html), `${rel}: 缺少 title`);
if (!isRootIndex) must(html.includes("<article>"), `${rel}: 缺少 article`);
const pres = markup.match(/<pre[\s\S]*?<\/pre>/g) || [];
pres.forEach((blk, i) => {
if (!/data-lang=/.test(blk)) problems.push(`${rel}: 第 ${i + 1} 个 <pre> 未标注 data-lang`);
const body = blk.replace(/^<pre[^>]*>/, "").replace(/<\/pre>$/, "");
if (/</.test(body)) problems.push(`${rel}: 第 ${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(`${rel}: <${t}> 开合不匹配 ${o}/${c}`);
}
const links = [...markup.matchAll(/href="(?!https?:|mailto:)([^"]+)"/g)].map(m => m[1]);
for (const l of new Set(links)) {
const [file, hash] = l.split("#");
const target = file ? path.resolve(path.dirname(p), file) : path.resolve(p);
if (file && !fs.existsSync(target)) { problems.push(`${rel}: 死链 ${l}`); continue; }
if (hash && idIndex.has(target) && !idIndex.get(target).has(hash)) {
problems.push(`${rel}: 锚点不存在 ${l}`);
}
}
const isChapter = /^modules\/[^/]+\/\d\d-/.test(rel);
if (isChapter) {
must(html.includes('id="chapters"'), `${rel}: 缺少章节目录侧栏 #chapters`);
must(!html.includes('id="sections"'), `${rel}: 侧栏应已合并为单栏,仍存在 #sections`);
must(/<h2 id="/.test(html), `${rel}: 缺少 h2 章节标题`);
must(html.includes('class="chapnav"'), `${rel}: 缺少章节目录定位条`);
must(html.includes('class="pager"'), `${rel}: 缺少上一/下一章导航`);
must(html.includes('href="index.html"'), `${rel}: 缺少返回模块目录链接`);
must(html.includes('href="../../assets/doc.css"'), `${rel}: doc.css 路径不对`);
must(html.includes('src="../../assets/doc.js"'), `${rel}: doc.js 路径不对`);
const h3count = (html.match(/<h3 id="/g) || []).length;
const h3any = (html.match(/<h3[ >]/g) || []).length;
must(h3count === h3any, `${rel}: 有 h3 未补 id(${h3count}/${h3any})`);
// 当前章必须被标记为 active,否则侧栏不知道把小节挂到哪
must(/class="toc-a active"[^>]*aria-current="page"/.test(html), `${rel}: 当前章未标记 active`);
}
if (/^modules\/[^/]+\/index\.html$/.test(rel)) {
must(html.includes('class="modhero"'), `${rel}: 缺少模块头部`);
must(html.includes('class="chaplist"'), `${rel}: 缺少章节目录列表`);
must(html.includes('id="chapters"'), `${rel}: 缺少章节目录侧栏 #chapters`);
must(html.includes('href="../../assets/doc.css"'), `${rel}: doc.css 路径不对`);
}
info.push(`${rel} ~${stripTags(html).replace(/\s+/g, "").length} 字 · 代码块 ${pres.length}`);
}
/* 侧栏目录树一致性:同一模块的每个页面都必须列出全部章节与全部小节 */
const modDirs = new Map();
for (const p of pages) {
const rel = path.relative(root, p).replace(/\\/g, "/");
const mm = rel.match(/^(modules\/[^/]+)\//);
if (!mm) continue;
if (!modDirs.has(mm[1])) modDirs.set(mm[1], []);
modDirs.get(mm[1]).push(p);
}
for (const [mod, list] of modDirs) {
const chapterFiles = fs.readdirSync(path.join(root, mod))
.filter(f => /^\d\d-.*\.html$/.test(f)).sort();
let expectedSecs = null;
for (const p of list) {
const rel = path.relative(root, p).replace(/\\/g, "/");
const nav = read(p).match(/<nav id="chapters">([\s\S]*?)<\/nav>/);
must(!!nav, `${rel}: 缺少 #chapters 目录树`);
if (!nav) continue;
// 每个章节文件都应出现在导航里(当前章是 span,其余是链接)
for (const f of chapterFiles) {
const self = rel.endsWith("/" + f);
if (self) continue;
must(nav[1].includes(`href="${f}"`), `${rel}: 目录树缺少章节 ${f}`);
}
const secs = (nav[1].match(/class="toc-a lv3"/g) || []).length;
const chapRows = (nav[1].match(/class="toc-a( active)?"/g) || []).length;
must(chapRows === chapterFiles.length, `${rel}: 目录树章节数 ${chapRows} ≠ ${chapterFiles.length}`);
if (expectedSecs === null) expectedSecs = secs;
else must(secs === expectedSecs, `${rel}: 目录树小节数 ${secs} 与本模块其它页面(${expectedSecs})不一致`);
}
info.push(`${mod}:目录树 ${chapterFiles.length} 章 / ${expectedSecs} 小节,已同步到 ${list.length} 个页面`);
}
/* 目录页路由 */
const index = read(path.join(root, "index.html"));
const m = index.match(/<script>([\s\S]*?)<\/script>\s*<\/body>/);
must(!!m, "index.html: 找不到主 script");
if (m) {
try { new Function(m[1]); } catch (e) { problems.push("index.html 脚本语法错误:" + e.message); }
const cut = m[1].indexOf("/* ============================ 工具");
const d = new Function(m[1].slice(0, cut) + "\nreturn {STAGES, PAGES};")();
for (const [k, v] of Object.entries(d.PAGES)) {
must(fs.existsSync(path.join(root, v)), `路由 ${k} 目标不存在:${v}`);
}
info.push(`index.html 路由:${Object.keys(d.PAGES).length} 条,阶段 ${d.STAGES.length} 个`);
}
info.sort();
console.log(info.join("\n"));
if (problems.length) {
console.error("\n发现 " + problems.length + " 个问题:");
problems.slice(0, 50).forEach(x => console.error(" - " + x));
process.exit(1);
}
console.log(`\nOK 全部校验通过(共 ${pages.length} 个页面)`);