|
| 1 | +--- |
| 2 | +title: "Mermaid Block Diagrams" |
| 3 | +date: 2024-12-12T22:30:00Z |
| 4 | +draft: false |
| 5 | +description: "Create block diagrams for system architecture with Mermaid" |
| 6 | +tags: ["mermaid", "block", "architecture", "diagram", "system-design", "diagrams"] |
| 7 | +category: "diagrams" |
| 8 | +--- |
| 9 | + |
| 10 | +Block diagrams show system components as blocks with connections. Perfect for visualizing system architecture, data flow, and component relationships. |
| 11 | + |
| 12 | +## Use Case |
| 13 | + |
| 14 | +Use block diagrams when you need to: |
| 15 | +- Show system architecture |
| 16 | +- Visualize component relationships |
| 17 | +- Document data flow |
| 18 | +- Design system components |
| 19 | +- Communicate system structure |
| 20 | + |
| 21 | +## Code (Basic) |
| 22 | + |
| 23 | +````markdown |
| 24 | +```mermaid |
| 25 | +block-beta |
| 26 | + columns 2 |
| 27 | +
|
| 28 | + block:Left |
| 29 | + A[Component A] |
| 30 | + B["Component B"] |
| 31 | + end |
| 32 | +
|
| 33 | + block:Right |
| 34 | + C[Component C] |
| 35 | + D[Component D] |
| 36 | + end |
| 37 | +
|
| 38 | + A --> B |
| 39 | + B --> C |
| 40 | + C --> D |
| 41 | +``` |
| 42 | +```` |
| 43 | + |
| 44 | +**Result:** |
| 45 | + |
| 46 | +```mermaid |
| 47 | +block-beta |
| 48 | + columns 2 |
| 49 | +
|
| 50 | + block:Left |
| 51 | + A[Component A] |
| 52 | + B["Component B"] |
| 53 | + end |
| 54 | +
|
| 55 | + block:Right |
| 56 | + C[Component C] |
| 57 | + D[Component D] |
| 58 | + end |
| 59 | +
|
| 60 | + A --> B |
| 61 | + B --> C |
| 62 | + C --> D |
| 63 | +``` |
| 64 | + |
| 65 | +## Explanation |
| 66 | + |
| 67 | +- `block-beta` - Start block diagram (beta syntax) |
| 68 | +- `columns N` - Optional, defines column layout |
| 69 | +- `block:ID` / `end` - Define a vertical block region with an identifier |
| 70 | +- Inside a block you place regular nodes like `A[Label]` |
| 71 | +- `-->` - Connection between nodes (same as in flowcharts) |
| 72 | + |
| 73 | +## Examples |
| 74 | + |
| 75 | +### Example 1: System Layout with Columns and Space |
| 76 | + |
| 77 | +````markdown |
| 78 | +```mermaid |
| 79 | +block-beta |
| 80 | + columns 1 |
| 81 | +
|
| 82 | + db(("DB")) |
| 83 | + blockArrowId6<[" "]>(down) |
| 84 | +
|
| 85 | + block:ID |
| 86 | + A |
| 87 | + B["A wide one in the middle"] |
| 88 | + C |
| 89 | + end |
| 90 | +
|
| 91 | + space |
| 92 | +
|
| 93 | + D |
| 94 | +
|
| 95 | + ID --> D |
| 96 | + C --> D |
| 97 | +
|
| 98 | + style B fill:#969,stroke:#333,stroke-width:4px |
| 99 | +``` |
| 100 | +```` |
| 101 | + |
| 102 | +**Result:** |
| 103 | + |
| 104 | +```mermaid |
| 105 | +block-beta |
| 106 | + columns 1 |
| 107 | +
|
| 108 | + db(("DB")) |
| 109 | + blockArrowId6<[" "]>(down) |
| 110 | +
|
| 111 | + block:ID |
| 112 | + A |
| 113 | + B["A wide one in the middle"] |
| 114 | + C |
| 115 | + end |
| 116 | +
|
| 117 | + space |
| 118 | +
|
| 119 | + D |
| 120 | +
|
| 121 | + ID --> D |
| 122 | + C --> D |
| 123 | +
|
| 124 | + style B fill:#969,stroke:#333,stroke-width:4px |
| 125 | +``` |
| 126 | + |
| 127 | +### Example 2: Simple System Architecture |
| 128 | + |
| 129 | +````markdown |
| 130 | +```mermaid |
| 131 | +block-beta |
| 132 | + columns 2 |
| 133 | +
|
| 134 | + block:ClientSide |
| 135 | + Client[Web Client] |
| 136 | + end |
| 137 | +
|
| 138 | + block:ServerSide |
| 139 | + API[API Gateway] |
| 140 | + Auth[Auth Service] |
| 141 | + DB[(Database)] |
| 142 | + end |
| 143 | +
|
| 144 | + Client --> API |
| 145 | + API --> Auth |
| 146 | + API --> DB |
| 147 | +``` |
| 148 | +```` |
| 149 | + |
| 150 | +**Result:** |
| 151 | + |
| 152 | +```mermaid |
| 153 | +block-beta |
| 154 | + columns 2 |
| 155 | +
|
| 156 | + block:ClientSide |
| 157 | + Client[Web Client] |
| 158 | + end |
| 159 | +
|
| 160 | + block:ServerSide |
| 161 | + API[API Gateway] |
| 162 | + Auth[Auth Service] |
| 163 | + DB[(Database)] |
| 164 | + end |
| 165 | +
|
| 166 | + Client --> API |
| 167 | + API --> Auth |
| 168 | + API --> DB |
| 169 | +``` |
| 170 | + |
| 171 | +## Block Elements & Styling |
| 172 | + |
| 173 | +- `[Label]` - Rectangle |
| 174 | +- `(Label)` - Rounded rectangle |
| 175 | +- `([Label])` - Stadium shape |
| 176 | +- `[[Label]]` - Double rectangle |
| 177 | +- `[(Label)]` - Cylinder (database) |
| 178 | +- `block:ID` - Logical group of vertically stacked nodes |
| 179 | +- `space` - Adds horizontal space between blocks |
| 180 | +- `style ID ...` - Apply CSS-like style to a node or block |
| 181 | + |
| 182 | +## Notes |
| 183 | + |
| 184 | +- Use `block-beta` for modern block diagrams |
| 185 | +- Combine `columns`, `block:ID`, `space`, and arrows to control layout |
| 186 | +- Connections use `-->` for directed edges |
| 187 | +- Layout is still mostly automatic; you influence it via columns and blocks |
| 188 | + |
| 189 | +## Gotchas/Warnings |
| 190 | + |
| 191 | +- ⚠️ **Indentation**: Block contents must be consistently indented under `block:ID` |
| 192 | +- ⚠️ **IDs**: `block:ID` defines an identifier you can later connect from/to |
| 193 | +- ⚠️ **HTML**: If you use ` ` inside labels, be sure your renderer passes it through correctly |
| 194 | +- ⚠️ **Beta**: `block-beta` is still evolving; check Mermaid docs if syntax changes |
| 195 | + |
0 commit comments