gstack/lib/diagram-render
Garry Tan 69bf3b07a1
feat(diagram-render): __downscaleRaster for print-resolution image normalization
Data-URI rasters re-encode in their own format (JPEG stays JPEG at q0.9 —
PNG-encoding photos bloats them) at an explicit target pixel width. Used by
make-pdf's pre-pass for the 300dpi content-box ceiling (eng-review D4).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 23:59:40 -07:00
..
dist feat(diagram-render): __downscaleRaster for print-resolution image normalization 2026-06-11 23:59:40 -07:00
scripts feat(diagram-render): offline mermaid+excalidraw render bundle for browse 2026-06-11 23:26:28 -07:00
src feat(diagram-render): __downscaleRaster for print-resolution image normalization 2026-06-11 23:59:40 -07:00
README.md feat(diagram-render): offline mermaid+excalidraw render bundle for browse 2026-06-11 23:26:28 -07:00
THIRD-PARTY-LICENSES.md feat(diagram-render): offline mermaid+excalidraw render bundle for browse 2026-06-11 23:26:28 -07:00
bun.lock feat(diagram-render): offline mermaid+excalidraw render bundle for browse 2026-06-11 23:26:28 -07:00
package.json feat(diagram-render): offline mermaid+excalidraw render bundle for browse 2026-06-11 23:26:28 -07:00

README.md

diagram-render

Offline diagram rendering for make-pdf and /diagram. One self-contained HTML page (dist/diagram-render.html, ~9MB) bundles mermaid, the excalidraw export utilities, and the official mermaid→excalidraw converter. The browse daemon loads it with load-html; callers drive it through browse js and pull bytes back with js --out.

The built page is committed (eng-review D2): rendering works with zero network at install time and render time, and there is no npm supply-chain surface in ./setup. The drift test (test/diagram-render-drift.test.ts) fails CI if dist/ is edited by hand or falls out of sync with BUILD_INFO.json.

Page API (window functions)

Function In → Out
__renderMermaid(id, text) mermaid text → SVG string. id must be unique per fence (mermaid-fence-<n>) — it namespaces every internal SVG id.
__mermaidToExcalidraw(text) mermaid text → .excalidraw scene JSON (flowcharts fully; other types degrade upstream).
__excalidrawToSvg(sceneJson) scene JSON → SVG string (Excalifont embedded, offline).
__rasterize(svg, targetWidthPx) SVG → PNG data URL. Callers own DPI math: targetWidthPx = placed width (in) × 300. Throws on tainted canvas.
__mountForScreenshot(svg, px) taint-proof fallback: mounts SVG at #raster-stage for browse screenshot --selector.
__probeImage(src) data URI/URL → {width, height} JSON.
__bundleInfo { name, deps } — pinned dependency versions baked at build.

Readiness: poll until #status text is ready (or browse wait '#done'). Page errors accumulate in window.__errors.

Updating

# 1. edit the exact pin in package.json
cd lib/diagram-render && bun install
# 2. rebuild (deterministic; build twice → same sha)
bun run build
# 3. commit package.json + bun.lock + dist/ together

Render contract details (securityLevel strict, htmlLabels false, print-css font lock, <base href> + </scri escaping) are documented in src/entry.ts and scripts/build.ts — read both before touching either.