2026-10-08 16:48:05 +08:00
|
|
|
|
#!/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");
|
|
|
|
|
|
|
2026-10-15 11:55:58 +08:00
|
|
|
|
const SITE = path.resolve(__dirname, "..", "SRE工程师", "sre-learning-path");
|
2026-10-08 16:48:05 +08:00
|
|
|
|
const MODDIR = path.join(SITE, "modules");
|
|
|
|
|
|
|
|
|
|
|
|
const unescape = s => s.replace(/</g, "<").replace(/>/g, ">")
|
|
|
|
|
|
.replace(/"/g, '"').replace(/'/g, "'").replace(/ /g, " ")
|
|
|
|
|
|
.replace(/—/g, "—").replace(/&/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 代码块均可解析");
|