All skills
ccheney avatar

/mermaid-diagrams

@f71635f

Create or fix Mermaid diagrams in Markdown. Use for requested flowcharts, sequence diagrams, ER diagrams, state machines, or system diagrams when Mermaid is the output format; not every visualization or explanation.

Use this Skill: https://skilld.dev/gh/ccheney/robust-skills/mermaid-diagrams

This session only. Nothing lands on disk.

referencesADVANCED.md

≈2.5k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Advanced Configuration & Styling

Theming, configuration, custom styling, and troubleshooting for Mermaid diagrams.

Contents


Configuration

Init Directive

Configure diagrams using the init directive. It must be immediately followed by a diagram — a directive alone renders nothing:

%%{init: { 'theme': 'dark' } }%%
flowchart LR
    A --> B

Multi-Line Configuration

%%{init: {
  'theme': 'base',
  'themeVariables': {
    'primaryColor': '#3b82f6',
    'primaryTextColor': '#ffffff'
  }
}}%%
flowchart LR
    A --> B --> C

Themes

Built-in Themes

Theme Description
default Default blue theme
dark Dark mode
forest Green nature theme
neutral Grayscale
base Base for customization

Usage

%%{init: {'theme': 'forest'}}%%
flowchart LR
    A --> B --> C

Theme Variables

Core Variables

Variable Description
primaryColor Main node color
primaryTextColor Text in primary nodes
primaryBorderColor Primary node border
secondaryColor Secondary elements
tertiaryColor Tertiary/background
lineColor Edge/arrow color
textColor General text
background Diagram background

Typography

Variable Description
fontSize Base font size
fontFamily Font family

Custom Theme Example

%%{init: {
  'theme': 'base',
  'themeVariables': {
    'primaryColor': '#3b82f6',
    'primaryTextColor': '#ffffff',
    'primaryBorderColor': '#2563eb',
    'secondaryColor': '#10b981',
    'tertiaryColor': '#f1f5f9',
    'lineColor': '#64748b',
    'textColor': '#1e293b',
    'fontSize': '16px',
    'fontFamily': 'Inter, sans-serif'
  }
}}%%
flowchart LR
    A[Start] --> B{Decision}
    B -->|Yes| C[Success]
    B -->|No| D[Failure]

Diagram-Specific Variables

Flowchart

Variable Description
nodeBorder Node border color
nodeTextColor Node text
clusterBkg Subgraph background
clusterBorder Subgraph border
edgeLabelBackground Edge label background

Sequence Diagram

Variable Description
actorBorder Actor border
actorBkg Actor background
actorTextColor Actor text
activationBorderColor Activation border
activationBkgColor Activation background
signalColor Arrow/signal color
signalTextColor Message text
noteBkgColor Note background
noteBorderColor Note border
noteTextColor Note text

State Diagram

Variable Description
labelColor State label
altBackground Composite state background

Gantt Chart

Variable Description
gridColor Grid lines
todayLineColor Today marker
taskTextColor Task text
doneTaskBkgColor Completed task
activeTaskBkgColor Active task
critBkgColor Critical path
taskBorderColor Task border

Styling

Class-Based Styling

Define Classes

flowchart LR
    A[Start]:::success --> B[Process]:::info --> C[End]:::warning

    classDef success fill:#10b981,stroke:#059669,color:white
    classDef info fill:#3b82f6,stroke:#2563eb,color:white
    classDef warning fill:#f59e0b,stroke:#d97706,color:white

Apply to Multiple Nodes

flowchart LR
    A --> B --> C --> D
    class A,D success
    class B,C info

    classDef success fill:#10b981
    classDef info fill:#3b82f6

Default Class

flowchart LR
    A --> B --> C

    classDef default fill:#f8fafc,stroke:#cbd5e1

Individual Node Styling

flowchart LR
    A --> B --> C

    style A fill:#10b981,stroke:#059669,color:white
    style B fill:#3b82f6,stroke:#2563eb,color:white
    style C fill:#ef4444,stroke:#dc2626,color:white

Style Properties

Property Example
fill fill:#3b82f6
stroke stroke:#2563eb
stroke-width stroke-width:2px
stroke-dasharray stroke-dasharray:5,5
color color:white
font-weight font-weight:bold

Link Styling

Individual Links

flowchart LR
    A --> B --> C --> D

    linkStyle 0 stroke:green,stroke-width:2px
    linkStyle 1 stroke:blue,stroke-width:2px
    linkStyle 2 stroke:red,stroke-width:2px,stroke-dasharray:5

All Links

flowchart LR
    A --> B --> C

    linkStyle default stroke:gray,stroke-width:1px

Layout Engine

ELK Renderer

For complex diagrams (v9.4+):

%%{init: {"flowchart": {"defaultRenderer": "elk"}} }%%
flowchart TB
    A --> B & C & D
    B & C & D --> E
    E --> F & G

Benefits:

  • Better handling of complex layouts
  • More predictable edge routing
  • Improved subgraph positioning

Security Levels

Control what Mermaid can do:

Level Description
strict Most secure, no HTML/JS
loose Allows some interaction
antiscript Allows HTML, blocks scripts
sandbox iframe sandbox
%%{init: { 'securityLevel': 'loose' }}%%
flowchart LR
    A --> B
    click A href "https://example.com" _blank

Troubleshooting

Common Issues

Special Characters

Escape with HTML entities or quotes:

flowchart LR
    A["Node with #quot;quotes#quot;"]
    B["Arrow -> symbol"]
    C["Hash #35; symbol"]

HTML Entities

Char Entity
# #35;
" #quot;
< #lt;
> #gt;
& #amp;
{ #123;
} #125;

Long Labels

Use markdown strings:

flowchart LR
    A["`This is a very long
    label that wraps
    across multiple lines`"]

Debugging Tips

1. Check Syntax

  • Verify diagram type declaration
  • Check for unclosed brackets/quotes
  • Ensure arrow syntax matches diagram type

2. Test Incrementally

  • Start with minimal diagram
  • Add elements one at a time
  • Identify breaking change

3. Use Live Editor

Test at: https://mermaid.live

4. Platform Differences

  • Check target platform support
  • Some features are version-specific
  • Export to PNG/SVG for guaranteed rendering

Arrow Syntax by Diagram Type

Diagram Solid arrow Async/open Dotted
Flowchart --> N/A -.->
Sequence ->> (sync) -) -->> (response)
Class --> N/A ..>
State --> N/A N/A

Arrow syntax does not transfer between diagram types — ->> in a flowchart or -.-> in a sequence diagram are parse errors.


Frontmatter Configuration

Alternative to init directive:

---
title: My Diagram
config:
  theme: forest
  flowchart:
    defaultRenderer: elk
---
flowchart LR
    A --> B

Directive Reference

Diagram Directives

Diagram Directive
All %%{init: {...}}%%
Flowchart flowchart config
Sequence sequenceDiagram config
Class classDiagram config
State stateDiagram config
ER erDiagram config
Gantt gantt config

Common Init Options

%%{init: {
  'theme': 'default',
  'themeVariables': { ... },
  'flowchart': {
    'defaultRenderer': 'elk',
    'curve': 'basis',
    'padding': 15
  },
  'sequence': {
    'showSequenceNumbers': true,
    'actorMargin': 50,
    'boxMargin': 10
  },
  'gantt': {
    'barHeight': 20,
    'fontSize': 11,
    'sectionFontSize': 14
  }
}}%%

Accessibility

accTitle and accDescr

Mermaid's built-in accessibility fields render as SVG <title>/<desc> for screen readers:

flowchart LR
    accTitle: Login flow
    accDescr: User submits credentials, the app validates them against the auth service
    A[User] --> B[App] --> C[Auth]

Alt Text

Also provide context in prose before diagrams:

The following diagram shows the authentication flow:

```mermaid
sequenceDiagram
    User->>App: Login
    App->>Auth: Validate

## ARIA Labels

When embedding in HTML:

```html
<div class="mermaid" role="img" aria-label="Authentication flow diagram">
  sequenceDiagram
    User->>App: Login
</div>

Performance Tips

  1. Limit complexity - Split large diagrams
  2. Use ELK for complex layouts
  3. Minimize styling - Class-based over inline
  4. Cache renders when possible
  5. Lazy load in documentation

Export Options

From Live Editor

  • PNG (transparent or white background)
  • SVG (scalable)
  • Markdown

Programmatic

import mermaid from 'mermaid';

const svg = await mermaid.render('id', diagramText);

CLI

# Single diagram file -> SVG (also .png, .pdf)
npx -y @mermaid-js/mermaid-cli -i diagram.mmd -o diagram.svg

# Markdown file: renders every ```mermaid block to an image, rewrites links
npx -y @mermaid-js/mermaid-cli -i input.md -o output.md

The CLI is also the fastest way to validate generated diagrams — a syntax error exits non-zero with a parse message.

Source: SKILL.md on GitHub

No alerts15d4 checks · Risk SAFE
  • Gen Agent Trust Hub15d

    This skill provides a comprehensive set of instructions and references for creating and fixing Mermaid diagrams. It includes extensive documentation on different diagram types and styles, and references official, trusted tools like the Mermaid Live Editor and the Mermaid CLI for validation and rendering.

  • Socket15d

    No alerts

  • Snyk15d

    Risk: LOW · No issues

  • Runlayer7mo

    3/10 files flagged

Signed by skilld at f71635f. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 3 weeks ago.

Activeupdated 3 weeks ago

README badge

README badge for ccheney/robust-skills/mermaid-diagrams