/* 修复模板字面量导致的两类静默损坏(只作用于正文区,即第一个反引号之后): ① 单反斜杠 + 引号 `\"` → `\\"` (本该显示的 \" 被退化成 ") ② 行尾单反斜杠 `\` → `\\` (本该显示的 bash 续行符吞掉了换行,命令被粘成一行) 用法: node .sre-s23/fix-esc.js 只报告(dry run) node .sre-s23/fix-esc.js --write 实际写回 */ const fs = require("fs"); const path = require("path"); const DIR = path.resolve(__dirname, "content"); const dir = process.argv[2]; const write = process.argv.includes("--write"); if (!dir) { console.error("用法:node .sre-s23/fix-esc.js [--write]"); process.exit(1); } const p = path.join(DIR, dir + ".js"); const src = fs.readFileSync(p, "utf8"); const firstTick = src.indexOf("`"); if (firstTick < 0) { console.error("找不到模板字面量起点"); process.exit(1); } const head = src.slice(0, firstTick); const body = src.slice(firstTick); const headBad = (head.match(/(? { const q = (ln.match(/(? console.log(" L" + l)); if (qLines.length > 8) console.log(` … 另 ${qLines.length - 8} 行`); console.log(` ② 行尾单反斜杠 \\ ${contHits} 处`); cLines.slice(0, 8).forEach(l => console.log(" L" + l)); if (cLines.length > 8) console.log(` … 另 ${cLines.length - 8} 行`); if (write && (quoteHits || contHits)) { const fixed = head + body .replace(/(? /(?