--- Why We Built This Diagrams are essential for AI-assisted programming. When you're working with an AI coding assistant, being able to vi…
--- Why We Built This Diagrams are essential for AI-assisted programming. When you're working with an AI coding assistant, being able to vi…
Diagrams are essential for AI-assisted programming. When you're working with an AI coding assistant, being able to visualize data flows, state machines, and system architecture—directly in your terminal or chat interface—makes complex concepts instantly graspable.
Mermaid is the de facto standard for text-based diagrams. It's brilliant. But the default renderer has problems:
We built beautiful-mermaid at Craft to power diagrams in Craft Agents. It's fast, beautiful, and works everywhere—from rich UIs to plain terminals.
The ASCII rendering engine is based on mermaid-ascii by Alexander Grooff. We ported it from Go to TypeScript and extended it. Thank you Alexander for the excellent foundation! (And inspiration that this was possible.)
useMemo()npm install beautiful-mermaid
# or
bun add beautiful-mermaid
# or
pnpm add beautiful-mermaidimport { renderMermaidSVG } from 'beautiful-mermaid'
const svg = renderMermaidSVG(`
graph TD
A[Start] --> B{Decision}
B -->|Yes| C[Action]
B -->|No| D[End]
`)Rendering is fully synchronous — no await, no promises. The ELK.js layout engine runs synchronously via a FakeWorker bypass, so you get your SVG string instantly.
Need async? Use renderMermaidSVGAsync() — same output, returns a Promise<string>.
import { renderMermaidASCII } from 'beautiful-mermaid'
const ascii = renderMermaidASCII(`graph LR; A --> B --> C`)┌───┐ ┌───┐ ┌───┐
│ │ │ │ │ │
│ A │────►│ B │────►│ C │
│ │ │ │ │ │
└───┘ └───┘ └───┘Because rendering is synchronous, you can use useMemo() for zero-flash diagram rendering:
import { renderMermaidSVG } from 'beautiful-mermaid'
function MermaidDiagram({ code }: { code: string }) {
const { svg, error } = React.useMemo(() => {
try {
return {
svg: renderMermaidSVG(code, {
bg: 'var(--background)',
fg: 'var(--foreground)',
transparent: true,
}),
error: null,
}
} catch (err) {
return { svg: null, error: err instanceof Error ? err : new Error(String(err)) }
}
}, [code])
if (error) return <pre>{error.message}</pre>
returnSupport rendering in Web Workers and Cloudflare Workers
`require("beautiful-mermaid")` in Node.js throws `ERR_PACKAGE_PATH_NOT_EXPORTED`
Bug: `A-->B` (no space before the arrow) parses as a single node `A--` and drops the edge
Bug: CJK Characters in Subgraph Title Cause Incorrect Layout (Labels Float to Top)
Emit classAssignments as CSS classes on SVG node elements
Root nodes in fan-in groups are placed without target-aware grouping, causing edge overlap
Root detection in grid layout is order-dependent, misclassifying child nodes as roots
A* pathfinding in edge-bundling exhausts memory (Map maximum size exceeded) on dense TD graphs
ASCII backend: an edge-less node in a target subgraph merges two subgraph frames and overwrites their titles
ASCII output: inline `<i>`/`<b>` tags rendered literally; edges to subgraph IDs create phantom nodes