# Keep Writing Mermaid

> Drop in existing Mermaid flowcharts and sequence diagrams for interactive upgrades with strict zero-loss fallback.

You do not need to learn a new syntax to get interactive diagrams. Paste ordinary Mermaid blocks and `aidiagram` upgrades them automatically whenever the **entire** diagram can be faithfully preserved.

## Step 1: Standard Flowchart

Write a normal `flowchart LR`, `TB`, or `TD` block:

```mermaid
flowchart LR
  A[Open pull request] --> B[Review changes]
  B --> C[Maintainer decides whether to merge]
```

## Step 2: Bidirectional & Undirected Connectors

Both arrowheads (`<-->`, `<==>`, `<-.->`) and undirected lines (`---`) are preserved accurately — including under `prefers-reduced-motion: reduce`:

```mermaid
flowchart LR
  Client["Client State<br/>Browser / CLI"] <-->|"Sync"| Server["Server State<br/>API / Store"]
```

## Step 3: Sequence Diagrams with Notes

`sequenceDiagram` supports `participant`/`actor` aliases, `autonumber`, solid/dashed arrows, self-calls, and `Note over / left of / right of`:

```mermaid
sequenceDiagram
  autonumber
  participant A as Author
  participant B as Reviewer
  A->>B: Open pull request
  Note over B: Manual approval required
  B-->>A: Approved & ready
```

## Step 4: Subgraphs & Highlights

Single-level `subgraph` frames and `style` hints upgrade cleanly:

```mermaid
flowchart LR
  subgraph Client["Browser Client"]
    UI["React Shell<br/>Docs + Chat"]
  end
  subgraph Host["Server"]
    G["Auth Guard"] --> A["Agent"]
    G -.->|"/docs"| D["Docs"]
  end
  UI -->|"HTTPS"| G
  A -.->|"stream"| UI
  style G fill:#e0b25a
```

## Step 5: Opt Out or Fall Back Safely

1. **Force classic Mermaid on any diagram** by placing `%% diagram: off` (or `%% visual: off`) at the top of your `mermaid` fence:
   ```text
   %% diagram: off
   flowchart LR
     A --> B
   ```
2. **Switch views at runtime:** Every upgraded Mermaid block in PraisonAIUI includes a **Classic** button in its toolbar.
3. **Zero silent partial conversion:** Unsupported syntax always returns `null` from `mermaidToDiagram()` so the host hands the exact original text (comments intact) to Mermaid.js.

### Supported vs. Fallback Matrix

| Syntax | Behaviour |
|---|---|
| `flowchart` / `graph` (`LR`, `TB`, `TD`, ≤ 14 nodes) | Upgraded interactively |
| `-->`, `-.->`, `==>`, `<-->`, `<==>`, `<-.->`, `---`, `-.-`, `===` | Upgraded faithfully |
| Single-level `subgraph ... end`, `<br/>` title/note split, `&` fan-in/out | Upgraded interactively |
| `sequenceDiagram` (≤ 6 actors), `autonumber`, self-calls, `Note over/left of/right of` | Upgraded interactively |
| `gantt`, `erDiagram`, `quadrantChart`, `stateDiagram`, `pie`, `gitGraph` | Clean fallback to Mermaid.js |
| `RL` / `BT` directions, nested/unclosed subgraphs, > 14 nodes, > 6 actors | Clean fallback to Mermaid.js |
| `alt`, `loop`, `opt`, `par`, `rect`, `box`, `activate`, `click`, `direction`, `linkStyle`, `o--o`, `x--x` | Clean fallback to Mermaid.js |
| Truncated fences while streaming | Clean fallback (no premature replacement) |

## Related pages

- [PraisonAIUI Integration](https://diagram.praison.ai/docs/guides/praisonaiui.md)
- [Graph Layout](https://diagram.praison.ai/docs/layouts/graph.md)
- [Sequence Layout](https://diagram.praison.ai/docs/layouts/sequence.md)
