mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
The docs site's 18 mermaid blocks are now drawn as SVGs before Hugo
builds the site, by docs/render-diagrams.mjs with pele 1.13.0, and
inlined by a render hook, so no page loads Mermaid's 5.5 MB script.
- docs/render-diagrams.mjs: finds each mermaid block in docs/content,
draws it with pele in Departure Mono at 14px (a measurer from the
font's metrics), and writes <key>.svg and <key>.json (its size) to
the git-ignored docs/.build/diagrams. pele draws edge labels,
messages and notes at round(14 * 0.875) = 12px with no option, so the
measurer gives that text its 14px width and the script rewrites
font-size="12" to 14 on each small-text group (pele-edge-labels,
-notes, -boxes, -frames); a guard fails on any such group without
font-size="12", or small text outside them, and a self-check, run
whenever there's something to draw, renders an edge label, a message
and a note and fails unless pele measured them at 12 and wrote their
group with font-size="12". It also fails on a diagram without
accTitle and accDescr, or on an indented block. A newline after each
</text>, outside any text element, lets the search index see labels
as separate words; screenshots of all 18 are byte-identical with and
without it. (Newlines between a label's tspans would move centered
lines, so a label's own lines stay joined in the index.)
- Caching: a block's key is sha256(salt + "\n" + text)[:16], the salt
being sha256("pele <version>\n" + the script's text)[:16], written to
diagrams/salt. The script keeps every drawing already there, draws
only missing ones (written to .tmp and renamed), prunes files no key
names, and touches the folder. docs:diagrams has sources (the pages,
the script, package.json, the lock) and outputs (the folder), so mise
skips an unchanged run, and runs npm ci only when the lock's SHA-256
differs from docs/node_modules/.devvm-lock-sha256. An unchanged run:
0.45-0.75 s before, 0.02 s now (0.09 s forced); one diagram changed:
0.09 s. CI's check job caches ~/.npm keyed on the lock; the Docs
workflow, which publishes, keeps no cache.
- docs/package.json and package-lock.json pin pele 1.13.0
(sha512-Ie8UKY9x00MsuR3jO6AzUwzK2Z6MIV3Tf4EDdITiuk8Uaz8K7g0R082WqsqNaSU8DdgkrRvZThzhit83mi/uiw==).
- docs/layouts/_markup/render-codeblock-mermaid.html: in place of
Hextra's hook, reads the salt, computes the key from it and .Inner,
inlines the SVG from assets/diagrams (hugo.yaml mounts .build/diagrams
there), sets the wrapper's contain-intrinsic-block-size to the
diagram's height and --devvm-diagram-width to its width, and errorf's
when a drawing or the salt is missing. layouts/_partials/scripts/
mermaid.html and params.mermaid go.
- custom.css: .devvm-diagram with content-visibility: auto, and pele's
--pele-* variables from the palette (subgraphs, lifelines and notes in
the border color). A diagram wider than the text reaches into the
article's right padding (1.5rem, 3rem from 768px) as far as it needs,
then into the left padding for the rest, so it never covers the table
of contents; wider than both, it scrolls. From about 1010px no diagram
scrolls, the four wide ones included (proxy checks 751px, agent store,
VM states, SSH agent); at 900px five scroll. With the table of
contents shown, the proxy checks take 48px on the right and 31px on
the left.
- mise.toml: docs:diagrams, which docs and lint:docs depend on.
docs.yml: the next and release jobs install node and draw the
diagrams; the release job only when the tag has the script, so older
tags still build.
- scripts/bump.sh: bump_pele, named only, replaces bump_mermaid: npm
install --save-exact --ignore-scripts in docs, then mise run
docs:diagrams, whose self-check catches a pele that changed how it
draws small text. TestBumpCoversPins finds docs/package.json's
dependencies (npm:NAME); bump.txtar tests --check and the install.
The header and hextra's note say the mermaid render hook replaces
Hextra's mermaid hook, so check it still has that name.
- Every diagram gets an accTitle and accDescr, which pele makes the
SVG's title and desc (aria-labelledby, aria-describedby). Staying
safe's columns are written in reading order, as pele lays subgraphs
out from left to right. The proxy checks' description names every
branch, and a hand-placed <br> keeps "CA)" off a line of its own.
- AGENTS.md: the one Node script; no other build script is JavaScript,
and no other npm package is installed outside mise's tools.
Contributing's docs, tools and
maintaining pages describe the drawing, the caching, the margin, the
task and the bump.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
189 lines
8.8 KiB
JavaScript
189 lines
8.8 KiB
JavaScript
// Draws every mermaid code block in docs/content as an SVG, with pele (docs/package.json), before Hugo
|
|
// builds the site, so its pages load no diagram JavaScript. Each block's files go in
|
|
// docs/.build/diagrams/, which git ignores, named for its key: <key>.svg, and <key>.json with its
|
|
// width and height. The render hook (layouts/_markup/render-codeblock-mermaid.html) computes the same
|
|
// key and inlines the SVG.
|
|
//
|
|
// A key is a hash of the block's text and of the salt, which is itself a hash of pele's version and
|
|
// this script's text, its render settings included; the salt is written to the folder's salt file for
|
|
// the hook. A drawing whose files are already there is kept, only the missing ones are drawn, and
|
|
// files no block's key names are deleted, so a run with nothing changed draws nothing, and a new pele
|
|
// or a change here draws them all again.
|
|
//
|
|
// The theme is CSS: custom.css sets pele's --pele-* variables. The layout is set here: Departure Mono
|
|
// at 14px, measured from the font's metrics, and the Mermaid settings below.
|
|
//
|
|
// Run it with mise run docs:diagrams, which installs pele first (npm ci).
|
|
import { createHash } from "node:crypto";
|
|
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, utimesSync, writeFileSync } from "node:fs";
|
|
import { dirname, join, relative } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { render } from "pele";
|
|
|
|
const script = fileURLToPath(import.meta.url);
|
|
const docs = dirname(script);
|
|
const out = join(docs, ".build", "diagrams");
|
|
const SALT_FILE = "salt";
|
|
|
|
// Departure Mono: 550 units to the em and every glyph 350 wide, so a character's width is its size
|
|
// times 350/550, and the measurer counts characters.
|
|
const FONT = '"Departure Mono", monospace';
|
|
const ADVANCE = 350 / 550;
|
|
const SIZE = 14;
|
|
// pele draws edge labels, sequence messages, notes and boxes smaller than the rest, at
|
|
// round(size * 0.875), 12px, with no option to change it. The measurer gives that text the width it
|
|
// has at 14px, so pele lays it out for 14px, and grow() then draws it at 14px.
|
|
const SMALL = Math.round(SIZE * 0.875);
|
|
const measured = [];
|
|
const measurer = {
|
|
width(text, size) {
|
|
measured.push([text, size]);
|
|
return Array.from(text).length * (size === SMALL ? SIZE : size) * ADVANCE;
|
|
},
|
|
};
|
|
|
|
const options = (key) => ({
|
|
measurer,
|
|
fontFamily: FONT,
|
|
fontFamilyMono: FONT,
|
|
fontSize: SIZE,
|
|
idPrefix: `d${key}-`,
|
|
// At their own size, never scaled to fit or turned: custom.css scrolls a wide one sideways.
|
|
responsive: false,
|
|
autoDirection: false,
|
|
links: false,
|
|
images: false,
|
|
// Defaults a diagram's front matter can override. step draws edges with right angles.
|
|
config: {
|
|
flowchart: { curve: "step", wrappingWidth: 340, nodeSpacing: 45, rankSpacing: 45 },
|
|
sequence: { mirrorActors: false, actorMargin: 45, diagramMarginX: 1 },
|
|
},
|
|
});
|
|
|
|
// grow: the small text's font-size, which pele writes on each group of it, at 14px. Every such group
|
|
// must carry font-size="SMALL", and a diagram with small text but none of those groups means pele has
|
|
// changed how it writes it.
|
|
const SMALL_GROUP = /<g class="pele-(edge-labels|notes|boxes|frames)"[^>]*>/g;
|
|
const SMALL_ITEM = /class="pele-(edge-label|note|box|frame)/;
|
|
const SMALL_SIZE = new RegExp(`\\sfont-size="${SMALL}"`);
|
|
function grow(svg, where) {
|
|
const groups = svg.match(SMALL_GROUP) ?? [];
|
|
if (SMALL_ITEM.test(svg) && !groups.length) {
|
|
throw new Error(`${where}: pele drew small text outside its groups: check grow() against this pele`);
|
|
}
|
|
for (const g of groups) {
|
|
if (!SMALL_SIZE.test(g)) {
|
|
throw new Error(`${where}: pele drew ${g} without font-size="${SMALL}": check grow() against this pele`);
|
|
}
|
|
}
|
|
return svg.replace(SMALL_GROUP, (g) => g.replace(SMALL_SIZE, ` font-size="${SIZE}"`));
|
|
}
|
|
|
|
// A newline after each text element, outside it, where SVG draws nothing, so the site's search index,
|
|
// which reads a page's text without its tags, sees each label's last word apart from the next label's
|
|
// first. The lines of one label are tspans in one text element, where a newline would be a space
|
|
// that moves a centered line, so they stay joined.
|
|
const searchable = (svg) => svg.replaceAll("</text>", "</text>\n");
|
|
|
|
// Checks the things about pele that grow() relies on, which its docs don't promise: an edge label, a
|
|
// message and a note are measured at SMALL, and drawn in a group with font-size="SMALL".
|
|
function selfCheck() {
|
|
const cases = [
|
|
["flowchart", "flowchart TD\n a --> |edge label| b", "edge label", "edge-labels"],
|
|
["sequence", "sequenceDiagram\n A->>B: a message", "a message", "edge-labels"],
|
|
["note", "sequenceDiagram\n A->>B: hi\n Note right of B: a note", "a note", "notes"],
|
|
];
|
|
for (const [name, source, label, group] of cases) {
|
|
measured.length = 0;
|
|
const { svg } = render(source, options(`check-${name}`));
|
|
const sizes = measured.filter(([t]) => t === label).map(([, s]) => s);
|
|
if (!sizes.length || sizes.some((s) => s !== SMALL)) {
|
|
throw new Error(`self-check: pele measured the ${name}'s "${label}" at ${sizes.join(", ") || "no size"}, not ${SMALL}px`);
|
|
}
|
|
const tag = (svg.match(new RegExp(`<g class="pele-${group}"[^>]*>`)) ?? [""])[0];
|
|
if (!SMALL_SIZE.test(tag)) {
|
|
throw new Error(`self-check: pele's ${name} has no <g class="pele-${group}" font-size="${SMALL}">`);
|
|
}
|
|
}
|
|
}
|
|
|
|
// A diagram's accessible name and description: pele makes its accTitle and accDescr the SVG's
|
|
// <title> and <desc>, which screen readers read, so every diagram needs both.
|
|
function accessible(svg, where) {
|
|
if (!/aria-labelledby="[^"]*-title"/.test(svg) || !/aria-describedby="[^"]*-desc"/.test(svg)) {
|
|
throw new Error(`${where}: give the diagram an accTitle and an accDescr, which screen readers read`);
|
|
}
|
|
}
|
|
|
|
function* markdownFiles(dir) {
|
|
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
|
const p = join(dir, e.name);
|
|
if (e.isDirectory()) yield* markdownFiles(p);
|
|
else if (e.name.endsWith(".md")) yield p;
|
|
}
|
|
}
|
|
|
|
// The blocks, fenced at the start of a line; an indented one would reach the hook with other text.
|
|
function diagrams() {
|
|
const found = [];
|
|
for (const file of markdownFiles(join(docs, "content"))) {
|
|
const text = readFileSync(file, "utf8");
|
|
const where = (index) => `${relative(docs, file)}:${text.slice(0, index).split("\n").length}`;
|
|
for (const m of text.matchAll(/^[ \t]+```mermaid/gm)) {
|
|
throw new Error(`${where(m.index)}: an indented mermaid block; start its fence at the line's start`);
|
|
}
|
|
for (const m of text.matchAll(/^```mermaid[ \t]*\n([\s\S]*?)\n```[ \t]*$/gm)) {
|
|
found.push({ where: where(m.index), inner: m[1] });
|
|
}
|
|
}
|
|
return found;
|
|
}
|
|
|
|
const sha256 = (text) => createHash("sha256").update(text).digest("hex").slice(0, 16);
|
|
|
|
// The salt: the first 16 hex digits of the SHA-256 of pele's installed version and this script.
|
|
function salt() {
|
|
const pele = JSON.parse(readFileSync(join(docs, "node_modules", "pele", "package.json"), "utf8"));
|
|
return sha256(`pele ${pele.version}\n${readFileSync(script, "utf8")}`);
|
|
}
|
|
|
|
// The key the render hook computes: the first 16 hex digits of the SHA-256 of the salt, a newline and
|
|
// the block's text, which is Hugo's .Inner, the lines between the fences without the last newline.
|
|
const key = (s, inner) => sha256(`${s}\n${inner}`);
|
|
|
|
// Written beside its final name and renamed, so a run stopped halfway leaves no partial drawing that a
|
|
// later run would keep.
|
|
function put(name, data) {
|
|
writeFileSync(join(out, `${name}.tmp`), data);
|
|
renameSync(join(out, `${name}.tmp`), join(out, name));
|
|
}
|
|
|
|
try {
|
|
const s = salt();
|
|
const all = diagrams().map((d) => ({ ...d, key: key(s, d.inner) }));
|
|
mkdirSync(out, { recursive: true });
|
|
const missing = all.filter((d) => !existsSync(join(out, `${d.key}.svg`)) || !existsSync(join(out, `${d.key}.json`)));
|
|
// Only when there's something to draw, as a drawing already there passed it when it was drawn.
|
|
if (missing.length) selfCheck();
|
|
for (const d of missing) {
|
|
const { svg, width, height } = render(d.inner, options(d.key));
|
|
accessible(svg, d.where);
|
|
// The size first: the hook needs both, and the SVG's presence is what says it's drawn.
|
|
put(`${d.key}.json`, JSON.stringify({ width: Math.ceil(width), height: Math.ceil(height) }) + "\n");
|
|
put(`${d.key}.svg`, searchable(grow(svg, d.where)));
|
|
}
|
|
put(SALT_FILE, s);
|
|
const keep = new Set([SALT_FILE, ...all.flatMap((d) => [`${d.key}.svg`, `${d.key}.json`])]);
|
|
const unused = readdirSync(out).filter((f) => !keep.has(f));
|
|
for (const f of unused) rmSync(join(out, f), { force: true });
|
|
// The folder is mise's output for docs:diagrams, which it compares with the sources' times.
|
|
const now = new Date();
|
|
utimesSync(out, now, now);
|
|
console.log(
|
|
`render-diagrams: ${all.length} diagrams in ${relative(process.cwd(), out) || "."}, ${missing.length} drawn, ${unused.length} files deleted`,
|
|
);
|
|
} catch (e) {
|
|
console.error(`render-diagrams: ${e.message}`);
|
|
process.exit(1);
|
|
}
|