#!/usr/bin/env node /* ========================================================================== Docker 专题 · 页面骨架生成器 用法:node tools/build-nav.js [站点根目录,默认本文件的上一级] 作用:扫描站点根目录下的 NN-*.html,采集「章标题(h2)+ 小节(h3)」与 每章的 ,然后重写页面里的占位区块: 章节页: 侧栏目录树(当前章展开小节)
「第 N / 14 章」定位条 上一章 / 下一章 页脚 目录页: 全部章节(不展开小节) 章节 / 小节 / 代码示例 / 字数 四块指标 章节卡片(勾选已读 + 展开小节链接) 「开始阅读」 写章节时只写正文,骨架由本脚本统一生成;新增/重排章节后重新执行即可, 幂等、可重复运行。 ========================================================================== */ const fs = require("fs"); const path = require("path"); const root = process.argv[2] || path.resolve(__dirname, ".."); const esc = s => String(s).replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); const stripTags = s => s.replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim(); const decode = s => s.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"') .replace(/'/g, "'").replace(/ /g, " ").replace(/&/g, "&"); const plain = s => decode(stripTags(s)); const stripNo = s => s.replace(/^\s*\d+(?:\.\d+)*\s*[\.、]?\s*/, "").trim(); /* ---------------------------- 1. 采集 ---------------------------- */ const files = fs.readdirSync(root, { withFileTypes: true }) .filter(e => e.isFile() && /^\d\d-.*\.html$/.test(e.name)) .map(e => e.name).sort(); if (!files.length) { console.error("没有找到 NN-*.html 章节页,请检查目录:" + root); process.exit(1); } const chapters = files.map(file => { const html = fs.readFileSync(path.join(root, file), "utf8"); const num = parseInt(file.slice(0, 2), 10); const h2 = html.match(//g, " ");
return {
num, file, title, sections,
desc: plain(metaDesc),
keywords: plain(metaKeys),
pres: (html.match(//g) || []).length,
chars: plain(bodyText).replace(/\s+/g, "").length
};
});
const totalSecs = chapters.reduce((n, c) => n + c.sections.length, 0);
const totalPres = chapters.reduce((n, c) => n + c.pres, 0);
const totalChars = chapters.reduce((n, c) => n + c.chars, 0);
/* ---------------------------- 2. 区块生成 ---------------------------- */
function navTree(curNum) {
return chapters.map(c => {
const isCur = c.num === curNum;
const row = isCur
? `${c.num}${esc(c.title)}`
: `${c.num}${esc(c.title)}`;
if (!isCur || !c.sections.length) return row;
const sub = c.sections.map(s =>
`${esc(s.label)}`).join("");
return row + ``;
}).join("");
}
function chapnav(c) {
return ` Docker 专题\n` +
` ·\n` +
` 第 ${c.num} / ${chapters.length} 章`;
}
function pager(c) {
const i = chapters.indexOf(c);
const a = (ch, cls, k) => ch
? ` ${k}第 ${ch.num} 章 · ${esc(ch.title)}`
: ` ${k}没有更多了`;
return a(chapters[i - 1], "prev", "← 上一章") + "\n" + a(chapters[i + 1], "next", "下一章 →");
}
function foot(c) {
return ` Docker 专题 · 第 ${c.num} 章 ${esc(c.title)}
\n` +
` 总目录 · SRE 学习路径`;
}
function kpis() {
const wan = (totalChars / 10000).toFixed(1);
return [
`${chapters.length}章 · 完整知识体系`,
`${totalSecs}个知识点小节`,
`${totalPres}段可复制代码`,
`${wan} 万字正文`
].join("\n ");
}
function cards() {
return chapters.map(c => {
const search = [c.title, c.desc, c.keywords, c.sections.map(s => s.label).join(" ")]
.join(" ").toLowerCase();
const secs = c.sections.map(s =>
`${esc(s.label)}`).join("");
return ` `;
}).join("\n");
}
/* ---------------------------- 3. 写回 ---------------------------- */
const Z = (re, gen, only) => ({ re, gen, only }); // only: "index" | "chapter" | undefined
const zones = [
Z(/