Features
Mermaid Diagram Integration
Guide on Mermaid diagram integration in Sypha, including rendering, auto-fixing, and architecture.
Mermaid Diagram Integration Guide
This feature renders Mermaid diagrams directly in chat messages and other markdown surfaces, automatically fixing common syntax issues and providing export options.
High-level flow
- 1. Authoring: A model or user outputs a Mermaid block in markdown (
mermaid ...). - 2. Detection: The markdown renderer detects
language-mermaidcode blocks and mounts theMermaidBlockcomponent. - 3. Render or Fix:
MermaidBlocktries to parse and render via Mermaid; if parsing fails, a deterministic fixer runs, and optionally an AI-assisted fixer is invoked. - 4. Display: Successful renders are injected as SVG into the DOM, themed for the editor, and can be exported as PNG.
- 5. Inline preference: If a tool attempts to write a file containing a Mermaid diagram, the system prefers inline rendering in the conversation instead of creating a file.
Where the logic lives
-
Rendering and UI (webview):
webview-ui/src/components/common/MarkdownBlock.tsx- Detects
language-mermaidcode blocks and routes them toMermaidBlock.
- Detects
webview-ui/src/components/common/MermaidBlock-sypha.tsx- Initializes Mermaid with a dark theme, parses and renders diagrams, makes SVG responsive, shows loading/errors, and exposes export via
MermaidButton.
- Initializes Mermaid with a dark theme, parses and renders diagrams, makes SVG responsive, shows loading/errors, and exposes export via
webview-ui/src/components/common/MermaidButton.tsxandMermaidActionButtons.tsx- Houses action buttons (copy/export/etc.) displayed over the rendered diagram container.
-
Syntax fixing (webview):
webview-ui/src/services/mermaidSyntaxFixer.ts- Deterministic fixes: strip fences, decode HTML arrows, remove zero‑width chars, validate with
mermaid.parse. - AI-assisted fix: posts a
fixMermaidSyntaxrequest to the host with an error hint; resolves onmermaidFixResponse.
- Deterministic fixes: strip fences, decode HTML arrows, remove zero‑width chars, validate with
-
Host bridge (VS Code extension side):
src/hosts/vscode/VscodeWebviewProvider.ts- Handles
fixMermaidSyntaxmessages from the webview. - Calls the configured API with a targeted system prompt to correct Mermaid content and replies with
mermaidFixResponse.
- Handles
-
Inline-over-file preference (core):
src/core/task/ToolExecutor.ts- If a write-to-file block contains Mermaid content (either fenced or bare starting with
graph|flowchart|sequenceDiagram|classDiagram|stateDiagram|erDiagram|gantt|journey|mindmap|pie), it emits the diagram inline to chat instead of writing a file.
- If a write-to-file block contains Mermaid content (either fenced or bare starting with
Rendering details
-
Theme and init:
MermaidBlock-sypha.tsxsets a dark theme (MERMAID_THEME) and callsmermaid.initialize({ startOnLoad: false, securityLevel: "loose", theme: "dark", flowchart: { useMaxWidth: true }, themeVariables: { ... } }).
-
Parse and render:
- Debounced effect calls
mermaid.parse(renderCode)thenmermaid.render(id, renderCode)and injects returned SVG into a container. - After insertion, width/height attributes are removed,
preserveAspectRatiois set, and CSS ensures responsiveness.
- Debounced effect calls
-
Error handling and auto-fix:
- On parse failure, runs
MermaidSyntaxFixer.autoFixSyntax(deterministic). If still invalid, shows an error and keeps original code visible as escaped text. - Users can click “Fix with AI,” which triggers
autoFixSyntaxWithAI→ postsfixMermaidSyntaxto the extension → extension uses the active provider to return corrected code → component re-renders.
- On parse failure, runs
-
Export to PNG:
- Clicking the SVG area runs
svgToPng, which clones the SVG, sets a large width (e.g., 3600), draws onto a canvas with the themed background, and opens the PNG viaFileServiceClient.openImage.
- Clicking the SVG area runs
Markdown detection
MarkdownBlock.tsxmaps code nodes withlanguage-mermaidto<MermaidBlock code={...} />.- It also avoids wrapping those blocks with the generic copy button container, since the diagram UI provides its own actions.
Message protocol for AI fixes
- Webview → Host:
{ type: "fixMermaidSyntax", requestId, text, values: { error } }. - Host computes a fix using the configured API and replies:
{ type: "mermaidFixResponse", requestId, success: true, fixedCode }on success{ type: "mermaidFixResponse", requestId, success: false, error }on failure
Developer tips
- To change theming: edit
MERMAID_THEMEandthemeVariablesinMermaidBlock-sypha.tsx. - To add more auto-fixes: extend
applyDeterministicFixesand optionally the host prompt inVscodeWebviewProvider. - To support more diagram kinds: update the detection regexes in
ToolExecutor.tsand ensure Mermaid version supports them.
Quick start
- Paste a fenced Mermaid block in chat or markdown:
graph TD
A[Start] --> B{Decision}
B -->|Yes| C[Do x]
B -->|No| D[Do y]- Ensure the code fence language is exactly
mermaid.
Getting Started Workflow (New Users)
- Paste a Mermaid code block into the chat using ```mermaid fences.
- If it fails to render, click “Fix with AI” to auto-correct syntax.
- Click the diagram area to export as PNG if needed.
Export and copy behavior
- Click on the rendered diagram area to export a PNG. The canvas renders at width 3600 for crisp images; height scales automatically.
- The diagram UI provides its own actions; generic copy buttons are suppressed for Mermaid blocks.
Troubleshooting
- Parse error: Unknown diagram type: Ensure the first line starts with a supported keyword (e.g.,
graph,flowchart,sequenceDiagram). - Broken arrows or missing edges: HTML-escaped arrows like
-->are auto-decoded; try “Fix with AI” if layout still breaks. - Invisible or clipped diagram: Large graphs may render off-screen; the SVG is responsive (width/height removed), so ensure the container is wide enough.
- Still failing: Click “Fix with AI” to run the extension-side syntax repair and re-render.
Performance and limits
- Rendering is debounced by ~500ms after input changes.
- Very large diagrams can be slow; consider simplifying or splitting.
- SVG responsiveness is applied by removing width/height and setting
preserveAspectRatio.
Theming specifics
- Theme variables inherit from VS Code theme variables where available.
- Adjust colors in
MERMAID_THEMEand extendthemeVariablesinwebview-ui/src/components/common/MermaidBlock-sypha.tsx.
Version and supported types
- Mermaid version is defined in
webview-ui/package.json. - Detection supports:
graph,flowchart,sequenceDiagram,classDiagram,stateDiagram,erDiagram,gantt,journey,mindmap,pie.
Inline-over-file behavior
- Implemented in
src/core/task/ToolExecutor.ts. When file content includes a Mermaid diagram, it’s rendered inline in chat instead of writing the file.