必须有 data-lang,且内部 < 必须转义 */
const pres = (c.html || "").match(//g) || [];
pres.forEach((blk, k) => {
if (!/]*data-lang=/.test(blk)) fail(`${tag}: 第 ${k + 1} 个 未标注 data-lang`);
const body = blk.replace(/^]*>/, "").replace(/<\/pre>$/, "");
if (/ 内有未转义的 <`);
});
/* 标签配对 */
for (const t of ["div", "section", "details", "table", "ul", "ol", "li", "p", "span"]) {
const o = ((c.html || "").match(new RegExp("<" + t + "(?=[\\s>])", "g")) || []).length;
const cl = ((c.html || "").match(new RegExp("" + t + ">", "g")) || []).length;
if (o !== cl) fail(`${tag}: <${t}> 开合不匹配 ${o}/${cl}`);
}
/* h3 必须带 id */
const h3all = ((c.html || "").match(/]/g) || []).length;
const h3id = ((c.html || "").match(//g, "")
.replace(/[\s\S]*?<\/code>/g, "").replace(/<[^>]+>/g, "");
if (/'/.test(prose)) fail(`${tag}: 中文正文里出现 ASCII 单引号,请改用「」`);
/* 危险:正文里出现裸 < 会被浏览器吞掉 */
const bare = (c.html || "").replace(//g, "")
.replace(/<[a-zA-Z\/!][^>]*>/g, "");
if (/
${esc(title)}
`;
}
let mod = null;
function pager(prevHref, prevK, prevV, nextHref, nextK, nextV) {
const a = prevHref
? `${prevK}${esc(prevV)}`
: `${prevK}${esc(prevV)}`;
const b = nextHref
? `${nextK}${esc(nextV)}`
: `${nextK}${esc(nextV)}`;
return a + "\n " + b;
}
/* ---------------------------------------------------------------- 生成 */
function generate(m) {
mod = m;
mod.stageAnchor = m.stage.anchor || ("s" + m.stage.num);
const outDir = path.join(SITE, "modules", m.dir);
fs.mkdirSync(outDir, { recursive: true });
const idx = CHAIN.findIndex(x => x[0] === m.dir);
const prevMod = idx > 0 ? CHAIN[idx - 1] : null;
const nextMod = idx >= 0 && idx < CHAIN.length - 1 ? CHAIN[idx + 1] : null;
const nextStage = "s" + (Number(m.stage.num) + 1);
const hasNextStage = Object.prototype.hasOwnProperty.call(STAGE_TITLE, nextStage);
/* ---- 模块目录页 ---- */
const secTotal = m.chapters.reduce((n, c) =>
n + ((c.html.match(/
${secs ? `\n ${secs}
` : ""}
`;
}).join("\n");
mod.crumbTail = `${esc(m.title)}`;
mod.footTail = "模块目录";
const idxPrev = prevMod
? [`../${prevMod[0]}/index.html`, "← 上一模块", prevMod[1]]
: [null, "← 上一模块", "已经是第一个模块"];
const idxNext = nextMod
? [`../${nextMod[0]}/index.html`, "下一模块 →", nextMod[1]]
: hasNextStage
? [`../../index.html#${nextStage}`, "下一模块 →", `阶段 ${nextStage.slice(1)} · ${STAGE_TITLE[nextStage]}`]
: [`../../index.html`, "全部读完 →", "返回学习路径目录"];
const idxBody = head(`${m.title} · 阶段 ${m.stage.num} 模块 ${m.no} · SRE 从零到成熟`,
`${m.title} 模块目录:共 ${m.chapters.length} 章、${secTotal} 个小节。`) +
`
阶段 ${m.stage.num} · 模块 ${m.no} / ${m.total}
${esc(m.title)}
${esc(m.sub)}
共 ${m.chapters.length} 章
${secTotal} 个小节
约 ${kilo} 千字
${reindent(m.lead, " ")}
章节目录
${chaplist}
怎么用这一模块
左侧是章节目录树:先看章标题快速定位,进入某一章后该章的小节会展开并跟随滚动高亮;点其它章标题可直接跳到对应页面。
每章结尾都有「上一章 / 下一章」可以连续阅读,代码块右上角可以一键复制。
` +
`
`;
fs.writeFileSync(path.join(outDir, "index.html"), idxBody, "utf8");
/* ---- 章节页 ---- */
m.chapters.forEach((c, i) => {
const no = i + 1;
const nc = m.chapters[i + 1];
const pc = m.chapters[i - 1];
mod.crumbTail = `${esc(m.title)}/第 ${no} 章`;
mod.footTail = `第 ${no} 章 ${c.title}`;
const pv = pc
? [`${pc.file}.html`, "← 上一章", `第 ${no - 1} 章 · ${pc.title}`]
: ["index.html", "← 模块目录", m.title];
const nx = nc
? [`${nc.file}.html`, "下一章 →", `第 ${no + 1} 章 · ${nc.title}`]
: ["index.html", "回到 →", `${m.title} 章节目录`];
const html = head(`${c.title} · ${m.title} · SRE 从零到成熟`,
`${m.title} 第 ${no} 章:${c.title}。本章 ${(c.html.match(/
${esc(m.title)}
·
第 ${no} / ${m.chapters.length} 章
${no}${esc(c.title)}
${reindent(c.html, " ")}
` +
`