Files
materials/.sre-s23/check-pre.js
huanghaosheng 1eea6f4687 init: SRE 学习路径站点(阶段 0-3 详解)与项目资料
站点
- knowledge/sre-learning-path/:离线静态站,无外部依赖、无需构建
- 目录页 index.html 按 7 个阶段组织,阶段 0-3 共 15 个模块已上线详解
- 阶段 0 Linux/网络/编程 3 模块、阶段 1 Nginx/部署/容器/CI-CD 4 模块
- 阶段 2 指标/日志/链路/看板 4 模块、阶段 3 SLO/高可用/容量/混沌 4 模块
- 共 203 个页面(105 章 / 317 小节),每章含场景推演与可复制实操

结构
- 模块目录 modules/<dir>/index.html + NN-<slug>.html 章节页
- 共享资源 assets/doc.css、doc.js(目录滚动高亮、代码块增强、主题切换)
- 静态侧边导航由 tools/build-nav.js 生成,tools/check-pages.js 全站校验

内容生成器 .sre-s23/(站点外,供阶段 4-6 复用)
- SPEC.md 撰写规范、README.md 流程说明
- gen.js 由 content/*.js 生成页面骨架,保证各模块样式一致
- check-pre.js / verify-template.js / fix-esc.js / coverage.js 校验工具

其它
- project/:AI Agent 面试题库、场景题集
- 增加 .gitattributes 统一 LF 行尾,忽略沙箱诊断产物
2026-10-08 16:48:05 +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, "..", "knowledge", "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 代码块均可解析");