# Mermaid Syntax Reference Mermaid diagrams render in presenterm with `+render` attribute: ````markdown ```mermaid +render flowchart LR A --> B ``` ```` ```` ## Flowcharts ### Directions | Code | Meaning | | ---- | ------- | | `LR` | Left to Right | | `RL` | Right to Left | | `TD` / `TB` | Top to Down | | `BT` | Bottom to Top | ### Node Shapes | Shape | Syntax | Use Case | | ---- | ------ | -------- | | Rectangle | `A[text]` | Default | | Rounded | `B(text)` | Rounded corners | | Stadium | `C([text])` | Programs | | Cylinder | `D[(text)]` | Database | | Circle | `E((text))` | Endpoints | | Diamond | `F{text}` | Decisions | | Hexagon | `G{{text}}` | Processes | | Parallelogram | `H[/text/]` | Input/Output | ### Connections ```markdown A --> B # Arrow A --- B # Line A -.-> B # Dotted A ==> B # Thick A <--> B # Bidirectional A -- text --> B # Labeled ```` ### Subgraphs ```markdown subgraph producers P1[App 1] P2[App 2] end subgraph kafka[Kafka] B1[(Broker 1)] B2[(Broker 2)] end P1 --> B1 P2 --> B2 ``` ### Pitfalls - The word `end` as node name breaks flowcharts — use `e[end]` - No space between ID and bracket: `A[text]` not `A [text]` - Parentheses need HTML entities: `#40;` for `(`, `#41;` for `)` - Theme engine only accepts hex colors, not color names ## Sequence Diagrams ````markdown ```mermaid +render sequenceDiagram participant C as Client participant G as Gateway participant S as Service participant DB as Database C->>G: HTTP Request G->>S: Forward activate S S->>DB: Query DB-->>S: Results deactivate S S-->>G: Response G-->>C: HTTP Response ``` ```` ```` ### Arrows | Arrow | Meaning | | ----- | ------- | | `->>` | Solid | | `-->>` | Dotted | | `-x` | Cross | | `-)` | Async | ### Blocks `loop`, `alt`/`else`, `opt`, `par`/`and`, `critical`, `break` ### Activation `activate`/`deactivate` or `+`/`-` suffixes. ## Class Diagrams ```markdown ```mermaid +render classDiagram class KafkaProducer { +Properties config +send(ProducerRecord) Future +close() void } class ProducerRecord { +String topic +K key +V value } KafkaProducer --> ProducerRecord : creates ```` ```` ### Visibility | Symbol | Meaning | | ------ | ------- | | `+` | Public | | `-` | Private | | `#` | Protected | | `~` | Package | ### Relationships `<|--` inheritance, `*--` composition, `o--` aggregation, `-->` association ## State Diagrams ```markdown ```mermaid +render stateDiagram-v2 [*] --> Created Created --> Running : start() Running --> Paused : pause() Paused --> Running : resume() Running --> Stopped : stop() Stopped --> [*] ```` ```` ## Entity-Relationship Diagrams ```markdown ```mermaid +render erDiagram TOPIC ||--o{ PARTITION : contains PARTITION ||--o{ MESSAGE : stores TOPIC { string name PK int partitions } ```` ```` ### Cardinality | Symbol | Meaning | | ------ | ------- | | `\|\|` | Exactly one | | `o\|` | Zero or one | | `}\|` | One or more | | `o{` | Zero or more | ## Gantt Charts ```markdown ```mermaid +render gantt title Project Schedule dateFormat YYYY-MM-DD section Design Research :done, d1, 2024-01-01, 7d Design :active, d2, after d1, 5d section Build Implementation :done, b1, after d2, 10d Testing :active, b2, after b1, 5d ```` ```` ## Pie Charts ```markdown ```mermaid +render pie title Distribution "Category A" : 40 "Category B" : 35 "Category C" : 25 ```` ```` ## Theming ### In-Block Theme ```markdown ```mermaid +render %%{init: {'theme': 'dark'}}%% flowchart LR A --> B ```` ```` ### Built-in Themes `default`, `neutral`, `dark`, `forest`, `base` ### Custom Variables (base theme) ```markdown %%{init: {'theme': 'base', 'themeVariables': {'primaryColor': '#ff0000'}}}%% ```` ### Terminal Sizing Tips - Keep diagrams to 5-10 nodes - Use `LR` direction for wide terminals - Use `%%{init: {'theme': 'dark'}}%%` on dark themes