30

Mermaid Diagram Generation

⏱️ 12 min

Mermaid Diagram Generation

What Is Mermaid?

Mermaid is a text-based diagram language. Simple syntax, real diagrams -- flowcharts, sequence diagrams, class diagrams, you name it. Paired with AI, you can visualize code architecture fast.

Common Diagram Types

1. Entity Relationship Diagrams

Great for database design and data models:

erDiagram
    CUSTOMER ||--o{ ORDER : places
    ORDER ||--|{ LINE-ITEM : contains
    PRODUCT ||--o{ LINE-ITEM : includes
    CUSTOMER {
        string name
        string email
    }
    ORDER {
        int orderNumber
        date created
    }

2. Flow Charts

Great for process and logic flows:

flowchart TD
    A[Start] --> B{Decision}
    B -->|Yes| C[Process]
    B -->|No| D[End]
    C --> E{Another Decision}
    E -->|Option 1| F[Action 1]
    E -->|Option 2| G[Action 2]
    F --> D
    G --> D

3. Sequence Diagrams

Great for interaction flows:

sequenceDiagram
    participant Client
    participant Server
    participant Database

    Client->>Server: HTTP Request
    Server->>Database: Query
    Database-->>Server: Result
    Server-->>Client: JSON Response

4. Class Diagrams

Great for object-oriented design:

classDiagram
    class Animal {
        +name: string
        +age: int
        +makeSound()
    }
    class Dog {
        +breed: string
        +bark()
    }
    class Cat {
        +color: string
        +meow()
    }
    Animal <|-- Dog
    Animal <|-- Cat

Process for Generating Diagrams

  1. Analyze source files (SQL, code, docs)
  2. Extract entities and relationships
  3. Generate appropriate diagram type
  4. Include relevant properties/methods
  5. Validate syntax with mermaid compiler
  6. Save to specified location

Validation

npx -p @mermaid-js/mermaid-cli mmdc -i <input>.md -o test.md

Best Practices

  • Keep diagrams focused and readable
  • Use consistent naming conventions
  • Group related entities
  • Add labels to relationships
  • Include cardinality where relevant
  • Use subgraphs for organization
  • Add styling for clarity

Having AI Generate Diagrams

In Cursor or Claude Code, you can prompt like this:

Analyze all model files under src/models/
and generate a Mermaid ER diagram showing entity relationships.

AI will:

  1. Read model files
  2. Analyze fields and relationships
  3. Generate Mermaid syntax
  4. Output renderable diagram code

Using Diagrams in Docs

Most Markdown renderers (GitHub, GitLab, Notion, etc.) support Mermaid. Just specify mermaid as the code block language:

```mermaid
flowchart LR
    A --> B --> C
```

Advanced Styling

flowchart TD
    subgraph Frontend
        A[React App]
        B[State Management]
    end

    subgraph Backend
        C[API Server]
        D[Database]
    end

    A --> C
    B --> A
    C --> D

    style A fill:#f9f,stroke:#333
    style C fill:#bbf,stroke:#333

Next Steps

Check out Task Implementation Methodology for systematic planning and execution.

📚 相关资源

❓ 常见问题

关于本章主题最常被搜索的问题,点击展开答案

Mermaid 支持哪些图表类型?分别用在什么场景?

本章重点讲 4 种:(1) ER 图(erDiagram)—— 数据库 schema、模型关系;(2) 流程图(flowchart TD)—— 业务流程、决策树;(3) 时序图(sequenceDiagram)—— 前后端 / API / DB 交互;(4) 类图(classDiagram)—— 面向对象设计、继承关系。还有 gantt / state / pie 等,但日常 80% 场景这 4 种够用。

Mermaid 图怎么本地验证语法是否正确?

用 Mermaid CLI:`npx -p @mermaid-js/mermaid-cli mmdc -i <input>.md -o test.md`,跑通就语法 OK,跑挂会报具体行号错误。不想装可以贴到 mermaid.live 在线渲染。GitHub / GitLab / Notion 的 Markdown 渲染器都内置 Mermaid,PR preview 也能直接看。

怎么让 AI 自动生成项目的 ER 图?

在 Cursor / Claude Code 里给一句目标 prompt:"请分析 src/models/ 目录下的所有模型文件,生成一个 Mermaid ER 图表,展示实体之间的关系"。AI 会读模型文件 → 提取字段和外键 → 输出 erDiagram 语法。建议先 limit 到 5-10 个核心 model,超过 20 个图就乱到看不懂。

Mermaid 图什么时候用 subgraph?

节点超过 10 个或有清晰边界(前端/后端、microservice 划分)时用。例如把 React App + State Management 包进 `subgraph Frontend`,API Server + Database 包进 `subgraph Backend`,再用 `A --> C` 跨域连线。视觉上立刻分组,比一堆扁平节点好读 3 倍。

Mermaid 图渲染太丑怎么办?怎么加样式?

用 `style` 关键字给单节点上色:`style A fill:#f9f,stroke:#333`,常用于高亮入口节点 / 异常节点。整体主题切换用 `%%{init: {'theme':'dark'}}%%` 写在文件首行,base/dark/forest/neutral 4 种内置主题。复杂样式建议导出 SVG 用 Figma 二次编辑,Mermaid 本身不是 design tool。