Mermaid Diagrams
Render beautiful diagrams and flowcharts directly in your markdown using Mermaid.
Overview
Marzipan provides two Mermaid plugins for different deployment scenarios:
| Plugin | Load Method | Best For |
|---|---|---|
| mermaidPlugin | ESM import | Production apps, bundled projects |
| mermaidExternalPlugin | CDN script | Sandboxed environments, quick prototypes |
Both plugins render Mermaid diagrams inline in the preview:
```mermaid
graph TD
A[Start] --> B{Decision}
B -->|Yes| C[Do Something]
B -->|No| D[Do Nothing]
C --> E[End]
D --> E
```
Mermaid Plugin (ESM)
Load Mermaid from npm/ESM for production use.
Installation
First, install Mermaid:
npm install mermaid
Then import the plugin:
import { Marzipan } from '@pinkpixel/marzipan';
import { mermaidPlugin } from '@pinkpixel/marzipan/plugins/mermaidPlugin';
new Marzipan('#editor', {
plugins: [mermaidPlugin()]
});
Options
interface MermaidPluginOptions {
theme?: 'default' | 'dark' | 'forest' | 'neutral';
config?: MermaidConfig; // Full Mermaid configuration object
}
Usage Examples
Basic Usage:
import { mermaidPlugin } from '@pinkpixel/marzipan/plugins/mermaidPlugin';
new Marzipan('#editor', {
plugins: [mermaidPlugin()]
});
With Dark Theme:
import { mermaidPlugin } from '@pinkpixel/marzipan/plugins/mermaidPlugin';
new Marzipan('#editor', {
theme: 'cave',
plugins: [
mermaidPlugin({
theme: 'dark'
})
]
});
Custom Configuration:
import { mermaidPlugin } from '@pinkpixel/marzipan/plugins/mermaidPlugin';
new Marzipan('#editor', {
plugins: [
mermaidPlugin({
config: {
theme: 'dark',
flowchart: {
curve: 'basis',
padding: 20
},
sequence: {
diagramMarginX: 50,
diagramMarginY: 10
}
}
})
]
});
Features
- ✅ Lazy loads Mermaid library
- ✅ Renders diagrams in preview
- ✅ Supports all Mermaid diagram types
- ✅ Configurable themes
- ✅ Production-ready (bundled with your app)
- ✅ Works offline
Mermaid External Plugin (CDN)
Load Mermaid from CDN - perfect for sandboxed environments.
Installation
import { mermaidExternalPlugin } from '@pinkpixel/marzipan/plugins/mermaidExternal';
new Marzipan('#editor', {
plugins: [mermaidExternalPlugin()]
});
Options
interface MermaidExternalOptions {
theme?: 'default' | 'dark' | 'forest' | 'neutral';
cdnUrl?: string; // Custom CDN URL
}
Usage Examples
Basic Usage:
import { mermaidExternalPlugin } from '@pinkpixel/marzipan/plugins/mermaidExternal';
new Marzipan('#editor', {
plugins: [mermaidExternalPlugin()]
});
Custom CDN:
import { mermaidExternalPlugin } from '@pinkpixel/marzipan/plugins/mermaidExternal';
new Marzipan('#editor', {
plugins: [
mermaidExternalPlugin({
theme: 'dark',
cdnUrl: 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js'
})
]
});
Features
- ✅ No npm dependency required
- ✅ Loads from CDN automatically
- ✅ Perfect for sandboxed playgrounds
- ✅ Lighter bundle size
- ✅ Same rendering quality
When to Use External Plugin
Choose mermaidExternalPlugin when:
- Building sandboxed code editors
- Creating quick prototypes
- Reducing bundle size is critical
- CDN access is reliable
- You don't need offline support
Supported Diagram Types
Mermaid supports many diagram types:
Flowchart
```mermaid
graph LR
A[Square Rect] --> B((Circle))
A --> C(Round Rect)
B --> D{Decision}
C --> D
```
Sequence Diagram
```mermaid
sequenceDiagram
Alice->>John: Hello John!
John-->>Alice: Great!
Alice-)John: See you later!
```
Class Diagram
```mermaid
classDiagram
Animal <|-- Duck
Animal <|-- Fish
Animal : +int age
Animal : +String gender
Animal: +isMammal()
class Duck{
+String beakColor
+swim()
}
class Fish{
-int sizeInFeet
-canEat()
}
```
State Diagram
```mermaid
stateDiagram-v2
[*] --> Still
Still --> [*]
Still --> Moving
Moving --> Still
Moving --> Crash
Crash --> [*]
```
Entity Relationship
```mermaid
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
```
Gantt Chart
```mermaid
gantt
title A Gantt Diagram
dateFormat YYYY-MM-DD
section Section
A task :a1, 2014-01-01, 30d
Another task :after a1 , 20d
```
Pie Chart
```mermaid
pie title Pets
"Dogs" : 386
"Cats" : 85
"Rats" : 15
```
Git Graph
```mermaid
gitGraph
commit
branch develop
checkout develop
commit
checkout main
merge develop
```
Theme Options
Built-in Mermaid themes:
Default Theme
mermaidPlugin({ theme: 'default' })
Light theme with blue/green colors.
Dark Theme
mermaidPlugin({ theme: 'dark' })
Dark background, perfect for cave theme.
Forest Theme
mermaidPlugin({ theme: 'forest' })
Green-focused color scheme.
Neutral Theme
mermaidPlugin({ theme: 'neutral' })
Grayscale, professional look.
Complete Example
import { Marzipan } from '@pinkpixel/marzipan';
import { mermaidPlugin } from '@pinkpixel/marzipan/plugins/mermaidPlugin';
const [editor] = new Marzipan('#editor', {
toolbar: true,
theme: 'cave',
plugins: [
mermaidPlugin({
theme: 'dark',
config: {
flowchart: {
curve: 'basis',
padding: 20
}
}
})
],
value: `# Project Flow
Here's our development workflow:
\`\`\`mermaid
graph TD
A[Planning] --> B[Development]
B --> C{Tests Pass?}
C -->|Yes| D[Deploy]
C -->|No| B
D --> E[Monitor]
E --> F{Issues?}
F -->|Yes| B
F -->|No| G[Done]
\`\`\`
`
});
Matching Editor Themes
Coordinate Mermaid theme with Marzipan theme:
import { Marzipan } from '@pinkpixel/marzipan';
import { mermaidPlugin } from '@pinkpixel/marzipan/plugins/mermaidPlugin';
// Determine theme based on user preference
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
new Marzipan('#editor', {
theme: isDark ? 'cave' : 'solar',
plugins: [
mermaidPlugin({
theme: isDark ? 'dark' : 'default'
})
]
});
Performance Tips
Optimization
- Lazy loading - Both plugins lazy-load Mermaid
- Debounce updates - Diagrams re-render on content change
- Simple diagrams - Complex diagrams slow down preview
- CDN for prototypes - Use external plugin for quick tests
Large Diagrams
Very large or complex Mermaid diagrams can slow down the preview. Consider:
- Breaking into smaller diagrams
- Using static images for very complex flows
- Debouncing preview updates
Browser Compatibility
Mermaid plugins work in:
- Chrome/Edge 60+
- Firefox 55+
- Safari 11+
Troubleshooting
Diagrams Not Rendering
- Check console for errors
- Verify Mermaid syntax is correct
- Ensure plugin is loaded before editor creation
- Try external plugin if ESM version fails
Theme Not Applying
// Ensure theme is set in config
mermaidPlugin({
theme: 'dark' // Not 'config.theme'
})
CDN Loading Fails
// Specify custom CDN
mermaidExternalPlugin({
cdnUrl: 'https://unpkg.com/mermaid@latest/dist/mermaid.min.js'
})
Resources
See Also
- Plugin Overview - All available plugins
- Configuration - Plugin configuration
- Bakeshop Demo - Live examples
Marzipan