/** 从同一教程内容源生成仓库README和带编号箭头的SVG截图;--check只验证,不修改文件。 */
import { readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const docRoot = path.join(root, "docs/experiments");
const guide = JSON.parse(
await readFile(path.join(docRoot, "guide.json"), "utf8")
);
const shots = JSON.parse(
await readFile(path.join(docRoot, "screenshots.json"), "utf8")
);
const check = process.argv.includes("--check");
const escape = value =>
String(value).replace(
/[&<>"']/g,
c =>
({
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
})[c]
);
/** 派生文件必须与共享源一致,防止网页教程、标注和README各自漂移。 */
async function output(file, content) {
if (check) {
if ((await readFile(file, "utf8")) !== content)
throw new Error(`派生文档需要重新生成:${path.relative(root, file)}`);
} else await writeFile(file, content);
}
let readme = `# ${guide.title}\n\n${guide.intro}\n\n更新时间:${guide.updated}\n\n> 截图由仓库实际页面组件在隔离演示环境拍摄。红色箭头和编号为操作标注;演示调用、账号和统计不是生产验收证据。\n\n## 目录\n\n`;
for (const step of guide.steps) readme += `- ${step.title}\n`;
for (const step of guide.steps) {
readme += `\n## ${step.title}\n\n${step.lead}\n`;
for (const image of step.images ?? []) {
const shot = shots[image];
if (!shot || shot.annotations.length < 1)
throw new Error(`缺少截图标注:${image}`);
if (guide.captions[image]?.length !== shot.annotations.length)
throw new Error(`标注编号与说明不匹配:${image}`);
const raw = await readFile(
path.join(docRoot, "screenshots", `${image}.jpg`)
);
if (raw[0] !== 255 || raw[1] !== 216)
throw new Error(`截图不是JPEG:${image}`);
const { width, height } = shot;
if (
!Number.isInteger(width) ||
!Number.isInteger(height) ||
width < 1 ||
height < 1
)
throw new Error("截图尺寸无效");
const annotations = shot.annotations
.map(a => {
const { x, y, width: w, height: h } = a.rect,
{ x: bx, y: by } = a.badge;
for (const n of [x, y, w, h, bx, by])
if (!Number.isFinite(n)) throw new Error("标注坐标无效");
if (
w <= 0 ||
h <= 0 ||
x < 0 ||
y < 0 ||
x + w > width + 1 ||
y + h > height + 1 ||
bx < 20 ||
bx > width - 20 ||
by < 20 ||
by > height - 20
)
throw new Error(`标注超出截图:${image}`);
const targetX = Math.max(x, Math.min(x + w, bx)),
targetY = Math.max(y, Math.min(y + h, by));
return `${a.number}`;
})
.join("");
const svg = `\n`;
await output(
path.join(root, "public/guides/experiments", `${image}.svg`),
svg
);
readme +=
`\n\n\n` +
guide.captions[image].map((note, i) => `${i + 1}. ${note}`).join("\n") +
"\n";
}
readme += "\n" + step.bullets.map(line => `- ${line}`).join("\n") + "\n";
if (step.code) readme += `\n\`\`\`text\n${step.code}\n\`\`\`\n`;
}
readme +=
"\n## 维护教程\n\n- 内容源:`guide.json`;二级教程页直接读取同一内容源。\n- 原始截图:`screenshots/*.jpg`;标注坐标:`screenshots.json`。截图必须使用隔离演示数据。\n- 执行 `node scripts/build-experiment-guide.mjs` 重新生成本README和带箭头、编号的SVG。\n- 执行 `node scripts/build-experiment-guide.mjs --check` 检查派生文件与内容源一致。\n- 二级页面:`/digital/experiments/guide`;点击“操作指南”进入。\n";
await output(path.join(docRoot, "README.md"), readme);
console.log(
check ? "图文教程与标注一致性检查通过" : "已生成图文README与标注截图"
);