Files
Luong NGUYEN 0e0115935d refactor(scripts): replace Mermaid.js with Pele for diagrams (#197)
* refactor(website): render diagrams with pinned pele bundle (#195)

* refactor(ci): validate diagrams with pele and fail when missing (#195)

* refactor(epub): render diagrams as flattened pele svg (#195)

* docs: document pele diagram rendering and node 20 setup (#195)

* test(epub): assert numbered labels reach pele unescaped (#195)

* fix(scripts): address review feedback (#195)

* fix(website): keep pele zoom controls off diagram labels (#195)

Pele (zoom: 'auto') draws an opaque zoom box over the top-right corner
of every diagram it shrinks to fit, which hid labels. When that box is
shown, div.mermaid now gets top padding so the diagram starts below it.
Top room rather than side room: Pele lays diagrams out to the content
width, so side padding would shrink them further and re-lay them out.

Zoom also makes each shrunk diagram a tab stop, but the SVG had no
accessible name. Name every SVG Pele draws after the nearest heading
plus its diagram kind ("Architecture - flowchart"). Pele draws a new
<svg> on resize and font load, so the name is set in onRender. SVGs
named by accTitle keep their own name.

* fix(scripts): load the cached pele bundle as ESM on node 20 (#195)

pele_render.mjs imports Pele's ES module build, dist/pele.js, which was
cached as a bare pele.js with no package.json "type": "module" beside
it. Node.js 20.0-20.18 and 22.0-22.6 have no ESM syntax detection, so
they load that file as CommonJS and crash on `export`, failing
check_mermaid.py and build_epub.py on a supported Node.

Cache dist/pele.js as pele.mjs (the same sha256-pinned file) and hand
that path to the helper, so every Node.js >= 20 loads it as an ES
module. A cache holding only pele.js downloads and verifies pele.mjs
once. A test turns off syntax detection to reproduce the old Node
behaviour on newer Node.
2026-10-19 14:17:01 +02:00

74 lines
2.5 KiB
JavaScript

// Render a batch of Mermaid sources to SVG with a pinned Pele bundle.
//
// Usage: node scripts/pele_render.mjs <absolute path to pele.mjs>
// (Pele's dist/pele.js, cached under a .mjs name so every Node.js >= 20
// loads it as an ES module.)
// stdin: {"diagrams": ["<mermaid source>", ...], "options": {...}}
// stdout: {"results": [{"svg", "type", "width", "height"} | {"error": {...}}]}
//
// Results keep the input order. A diagram that fails to render is reported as
// data ({"error": {"code", "message", "line", "column"}}) and the process still
// exits 0. Exit 2 means a usage error or a Node.js version below 20; any other
// non-zero exit is a crash. Called by scripts/pele_renderer.py.
import { pathToFileURL } from 'node:url';
const MIN_NODE_MAJOR = 20;
const PELE_CODES = new Set(['unsupported-diagram', 'syntax', 'semantic', 'limit']);
function usage(message) {
process.stderr.write(`pele_render: ${message}\n`);
process.exit(2);
}
const nodeMajor = Number(process.versions.node.split('.')[0]);
if (!(nodeMajor >= MIN_NODE_MAJOR)) {
usage(`Node.js >= ${MIN_NODE_MAJOR} is required (found ${process.versions.node})`);
}
if (process.argv.length !== 3) {
usage('expected exactly one argument: the path to pele.mjs');
}
const { render } = await import(pathToFileURL(process.argv[2]).href);
if (typeof render !== 'function') {
usage(`${process.argv[2]} does not export render()`);
}
let input = '';
process.stdin.setEncoding('utf8');
for await (const chunk of process.stdin) {
input += chunk;
}
let request;
try {
request = JSON.parse(input);
} catch (err) {
usage(`stdin is not valid JSON: ${err.message}`);
}
if (!request || !Array.isArray(request.diagrams)) {
usage('stdin must be an object with a "diagrams" array');
}
const options = request.options && typeof request.options === 'object' ? request.options : {};
function toError(err) {
const known = err !== null && typeof err === 'object' && PELE_CODES.has(err.code);
const message = err !== null && typeof err === 'object' && 'message' in err ? err.message : err;
return {
code: known ? err.code : 'internal',
message: String(message),
line: known && Number.isInteger(err.line) ? err.line : null,
column: known && Number.isInteger(err.column) ? err.column : null,
};
}
const results = request.diagrams.map((source) => {
try {
const out = render(String(source), options);
return { svg: out.svg, type: out.type, width: out.width, height: out.height };
} catch (err) {
return { error: toError(err) };
}
});
process.stdout.write(JSON.stringify({ results }));