#!/usr/bin/env node /* ========================================================================== Linux 专题 · 页面骨架生成器 用法:node tools/build-nav.js [站点根目录,默认本文件的上一级] 作用:扫描站点根目录下的 NN-*.html,采集「章标题(h2)+ 小节(h3)」与 每章的 ,然后重写页面里的占位区块: 章节页: 侧栏目录树(当前章展开小节)

「第 N / 17 章」定位条 上一章 / 下一章

页脚 目录页: 全部章节(不展开小节) 章节 / 小节 / 代码示例 / 字数 四块指标 章节卡片(勾选已读 + 展开小节链接) 「开始阅读」

写章节时只写正文,骨架由本脚本统一生成;新增/重排章节后重新执行即可, 幂等、可重复运行。 ========================================================================== */ const fs = require("fs"); const path = require("path"); const root = process.argv[2] || path.resolve(__dirname, ".."); const SITE = "Linux 专题"; const SITE_SUB = "从命令行到生产运维的系统知识库"; 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)); /* ---------------------------- 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(/

([\s\S]*?)<\/h2>/); if (!h2) throw new Error(`${file}: 找不到

章标题

`); const title = plain(h2[1].replace(/\d+<\/span>/, "")); const sections = [...html.matchAll(/

([\s\S]*?)<\/h3>/g)] .map(m => ({ id: m[1], label: plain(m[2]) })); const metaDesc = (html.match(/([\s\S]*)<\/article>/) || [, ""])[1]; // 正文字数不含代码块(代码块单独统计段数) const bodyText = article.replace(//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 ` ${SITE}\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 ` ${SITE} · 第 ${c.num} 章 ${esc(c.title)}
\n` + ` 总目录 · Docker 专题 · ` + `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 `

${esc(c.desc)}

${c.sections.length} 个小节 · 展开看知识点
${secs}
`; }).join("\n"); } /* ---------------------------- 3. 写回 ---------------------------- */ const Z = (re, gen, only) => ({ re, gen, only }); // only: "index" | "chapter" | undefined const zones = [ Z(/