Mighty Practices

Use Mermaid Diagrams to Visualize Complex Concepts

A picture says a thousand words. Use Mermaid diagrams to clearly communicate complex ideas in your documentation.

Context

Markdown is a great format for writing documentation - it's easy to read, easy to write, and easy to version control. However, when it comes to explaining complex concepts, sometimes words alone aren't enough. Visual aids can help convey ideas more clearly and make it easier for readers to understand and retain information.

Mermaid is a simple markdown-like script language for generating charts and diagrams from text. It allows you to create flowcharts, sequence diagrams, class diagrams, state diagrams, Gantt charts, and more, all using a straightforward syntax.

When you combine Mermaid with Markdown, you can create rich, informative documentation that leverages both text and visuals to communicate effectively. What's more, because it's a text-based format, diagrams can be version controlled, reviewed, and easily shared just like any other code or documentation.

Mermaid Basics

The best place to start learning Mermaid is https://mermaid.live/, a live editor where you can experiment with Mermaid syntax and see the results in real time. The site also includes a comprehensive Mermaid documentation that covers all the different types of diagrams you can create and the syntax for each.

At its core, Mermaid diagrams are created using a simple text-based syntax. Here's an example:

flowchart TD
    A[Start Learning Mermaid]-->B[Communicate Well]

When we run it through a Mermaid renderer, it produces the following diagram:

Using Mermaid in Markdown

To create a Mermaid diagram in Markdown, you use a code block with the language set to mermaid. Inside the code block, you write your Mermaid syntax to define the diagram. Here's an example:

### Markdown Example

This example shows how to embed a Mermaid diagram in Markdown.

```mermaid
flowchart TD
    A[Start Learning Mermaid]-->B[Communicate Well]
```

When rendered in Github (either in code, issues, or pull requests), the above code block will display the flowchart diagram embedded in your content. For more information about the Github/Mermaid integration, see the Github Docs on Mermaid, or the Github Developer Skills post on the topic.

Advanced Mermaid

Mermaid supports a variety of diagram types beyond simple flowcharts. Here are a few examples:

Sequence Diagrams:

sequenceDiagram
    Alice->>John: Hello John, how are you?
    John-->>Alice: Great!
    Alice-)John: See you later!

Git Graphs

gitGraph
   commit
   commit
   branch develop
   checkout develop
   commit
   commit
   checkout main
   merge develop
   commit
   commit

ER Diagrams

erDiagram
    CUSTOMER ||--o{ ORDER : places
    ORDER ||--|{ LINE-ITEM : contains
    CUSTOMER }|..|{ DELIVERY-ADDRESS : uses

Architecture Diagrams

architecture-beta
    group api(cloud)[API]

    service db(database)[Database] in api
    service disk1(disk)[Storage] in api
    service disk2(disk)[Storage] in api
    service server(server)[Server] in api

    db:L -- R:server
    disk1:T -- B:server
    disk2:T -- B:db

Best Practices

When using Mermaid diagrams in your documentation, consider the following best practices:

  • Keep it Simple: Avoid overcomplicating your diagrams. Focus on the key concepts you want to convey and keep the diagrams as simple as possible.
  • Know Your Audience: Tailor your diagrams to the knowledge level of your audience. Use terminology and symbols that they will understand.
  • Use Clear Labels: Ensure that all nodes and edges in your diagrams are clearly labeled so that readers can easily understand what they represent.
  • Complement with Text: Use diagrams to complement your written content, not replace it. Provide context and explanations alongside your diagrams to help readers understand their significance.

Help us improve

Your feedback helps us create better resources for teams like yours.

Was this page helpful?

Last updated on