Appearance
How to render a diagram
Render saved Coral source to SVG and a standalone HTML document using the production source reader, checks, layout, and renderer.
Before you begin
Use Node.js 24.19.0 or later. Install the verified Coral 0.2.17 browser archive with its exact published GraphIR Core pin:
sh
npm install @graph-ir/core@0.2.4 ./coral-viz-browser-0.2.17.tgzUse the archive names delivered with your verified packages. The browser package bundles the language and render implementation; this example does not require separate language or render installs. Verify archive checksums before installation.
Render the document
- Keep the saved
@diagramheader and ID-first declarations in the source. - Call
renderfrom@coral-viz/browserwith that exact source text. - Use the returned
svg, authoredsourceGraph, and diagnostics. - For standalone HTML, place the exact self-contained SVG in an HTML document, as shown below. Save
sourceas text to reopen the diagram.
The executable source is included directly from render-diagram.mjs:
mjs
import assert from 'node:assert/strict';
import { render } from '@coral-viz/browser';
// Keep this exact text as the document. The saved header selects its notation.
export const source = `@diagram architecture
api service as "API"
orders database as "Orders"
writes = api -> orders as "writes"
`;
// The production source path parses, checks, lays out, and renders the document.
// A rejected input throws CoralRenderError with located diagnostics and no SVG.
export const result = await render(source, {
style: 'hifi', direction: 'RIGHT', title: 'Order service',
});
export const { svg } = result;
assert.equal(result.sourceGraph.version, '4.0.0');
assert.equal(result.sourceGraph.nodes.length, 2);
assert.equal(result.sourceGraph.edges.length, 1);
assert.match(svg, /^<svg/);
assert.match(svg, /API/);
// The SVG includes its font resources. Wrap that exact result for a standalone
// document; no second projection, layout, script, or network renderer is needed.
export const html = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Order service</title>
<style>body{margin:1rem}svg{max-width:100%;height:auto}</style>
</head>
<body>${svg}</body>
</html>`;
assert.ok(html.includes(svg));
assert.ok(result.sourceGraph.nodes.every((node) => node.position === undefined),
'generated layout must not become authored source geometry');
// Persist source as text. sourceGraph is authored portable meaning; result.graph
// is a temporary GraphIR 3 display view, not a document to save or print.
console.log(`Rendered ${result.sourceGraph.nodes.length} nodes from saved Coral to SVG and standalone HTML.`);Run the example with node render-diagram.mjs from a directory containing the installed package. Its exported source, result, svg, and html values can also be imported by an application. The example does not write files.
render rejects unsupported or invalid input with a CoralRenderError carrying located diagnostics. A missing header cannot select an older reader. Retain rejected source as text so the author can fix it. Successful rendering returns warnings and informational diagnostics in result.diagnostics.
Keep source and display geometry distinct
result.sourceGraph contains the authored GraphIR 4 meaning. result.graph is a temporary GraphIR 3 display projection; generated positions and connector routes are display state. Keep the original source for editing and saving.
The lower-level renderSvg and renderHtml functions in @coral-viz/render serialize supplied geometry and do not perform layout. This example uses the production source-owned convenience path, which performs the required layout and keeps semantic ports, connector ends, and source diagnostics paired.