π§ββοΈ Mermaid Diagrams Guide
π§ Overviewβ
Mermaid is a JavaScript-based diagramming and charting tool that lets you create diagrams using simple text syntax. Itβs widely used in documentation, Markdown files, and developer tools.
π Basic Syntaxβ
A Mermaid diagram starts with a code block declaring its type and layout direction.
graph TDβ Top-down layoutgraph LRβ Left-to-right layout
π Types of Diagramsβ
1. Flowchartβ
2. ER Diagram (Entity Relationship)β
ER diagrams define entities and their relationships.
Symbol Meaning Breakdownβ
| Symbol | Meaning |
|---|---|
| | Exactly one |
o | Zero (optional) |
{ or } | Many |
Quick Referenceβ
| Syntax | Relationship |
|---|---|
A ||--|| B | One-to-one |
A ||--o{ B | One-to-many |
A }o--|| B | Many-to-one |
A }o--o{ B | Many-to-many |
Example ER Diagram:β
3. Sequence Diagramβ
4. Class Diagramβ
5. State Diagramβ
6. Gantt Chartβ
βοΈ Styling & Customizationβ
Labels To add text on lines or arrows, use the pipe syntax:A -->|Yes| B
Shapes (Flowcharts) Change node appearances by closing your declarations with different bracket types:
-
[Text] -> Rectangle
-
(Text) -> Rounded Rectangle
-
{Text} -> Diamond
-
((Text)) -> Circle
π§ Best Practicesβ
-
Consistent Naming: Use UPPER_CASE for ER diagrams (e.g., USER_ACCOUNT).
-
No Spaces: Avoid spaces in entity names. Use underscores instead (e.g., user_account).
-
KISS: Keep diagrams simple and readable. Break massive diagrams into smaller, connected ones.
-
Clarity: Add labels on your connecting lines to clarify relationships.
β οΈ Common Mistakesβ
-
β Using spaces in ER entity names
- βοΈ Fix: Use USER_ACCOUNT instead of User Account.
-
β Forgetting diagram type
- βοΈ Fix: Always define the diagram type at the top (like erDiagram or flowchart TD).
-
β Incorrect relationships
- βοΈ Fix: Match symbols to real-world data logic.
π§© Tips for Docs Platforms (MDX)β
-
Wrap your Mermaid code in standard triple backticks with the language set to mermaid.
-
Ensure your documentation platform supports Mermaid natively or via plugin. For example, Docusaurus requires the @docusaurus/theme-mermaid theme added to your config file.
-
Keep diagrams modular for better reusability across pages!