Files
materials/.sre-s23/check-pre.js
huanghaosheng 84aa0143a8 refactor: 板块 03 改为「SRE 工程师」专属板块,站点迁至 SRE工程师/sre-learning-path/
- 取消「学习路径 / learning-path」模块,新增 id=sre、目录=SRE工程师 的「SRE 工程师」板块:
  统一归档 SRE 相关资料(学习路径 + 排障速查 / SLO 模板 / 值班与复盘模板 / 面试题 / 书单等)
- 学习路线/sre-learning-path/ -> SRE工程师/sre-learning-path/(git mv,404 个文件保留改名历史),
  搬空的 学习路线/ 目录删除
- 路由改为 #/sre/sre-learning-path,返回入口标签改为「SRE 工程师 › SRE 从零到成熟学习路径」
  (399 个页面由 tools/add-back-links.js 重算)
- docker / linux / nginx 的外链、build-nav.js 生成器与自检白名单,.sre-s23 脚手架,
  以及 README / DEPLOY.md(含 nginx 中文目录的百分号编码示例)全部同步到新路径

自检:check-portal(4 个板块 / 6 份资料)、add-back-links --check(454 页)全绿;
SRE 站点 399 页与 docker、linux、nginx 的 check-pages、.sre-s23 四项体检均通过。
2026-10-15 11:55:58 +08:00

76 lines
3.3 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
/* ==========================================================================
代码块内容体检(针对 JS 模板字面量导致的静默损坏)
生成器用 `html: \`...\`` 承载正文,这带来三类静默损坏,HTML 校验查不出来:
① `${...}` 被当成 JS 表达式求值(通常会直接报 xxx is not defined,但也有不报错的)
② `\"` 丢掉反斜杠 → 渲染后只剩 `"`,JSON / curl 片段坏掉
③ 行尾单个 `\` 吞掉换行 → 命令粘在一起
本脚本对生成后的真实页面做检查:
- data-lang="json" 的 <pre> 能否 JSON.parse
- 出现 `\{` `\}` `\"` 等可疑残留
- shell/json 块里 `${` 未转义残留在不该出现的地方(只报告,人工判断)
node .sre-s23/check-pre.js <模块目录名...>
========================================================================== */
const fs = require("fs");
const path = require("path");
const SITE = path.resolve(__dirname, "..", "SRE工程师", "sre-learning-path");
const MODDIR = path.join(SITE, "modules");
const unescape = s => s.replace(/&lt;/g, "<").replace(/&gt;/g, ">")
.replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&nbsp;/g, " ")
.replace(/&mdash;/g, "—").replace(/&amp;/g, "&");
const targets = process.argv.slice(2).length
? process.argv.slice(2)
: fs.readdirSync(MODDIR, { withFileTypes: true })
.filter(d => d.isDirectory()).map(d => d.name).filter(d => /^s[23]-/.test(d)).sort();
let problems = 0, blocks = 0;
const notes = [];
for (const d of targets) {
const dir = path.join(MODDIR, d);
if (!fs.existsSync(dir)) continue;
for (const f of fs.readdirSync(dir).filter(x => x.endsWith(".html")).sort()) {
const rel = `${d}/${f}`;
const html = fs.readFileSync(path.join(dir, f), "utf8");
const pres = [...html.matchAll(/<pre([^>]*)>([\s\S]*?)<\/pre>/g)];
pres.forEach((m, i) => {
blocks++;
const attrs = m[1];
const raw = m[2];
const lang = (attrs.match(/data-lang="([^"]*)"/) || [, ""])[1].toLowerCase();
const name = (attrs.match(/data-name="([^"]*)"/) || [, ""])[1];
const body = unescape(raw).replace(/^\n/, "").replace(/\n$/, "");
/* ① JSON 块必须可解析 */
if (lang === "json") {
try { JSON.parse(body); }
catch (e) {
problems++;
console.log(`✗ ${rel} 第 ${i + 1} 个 <pre>(json${name ? " · " + name : ""})无法解析:${e.message}`);
console.log(` 首行:${body.split("\n")[0].slice(0, 110)}`);
}
}
/* ② 可疑残留:\ 后面跟 { } " 这些不该被转义的字符 */
const sus = [...body.matchAll(/\\[{}"]/g)];
if (sus.length) {
notes.push(`${rel} 第 ${i + 1} 个 <pre>(${lang}${name ? " · " + name : ""})有 ${sus.length} 处 \\{ \\} \\" 残留`);
}
/* ③ 反斜杠 + 行尾:单个 \ 吞换行 */
if (/\\\n/.test(body)) notes.push(`${rel} 第 ${i + 1} 个 <pre> 行尾有单个反斜杠(会吞换行)`);
});
}
}
console.log(`\n共检查 ${blocks} 个代码块`);
if (notes.length) {
console.log(`\n需要人工确认的 ${notes.length} 条:`);
notes.forEach(n => console.log(" · " + n));
}
if (problems) { console.log(`\n${problems} 个 JSON 代码块无法解析`); process.exit(1); }
console.log("\nOK 所有 JSON 代码块均可解析");