Files
materials/knowledge/sre-learning-path/tools/build-nav.js
huanghaosheng 1eea6f4687 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 行尾,忽略沙箱诊断产物
2026-10-08 16:48:05 +08:00

93 lines
4.2 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
/* ==========================================================================
侧栏目录树生成器
用法: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} 个页面。`);