---
title: "Mermaid Diagrams"
description: "With `movkNuxtDocs.mermaid` enabled and mermaid and dompurify installed, code blocks in the `mermaid` language render as flowcharts, sequence diagrams, class diagrams and more. Diagrams follow the color mode, load when scrolled into view and support copy and fullscreen."
canonical_url: "https://docs.mhaibaraai.cn/en/docs/writing/mermaid"
---
# Mermaid Diagrams

> With `movkNuxtDocs.mermaid` enabled and mermaid and dompurify installed, code blocks in the `mermaid` language render as flowcharts, sequence diagrams, class diagrams and more. Diagrams follow the color mode, load when scrolled into view and support copy and fullscreen.

## Enable

Mermaid is disabled by default.

### Install the dependencies

```bash [pnpm]
pnpm add mermaid dompurify
```

```bash [npm]
npm install mermaid dompurify
```

```bash [yarn]
yarn add mermaid dompurify
```

```bash [bun]
bun add mermaid dompurify
```

### Turn on the feature flag

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  extends: ['@movk/nuxt-docs'],

  movkNuxtDocs: {
    mermaid: true
  }
})
```

> [!WARNING]
> See: /en/docs/guide/features
> 
> If 
> 
> mermaid
> 
>  is enabled without the dependencies installed, the build fails and the error lists the packages to install. When it is disabled, 
> 
> mermaid
> 
>  blocks are shown as plain code.

## Usage

Write a diagram in a code block with the `mermaid` language:

````md
```mermaid
flowchart LR
    A[Write Markdown] --> B{Has MDC components?}
    B -->|Yes| C[Render Vue components]
    B -->|No| D[Render prose elements]
    C --> E[Output page]
    D --> E
```
````

- Switches the Mermaid theme with the site's color mode
- Loads `mermaid` and renders only once the diagram enters the viewport
- Sanitizes the output with DOMPurify and runs Mermaid with `securityLevel: 'strict'`
- Shows copy and fullscreen buttons in the top-right corner on hover

### Filename

As with regular code blocks, put a filename in square brackets to show it above the diagram:

````md
```mermaid [ai-chat.mmd]
sequenceDiagram
    ...
```
````

## Diagram types

### Class diagram

`classDiagram` shows types and inheritance:

```mermaid
classDiagram
    class Page {
        +String title
        +String description
        +String path
    }
    class DocsPage {
        +String category
        +Link[] links
    }
    class ReleasePage {
        +String tag
        +Date publishedAt
    }
    Page <|-- DocsPage
    Page <|-- ReleasePage
```

### State diagram

`stateDiagram-v2` shows state transitions:

```mermaid
stateDiagram-v2
    [*] --> Draft
    Draft --> Review: Open PR
    Review --> Draft: Changes requested
    Review --> Published: Merge
    Published --> [*]
```

### Pie chart

`pie` shows proportions:

```mermaid
pie title Example: page types
    "Guide" : 11
    "AI" : 5
    "Writing" : 6
    "Components" : 7
    "Reference" : 14
```

### Gantt chart

`gantt` shows schedules and milestones:

```mermaid
gantt
    title Example release plan
    dateFormat YYYY-MM-DD
    section Development
        Features    :a1, 2026-09-01, 21d
        Docs        :a2, after a1, 14d
    section Release
        Beta        :b1, after a2, 7d
        Stable      :milestone, after b1, 0d
```

### Entity relationship diagram

`erDiagram` shows entities and their relationships:

```mermaid
erDiagram
    COLLECTION ||--o{ PAGE : contains
    COLLECTION {
        string name
        string locale
    }
    PAGE ||--o{ SECTION : has
    PAGE {
        string path PK
        string title
        string category
    }
    SECTION {
        string id PK
        string heading
    }
```

> [!TIP]
> See: https://mermaid.js.org/intro/
> 
> See the Mermaid docs for more diagram types and syntax.

## Styling

Override slot classes under `ui.prose.mermaid` in `app/app.config.ts`:

```ts [app/app.config.ts]
export default defineAppConfig({
  ui: {
    prose: {
      mermaid: {
        slots: {
          root: 'my-8 border-primary/40',
          diagram: 'bg-default p-6'
        }
      }
    }
  }
})
```

Default theme:

```ts [app/app.config.ts]
export default defineAppConfig({
  ui: {
    prose: {
      mermaid: {
        slots: {
          root: 'relative my-5 group border border-muted rounded-md overflow-hidden',
          header: 'flex items-center gap-1.5 border-b border-muted bg-default px-4 py-3',
          filename: 'text-default text-sm/6',
          icon: 'size-4 shrink-0',
          toolbar: 'absolute top-2 right-2 flex gap-1 z-10 opacity-0 group-hover:opacity-100 transition-opacity',
          diagram: 'p-4 flex justify-center bg-elevated overflow-x-auto',
          loading: 'p-4 flex items-center justify-center gap-2 text-sm text-muted',
          error: 'p-4 flex items-center justify-center gap-2 text-sm text-error bg-error/10'
        },
        variants: {
          fullscreen: {
            true: {
              root: 'fixed inset-0 z-50 m-0 rounded-none bg-default flex flex-col',
              diagram: 'flex-1 overflow-auto',
              toolbar: 'opacity-100'
            }
          }
        }
      }
    }
  }
})
```

## API

### Props

```ts
/**
 * Props for the ProseMermaid component
 */
interface ProseMermaidProps {
  /**
   * 图表代码
   */
  code: string;
  /**
   * 图标
   */
  icon?: any;
  /**
   * 文件名
   */
  filename?: string | undefined;
  ui?: Partial<{ root: string; header: string; filename: string; icon: string; toolbar: string; diagram: string; loading: string; error: string; }> | undefined;
}
```

## Changelog

See commit history for [layer/modules/components/prose/Mermaid.vue](https://github.com/mhaibaraai/movk-nuxt-docs/commits/main/layer/modules/components/prose/Mermaid.vue).

---

- [GitHub](https://github.com/mhaibaraai/movk-nuxt-docs/blob/main/layer/modules/components/prose/Mermaid.vue)


## Sitemap

See the full [sitemap](https://docs.mhaibaraai.cn/sitemap.md) for all pages.
