Mermaid Diagrams
Markdown-OS provides full support for Mermaid diagrams with interactive pan/zoom controls, fullscreen viewing, and theme synchronization. Diagrams are rendered using Mermaid.js withsvg-pan-zoom for interactivity.
Creating Diagrams
Mermaid diagrams are created using fenced code blocks with themermaid language identifier:
The editor automatically detects
language-mermaid or lang-mermaid code blocks and renders them as interactive diagrams.Diagram Types
Mermaid supports many diagram types:- Flowcharts:
graph TD,graph LR - Sequence Diagrams:
sequenceDiagram - Class Diagrams:
classDiagram - State Diagrams:
stateDiagram-v2 - Entity Relationship:
erDiagram - Gantt Charts:
gantt - Pie Charts:
pie - Git Graphs:
gitGraph
Interactive Controls
Each rendered diagram includes interactive controls:Inline Toolbar
1
Edit Button
Opens a modal editor to modify the diagram source code
2
Fullscreen Button
Expands the diagram to fullscreen with enhanced pan/zoom controls
Zoom Controls
Every diagram has zoom controls in the bottom-right corner:- + Button: Zoom in
- Reset Icon: Reset view (fit and center)
- − Button: Zoom out
Pan & Zoom
Diagrams usesvg-pan-zoom library for smooth interaction:
Pan/Zoom Features
- Zoom Sensitivity: 0.03 for smooth, precise zooming
- Zoom Range: 0.5x to 20x magnification
- Auto-fit: Diagrams automatically fit within the container on load
- Auto-center: Diagrams are centered after fitting
Fullscreen Mode
Click the fullscreen button to view diagrams in an expanded modal:1
Open Fullscreen
Diagram is re-rendered at full viewport size with loading spinner
2
Enhanced Controls
Fullscreen mode has wider zoom range (0.2x to 40x) and full-viewport panning
3
Close
Press Escape or click the close button to return to the editor
Fullscreen mode preserves editor scroll position and focus, restoring them when closed.
Theme Synchronization
Mermaid diagrams automatically adapt to the active editor theme:Theme Initialization
Rendering Pipeline
Diagrams go through a multi-step rendering process:1
Detection
Code blocks with
language-mermaid are detected during markdown parsing2
Container Creation
Each diagram is wrapped in a
.mermaid-container with a .mermaid-canvas3
Mermaid Rendering
mermaid.run() processes all .mermaid nodes and generates SVG output4
SVG Sizing
Custom logic fixes SVG dimensions to maintain aspect ratio within max height
5
Pan/Zoom Setup
svg-pan-zoom is attached to enable interactive controls6
Controls Addition
Edit, fullscreen, and zoom control buttons are added to the container
SVG Dimension Handling
Diagrams are sized intelligently to fit within constraints:The max height is configurable via CSS custom property
--mermaid-max-height (default: 420px).Error Handling
Invalid Mermaid syntax is handled gracefully:Editing Diagrams
Click the edit button to modify a diagram:Edit Modal
Edit Modal
The block editor modal opens with:
- Title: “Edit Mermaid diagram”
- Textarea pre-filled with current source
- Save and Cancel buttons
- Keyboard shortcuts (Enter to save, Escape to cancel)
Re-rendering
Re-rendering
After saving:
- Source is stored in
data-mermaid-sourceattribute - Canvas is cleared and new mermaid node is created
renderMermaidDiagrams()is called to re-render- Controls are re-attached
- Change event is emitted for auto-save
Best Practices
Keep diagrams focused: Large complex diagrams can be difficult to read. Consider splitting them into multiple smaller diagrams.