Mermaid Diagrams
ModernDuang includes built-in support for Mermaid, a JavaScript library that renders diagrams and charts from plain text descriptions. Use Mermaid to create flowcharts, sequence diagrams, class diagrams, Gantt charts, and more — all within your Markdown posts.
Enabling Mermaid
Mermaid is loaded per post to avoid unnecessary JavaScript on pages that do not use it. To enable Mermaid for a specific post, add mermaid: true to the post's frontmatter:
Both syntaxes are equivalent and produce identical output. The tag syntax can be more convenient when your diagram content contains special characters. Regardless of which syntax you use, you must still set mermaid: true in the post frontmatter.
Supported Diagram Types
ModernDuang supports all diagram types provided by Mermaid. Here are the most commonly used:
Example: Sequence Diagram
Example: Gantt Chart
Example: State Diagram
Example: Mindmap
Dark Mode Compatibility
Mermaid diagrams automatically adapt to the current color scheme. The theme injects the Mermaid theme configuration based on whether light or dark mode is active:
- Light mode: Mermaid renders with
theme: default(dark text on white/transparent background) - Dark mode: Mermaid renders with
theme: dark(light text on dark background)
The theme switch happens automatically when the visitor toggles dark mode — diagrams re-render with the appropriate theme. No manual configuration is needed.
For more details on dark mode, see the Dark Mode guide.
Customizing Mermaid Theme
If you need more control over diagram appearance, you can use Mermaid's %%{init}%% directive at the top of your diagram:
Mermaid supports these built-in themes: default, forest, dark, neutral, base. However, manually setting a theme will override the automatic dark mode adaptation — use this only if you want a fixed appearance regardless of the user's color scheme.
Performance Considerations
- Each Mermaid diagram adds a small rendering overhead (~10–50ms for typical diagrams)
- Complex diagrams with many nodes may take longer
- The library itself is ~1MB (gzipped ~300KB) but loads only on pages with
mermaid: true - On slow connections, users will see loading placeholders until the library and render complete
Enabling Mermaid Globally
If you use Mermaid in most of your posts and do not want to set mermaid: true in every frontmatter, you could enable it globally by modifying your theme's layout. However, this is not recommended — it adds JavaScript overhead to every page, even those without diagrams. The per-post approach is the intended design.
Troubleshooting
Diagram renders as raw text
- Make sure the code block uses
```mermaid(not a different identifier) - Verify
mermaid: trueis in the post frontmatter - Check for syntax errors in the diagram definition
Diagram has wrong colors in dark mode
- Refresh the page after toggling dark mode (diagrams re-render on load)
- Avoid using
%%{init}%%to set a fixed theme if you want auto-adaptation
Diagram shows a loading spinner indefinitely
- Check browser console for network errors (CDN might be blocked)
- If you are in mainland China, consider configuring a domestic CDN mirror for Mermaid
Related Pages
- Dark Mode — How Mermaid diagrams adapt to color scheme changes
- Code Highlighting — Code block syntax highlighting (separate from Mermaid rendering)
