- index.html:知识地图(6 阶段)/ 三条读法 / 26 章卡片 / 8 个高频坑 - 08-storage.html:第 8 章按 tools/SPEC.md 落地,13 小节 2 图 5 表 30 段代码 - assets/:复用 doc.css/doc.js,注入 K8s 蓝,补阶段分组样式 - tools/chapters.json + build-nav.js + check-pages.js + SPEC.md:清单驱动的导航与自检 - 06 §13 换成与真实页面核对过的组件映射;01 §6 站点 IA 改成扁平静态站;05 §0.5 改为三步循环 - portal.data.js 登记知识库入口
209 lines
9.9 KiB
JavaScript
209 lines
9.9 KiB
JavaScript
#!/usr/bin/env node
|
||
/* ==========================================================================
|
||
Kubernetes 专题 · 页面骨架生成器(与 docker / linux 等专题同一套骨架)
|
||
用法:node tools/build-nav.js [站点根目录,默认本文件的上一级]
|
||
|
||
与其它专题的唯一差别:章节清单来自 tools/chapters.json(26 章 / 6 阶段),
|
||
而不是扫描目录。这样还没写的章也能出现在目录里,标成「待写」,
|
||
写完之后重新跑一次,链接与卡片自动点亮。
|
||
|
||
重写的占位区块:
|
||
章节页:<nav id="chapters"> 侧栏目录树(按阶段分组,当前章展开小节)
|
||
<p class="chapnav"> 「第 N / 26 章」定位条
|
||
<nav class="pager"> 上一章 / 下一章
|
||
<p class="foot"> 页脚
|
||
目录页:<nav id="chapters">、<!-- AUTO:kpis -->、<!-- AUTO:cards -->、
|
||
<nav class="pager">、<p class="foot">
|
||
========================================================================== */
|
||
const fs = require("fs");
|
||
const path = require("path");
|
||
|
||
const root = process.argv[2] || path.resolve(__dirname, "..");
|
||
const man = JSON.parse(fs.readFileSync(path.join(root, "tools/chapters.json"), "utf8"));
|
||
const stages = man.stages;
|
||
const all = man.chapters.slice().sort((a, b) => a.num - b.num);
|
||
|
||
const esc = s => String(s).replace(/&/g, "&").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. 采集 ---------------------------- */
|
||
let done = 0, totalSecs = 0, totalPres = 0, totalChars = 0;
|
||
|
||
const chapters = all.map(c => {
|
||
const p = path.join(root, c.file);
|
||
const exists = fs.existsSync(p);
|
||
const rec = {
|
||
...c, exists, title: c.title, desc: "", keywords: "", sections: [], pres: 0, chars: 0
|
||
};
|
||
if (!exists) return rec;
|
||
|
||
const html = fs.readFileSync(p, "utf8");
|
||
const h2 = html.match(/<h2 id="[^"]*">([\s\S]*?)<\/h2>/);
|
||
if (!h2) throw new Error(`${c.file}: 找不到 <h2 id="...">章标题</h2>`);
|
||
rec.title = plain(h2[1].replace(/<span class="n">[\d.]+<\/span>/, ""));
|
||
|
||
rec.sections = [...html.matchAll(/<h3 id="([^"]+)">([\s\S]*?)<\/h3>/g)]
|
||
.map(m => ({ id: m[1], label: plain(m[2]) }));
|
||
|
||
rec.desc = plain((html.match(/<meta name="description" content="([^"]*)"/) || [, ""])[1]);
|
||
rec.keywords = plain((html.match(/<meta name="keywords" content="([^"]*)"/) || [, ""])[1]);
|
||
|
||
const article = (html.match(/<article>([\s\S]*)<\/article>/) || [, ""])[1];
|
||
const bodyText = article.replace(/<pre[\s\S]*?<\/pre>/g, " ");
|
||
rec.pres = (html.match(/<pre[\s\S]*?<\/pre>/g) || []).length;
|
||
rec.chars = plain(bodyText).replace(/\s+/g, "").length;
|
||
|
||
done++;
|
||
totalSecs += rec.sections.length;
|
||
totalPres += rec.pres;
|
||
totalChars += rec.chars;
|
||
return rec;
|
||
});
|
||
|
||
const firstReady = chapters.find(c => c.exists);
|
||
|
||
/* ---------------------------- 2. 区块生成 ---------------------------- */
|
||
function navTree(curNum) {
|
||
return stages.map(st => {
|
||
const rows = chapters.filter(c => c.stage === st.id).map(c => {
|
||
const label = `<i class="cn">${c.num}</i>${esc(c.title)}`;
|
||
if (!c.exists) return `<span class="toc-a todo" title="待写">${label}</span>`;
|
||
if (c.num === curNum) {
|
||
const sub = c.sections.map(s =>
|
||
`<a class="toc-a lv3" href="#${s.id}">${esc(s.label)}</a>`).join("");
|
||
return `<span class="toc-a active" aria-current="page">${label}</span>` +
|
||
(sub ? `<div class="subnav current">${sub}</div>` : "");
|
||
}
|
||
return `<a class="toc-a" href="${c.file}">${label}</a>`;
|
||
}).join("");
|
||
return `<div class="toc-stage"><b>${st.id}</b><span class="h">${esc(st.title)}</span></div>` + rows;
|
||
}).join("");
|
||
}
|
||
|
||
function chapnav(c) {
|
||
const st = stages.find(s => s.id === c.stage) || { id: "", title: "" };
|
||
return ` <a href="index.html">Kubernetes 专题</a>\n` +
|
||
` <span>·</span>\n` +
|
||
` <span>${st.id} ${esc(st.title)}</span>\n` +
|
||
` <span>·</span>\n` +
|
||
` <span>第 <b class="pos">${c.num}</b> / ${chapters.length} 章</span>`;
|
||
}
|
||
|
||
function pager(c) {
|
||
const i = chapters.indexOf(c);
|
||
const a = (ch, cls, k) => !ch
|
||
? ` <a class="${cls} disabled"><span class="k">${k}</span><span class="v">没有更多了</span></a>`
|
||
: ch.exists
|
||
? ` <a class="${cls}" href="${ch.file}"><span class="k">${k}</span><span class="v">第 ${ch.num} 章 · ${esc(ch.title)}</span></a>`
|
||
: ` <a class="${cls} disabled"><span class="k">${k}</span><span class="v">第 ${ch.num} 章 · 待写</span></a>`;
|
||
return a(chapters[i - 1], "prev", "← 上一章") + "\n" + a(chapters[i + 1], "next", "下一章 →");
|
||
}
|
||
|
||
const homeLink = ` <a href="../../index.html">知识库首页</a>`;
|
||
|
||
function foot(c) {
|
||
return ` Kubernetes 专题 · ${c.stage} 第 ${c.num} 章 ${esc(c.title)}<br>\n` +
|
||
` <a href="index.html">总目录</a> ·${homeLink} · ` +
|
||
`<a href="../../SRE工程师/sre-learning-path/index.html">SRE 学习路径</a>`;
|
||
}
|
||
|
||
function kpis() {
|
||
const wan = (totalChars / 10000).toFixed(1);
|
||
const pct = Math.round(done / chapters.length * 100);
|
||
return [
|
||
`<div class="kpi"><b>${done} / ${chapters.length}</b><span>章已上线 · ${pct}%</span></div>`,
|
||
`<div class="kpi"><b>${totalSecs}</b><span>个知识点小节</span></div>`,
|
||
`<div class="kpi"><b>${totalPres}</b><span>段可复制代码</span></div>`,
|
||
`<div class="kpi"><b>${wan} 万</b><span>字正文</span></div>`
|
||
].join("\n ");
|
||
}
|
||
|
||
function cards() {
|
||
return stages.map(st => {
|
||
const list = chapters.filter(c => c.stage === st.id);
|
||
const head = ` <div class="toc-stage"><b>${st.id}</b><span class="h">${esc(st.title)} · ${st.hours}</span></div>`;
|
||
const rows = list.map(c => {
|
||
const search = [c.title, c.desc, c.keywords, c.sections.map(s => s.label).join(" ")]
|
||
.join(" ").toLowerCase();
|
||
|
||
if (!c.exists) {
|
||
return ` <div class="chapcard todo-card" data-search="${esc(search + " 待写")}">
|
||
<div class="cc-head">
|
||
<span class="cc-no">${c.num}</span>
|
||
<span class="cc-title">${esc(c.title)}</span>
|
||
<span class="cc-go"><span class="chip todo">待写</span></span>
|
||
</div>
|
||
<p class="cc-desc">约 ${c.mins} 分钟 · 章节页尚未生成,按 06-最终契约 产出后即可点亮。</p>
|
||
</div>`;
|
||
}
|
||
|
||
const secs = c.sections.map(s =>
|
||
`<a href="${c.file}#${s.id}">${esc(s.label)}</a>`).join("");
|
||
return ` <div class="chapcard" data-search="${esc(search)}">
|
||
<div class="cc-head">
|
||
<input type="checkbox" data-ch="${c.num}" aria-label="标记第 ${c.num} 章已读">
|
||
<span class="cc-no">${c.num}</span>
|
||
<a class="cc-title" href="${c.file}">${esc(c.title)}</a>
|
||
<a class="cc-go" href="${c.file}">进入 →</a>
|
||
</div>
|
||
<p class="cc-desc">${esc(c.desc || `约 ${c.mins} 分钟。`)}</p>
|
||
<details class="cc-secs">
|
||
<summary>${c.sections.length} 个小节 · 展开看知识点</summary>
|
||
<div>${secs}</div>
|
||
</details>
|
||
</div>`;
|
||
}).join("\n");
|
||
return head + "\n" + rows;
|
||
}).join("\n");
|
||
}
|
||
|
||
/* ---------------------------- 3. 写回 ---------------------------- */
|
||
const Z = (re, gen, only) => ({ re, gen, only });
|
||
|
||
const zones = [
|
||
Z(/<nav id="chapters">[\s\S]*?<\/nav>/, (c, idx) => `<nav id="chapters">${navTree(idx ? -1 : c.num)}</nav>`),
|
||
Z(/<p class="chapnav">[\s\S]*?<\/p>/, c => `<p class="chapnav">\n${chapnav(c)}\n </p>`, "chapter"),
|
||
Z(/<!-- AUTO:kpis -->[\s\S]*?<!-- \/AUTO:kpis -->/, () =>
|
||
`<!-- AUTO:kpis -->\n ${kpis()}\n <!-- /AUTO:kpis -->`, "index"),
|
||
Z(/<!-- AUTO:cards -->[\s\S]*?<!-- \/AUTO:cards -->/, () =>
|
||
`<!-- AUTO:cards -->\n${cards()}\n <!-- /AUTO:cards -->`, "index"),
|
||
Z(/<nav class="pager">[\s\S]*?<\/nav>/, (c, idx) => {
|
||
if (!idx) return `<nav class="pager">\n${pager(c)}\n </nav>`;
|
||
if (!firstReady) return `<nav class="pager">\n <a class="next disabled"><span class="k">即将开始</span><span class="v">第 1 章 · 待写</span></a>\n </nav>`;
|
||
return `<nav class="pager">\n <a class="next" href="${firstReady.file}"><span class="k">开始阅读 →</span><span class="v">第 ${firstReady.num} 章 · ${esc(firstReady.title)}</span></a>\n </nav>`;
|
||
}),
|
||
Z(/<p class="foot">[\s\S]*?<\/p>/, (c, idx) => idx
|
||
? `<p class="foot">\n Kubernetes 专题知识库 · ${done} / ${chapters.length} 章已上线 · ${totalSecs} 个小节<br>\n <a href="index.html">总目录</a> ·${homeLink}\n </p>`
|
||
: `<p class="foot">\n${foot(c)}\n </p>`)
|
||
];
|
||
|
||
let pages = 0;
|
||
const targets = chapters.filter(c => c.exists).map(c => ({ c, file: c.file, idx: false }));
|
||
if (fs.existsSync(path.join(root, "index.html"))) {
|
||
targets.push({ c: chapters[0], file: "index.html", idx: true });
|
||
}
|
||
|
||
for (const t of targets) {
|
||
const p = path.join(root, t.file);
|
||
let html = fs.readFileSync(p, "utf8");
|
||
for (const z of zones) {
|
||
if (z.only === "index" && !t.idx) continue;
|
||
if (z.only === "chapter" && t.idx) continue;
|
||
if (!z.re.test(html)) throw new Error(`${t.file}: 缺少占位区块 ${z.re}`);
|
||
html = html.replace(z.re, z.gen(t.c, t.idx));
|
||
}
|
||
fs.writeFileSync(p, html, "utf8");
|
||
pages++;
|
||
}
|
||
|
||
chapters.forEach(c => console.log(
|
||
` ${c.file.padEnd(20)} ${c.exists ? "✔" : "·"} 第 ${String(c.num).padStart(2)} 章 · ` +
|
||
`${String(c.sections.length).padStart(2)} 小节 · ${String(c.pres).padStart(2)} 段代码 · ` +
|
||
`${String(c.chars).padStart(6)} 字 · ${c.title}`));
|
||
console.log(`\n共 ${done} / ${chapters.length} 章已上线 · ${totalSecs} 小节 · ${totalPres} 段代码 · ` +
|
||
`${(totalChars / 10000).toFixed(1)} 万字,已同步 ${pages} 个页面。`);
|