home / skills / nilecui / skillsbase / mermaid

mermaid skill

/.cursor/skills/mermaid

This skill helps you create, validate, and render Mermaid diagrams from natural language descriptions or code analysis.

npx playbooks add skill nilecui/skillsbase --skill mermaid

Review the files below or copy the command above to add this skill to your agents.

Files (25)
SKILL.md
4.7 KB
---
name: mermaid
description: Guide for creating mermaid diagrams. This skill should be used when users want to create a mermaid diagram (or update an existing diagram).
allowed_tools:
  - Bash(mmdc *)
---

# Mermaid Diagram Skill

This skill helps you create, validate, and render Mermaid diagrams from natural language descriptions, code analysis, or by editing existing diagrams.

## Core Workflow

Follow these steps for every diagram creation/modification:

### 1. Understand the Request
- Identify the diagram type needed (flowchart, sequence, class, etc.)
- Extract key elements: entities, relationships, flow, hierarchy
- Determine if creating new, editing existing, or generating from code

### 2. Generate Mermaid Code
- Create syntactically correct Mermaid diagram code
- Use appropriate diagram type syntax
- Apply consistent naming and styling
- Refer to `examples/` directory for type-specific syntax (load on-demand)

### 3. Save to File
- Write code to `.mmd` file
- Generate meaningful filename based on diagram purpose (e.g., `user-authentication-flow.mmd`, `database-schema.mmd`)
- Use kebab-case for filenames

### 4. Validate with CLI
- **ALWAYS** validate using: `mmdc -i <filename>.mmd`
- Check for syntax errors and warnings
- If validation fails, proceed to step 5

### 5. Auto-Correct Errors
- Analyze error messages from `mmdc`
- Common issues:
  - Invalid syntax or keywords
  - Missing quotes around labels with spaces
  - Incorrect arrow syntax
  - Malformed node definitions
- Automatically fix the code
- Re-save and re-validate
- Repeat until validation succeeds

### 6. Render on Request
- Default format: **SVG**
- Command: `mmdc -i <filename>.mmd -o <filename>.svg`
- Alternative formats: PNG (`-o <filename>.png`), PDF (`-o <filename>.pdf`)
- Only render when user explicitly requests a preview/image

## Supported Diagram Types

Load examples from `examples/` directory as needed:

**Basic Diagrams:**
- Flowchart (`examples/flowchart.md`)
- Sequence Diagram (`examples/sequence.md`)
- Class Diagram (`examples/class.md`)
- State Diagram (`examples/state.md`)
- Entity Relationship (`examples/er.md`)

**Planning & Management:**
- Gantt Chart (`examples/gantt.md`)
- User Journey (`examples/journey.md`)
- Timeline (`examples/timeline.md`)
- Kanban (`examples/kanban.md`)

**Data Visualization:**
- Pie Chart (`examples/pie.md`)
- XY Chart (`examples/xy-chart.md`)
- Quadrant Chart (`examples/quadrant.md`)
- Sankey (`examples/sankey.md`)
- Radar (`examples/radar.md`)
- Treemap (`examples/treemap.md`)

**Technical Diagrams:**
- Git Graph (`examples/git.md`)
- C4 Diagram (`examples/c4.md`)
- Requirement Diagram (`examples/requirement.md`)
- Architecture (`examples/architecture.md`)
- Block Diagram (`examples/block.md`)
- Packet (`examples/packet.md`)

**Organizational:**
- Mindmap (`examples/mindmap.md`)
- ZenUML (`examples/zenuml.md`)

## Code Analysis → Diagram

When analyzing code to create diagrams:

**Class Diagrams:**
- Extract classes, methods, properties, inheritance, interfaces
- Show relationships: inheritance, composition, aggregation

**Sequence Diagrams:**
- Track function calls, async operations, API interactions
- Show actors, lifelines, activation boxes

**Flowcharts:**
- Map control flow, conditionals, loops
- Show function entry/exit points

**State Diagrams:**
- Identify states from enums, state machines, status fields
- Map transitions and events

## Templates

Common patterns available in `templates/common-patterns.md` (load on-demand):
- Standard flowchart structures
- API sequence patterns
- Database ER patterns
- Microservice architecture layouts
- State machine templates

## Best Practices

**Styling:**
- Use meaningful node IDs
- Add clear, concise labels
- Apply subgraphs for grouping related elements
- Use classDefs for visual consistency

**Readability:**
- Keep diagrams focused (split large diagrams into smaller ones)
- Use top-to-bottom or left-to-right orientation consistently
- Add comments in code for complex sections

**Validation:**
- NEVER skip validation step
- Always fix errors before presenting to user
- Test rendered output when in doubt

## Error Handling

If `mmdc` validation fails:
1. Read error message carefully
2. Identify line number and issue
3. Apply fix (common fixes in examples)
4. Re-validate
5. Inform user only if repeated attempts fail

## Output Format

Present to user:
```
Created: <filename>.mmd

<Show the mermaid code in a code block>

āœ“ Validated successfully with mermaid-cli
```

If rendered:
```
Created: <filename>.mmd
Rendered: <filename>.svg

[Show file paths]
```

## Notes

- Assume `mmdc` (mermaid-cli) is installed and available
- Default output format: SVG
- Always validate before presenting
- Fix errors autonomously
- Only load example files when needed for specific diagram type

Overview

This skill guides creation, editing, validation, and optional rendering of Mermaid diagrams from natural language, code analysis, or existing .mmd files. It produces syntactically correct Mermaid code, saves it to kebab-case .mmd files, validates with mermaid-cli, auto-corrects errors, and renders images only when requested. The goal is clear, maintainable diagrams you can preview or export as SVG/PNG/PDF.

How this skill works

I determine the diagram type (flowchart, sequence, class, state, ER, Gantt, etc.), extract entities and relationships from your description or source code, and generate Mermaid code using appropriate syntax and naming conventions. I save the file with a meaningful kebab-case name, run mmdc -i <filename>.mmd to validate, analyze any error output, apply automatic fixes, and re-validate until successful. Rendering to SVG/PNG/PDF is performed only when you explicitly request an image preview.

When to use it

  • You want a new Mermaid diagram from a textual description or system spec.
  • You need a diagram generated from source code (classes, sequences, states, flows).
  • You have an existing .mmd file that needs fixing or updating.
  • You want validated, ready-to-render Mermaid code with meaningful filenames.
  • You need a preview image (SVG/PNG/PDF) only after validation.

Best practices

  • Choose the right diagram type before generating code (flowchart, sequence, class, etc.).
  • Use meaningful node IDs and concise labels; prefer kebab-case filenames like user-authentication-flow.mmd.
  • Group related elements with subgraphs and use classDefs for consistent styling.
  • Keep diagrams focused; split large diagrams into smaller linked files.
  • Always validate with mmdc and let the skill auto-correct errors before rendering.

Example use cases

  • Generate a user-authentication flowchart from a brief text description and save as user-authentication-flow.mmd.
  • Analyze Python classes to produce a class diagram showing inheritance and composition.
  • Convert API call traces into a sequence diagram that visualizes interactions and async calls.
  • Fix a failing .mmd file by validating with mermaid-cli, applying suggested fixes, and returning corrected code.
  • Render an SVG preview of a validated diagram on explicit request.

FAQ

Will you render images automatically?

No. I validate and fix code first; I only render to SVG/PNG/PDF when you explicitly ask for a preview or export.

What do you do when mmdc reports an error?

I parse the mmdc output, apply common fixes (quotes around labels with spaces, correct arrows, node syntax), re-save, and re-validate until it succeeds or repeated attempts fail, at which point I report the issue and suggested next steps.