Files
role-admin/scripts/build-experiment-guide.mjs
T
湛兮 c406f8c127 feat(experiments): 发布完整截图教程与分组规则说明
- 操作指南改为带目录和图片放大的二级教程页面,完整覆盖实验生命周期
- 用隔离演示数据拍摄14张实际组件截图,生成编号箭头标注与共享内容README
- 标明稳定分组算法与比例区间,修正比例输入提交时的回显同步
- 同步路由、项目目录和教程资源一致性校验脚本
2026-10-27 16:13:49 +08:00

99 lines
5.1 KiB
JavaScript
Raw Permalink 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.
/** 从同一教程内容源生成仓库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 =>
({
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&apos;"
})[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 `<g><rect x="${x - 4}" y="${y - 4}" width="${w + 8}" height="${h + 8}" rx="7" fill="none" stroke="#ff3b30" stroke-width="4"/><path d="M ${bx} ${by} L ${targetX} ${targetY}" fill="none" stroke="#ff3b30" stroke-width="5" marker-end="url(#arrow)"/><circle cx="${bx}" cy="${by}" r="22" fill="#ff3b30" stroke="white" stroke-width="3"/><text x="${bx}" y="${by + 8}" text-anchor="middle" fill="white" font-family="Arial,sans-serif" font-size="24" font-weight="700">${a.number}</text></g>`;
})
.join("");
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" role="img" aria-labelledby="title desc"><title id="title">${escape(step.title)}</title><desc id="desc">隔离演示界面截图,红色箭头和编号对应教程操作步骤。</desc><defs><marker id="arrow" markerWidth="10" markerHeight="10" refX="9" refY="5" orient="auto" markerUnits="userSpaceOnUse"><path d="M0,0 L10,5 L0,10Z" fill="#ff3b30"/></marker></defs><image width="${width}" height="${height}" href="data:image/jpeg;base64,${raw.toString("base64")}"/>${annotations}</svg>\n`;
await output(
path.join(root, "public/guides/experiments", `${image}.svg`),
svg
);
readme +=
`\n![${step.title}:编号操作截图](../../public/guides/experiments/${image}.svg)\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与标注截图"
);