Create your own
Lesson illustration

Mastering Mermaid Diagrams: Creation and Troubleshooting

In our previous lesson, we established a mental framework for choosing the right diagram to answer a specific question, distinguishing between views of a system's structure, behavior, and architecture. Now that you have a map of what to draw and why, we'll dive into the practical mechanics of how to draw it using Mermaid.

This lesson focuses on the core skill of bringing a diagram to life from text. You will learn to render, validate, and troubleshoot basic Mermaid diagrams. As an experienced developer, you're accustomed to the "code -> compile -> debug" cycle. We'll apply a similar process here, treating our diagrams as code. We will explore two essential environments: the browser-based Mermaid Live Editor for rapid prototyping and debugging, and your local development environment for integrating diagrams directly into Markdown documentation.

The "Diagrams as Code" Philosophy

Before we write any syntax, let's consider why a tool like Mermaid is so powerful for developers. You manage source code, configuration files, and scripts as plain text under version control. The "diagrams as code" approach extends this familiar and robust workflow to visual documentation. Instead of using a graphical tool to create a static image file that is difficult to version or review, you define the diagram in a text-based language.

This approach brings several advantages:

  • Versionability: Changes can be tracked via git diff.
  • Collaboration: Diagrams can be reviewed in pull requests, just like code.
  • Maintainability: It's far easier to update a few lines of text than to redraw a complex diagram from scratch.
  • Integration: Diagrams live alongside the code and documentation they describe.

To see this philosophy in action, let's watch a short video from a developer's perspective.

Diagrams as Code with Mermaid, GitHub, and Visual Studio Code

The video "Diagrams as Code with Mermaid, GitHub, and Visual Studio Code" from the DevOps & AI Toolkit channel provides an excellent overview of this workflow. Watch the first few minutes to understand the motivation behind using Mermaid.

Focus on the segment from the introduction, where the presenter explains why a developer would want to create diagrams as code and how Mermaid fits into a standard toolchain (Git, VS Code, GitHub).

The Mermaid Live Editor: Your Sandbox

The fastest way to start learning Mermaid, test syntax, and debug issues is with the Mermaid Live Editor. It's a web-based tool that provides an instant preview of your diagram as you type.

The editor is typically split into a few key panels:

  • Code: Where you write your Mermaid syntax.
  • Preview: Where the rendered diagram appears.
  • Configuration: For adjusting themes and other rendering options.
  • Actions: For saving, exporting, or sharing your diagram.

When your syntax is incorrect, the editor will usually display an error message, making it an invaluable tool for validation.

A common sight when learning: the Mermaid Live Editor indicating "Invalid syntax." This immediate feedback is crucial for troubleshooting.

Let's watch a brief walkthrough of the editor's interface and basic capabilities.

Mermaid - Flow Chart & Diagram Tools | Part - 1 | Introduction & Overview

The video "Mermaid - Flow Chart & Diagram Tools | Part - 1" from Code Analytics offers a clear tour of the Live Editor.

Please watch the following segments: Editor Interface: A quick walkthrough of the Code, Output, Samples, and Action sections. Styling and Comments: This shows how to apply simple styles and add comments, which is useful for annotation. Downloading the Diagram: A demonstration of how to export your work as a PNG file.

Now, it's your turn to get hands-on.

Exercise 1: Validate and Troubleshoot in the Live Editor

  1. Open the Mermaid Live Editor in a new browser tab.
  2. Clear any existing text in the Code pane.
  3. Copy and paste the following simple flowchart code:
You should see a diagram appear instantly in the `Preview` pane.

4. Now, let's practice troubleshooting. Intentionally introduce an error. For example, change flowchart TD to flowchat TD. Observe the error message that appears below the code pane.
5. Correct the error and try another one. Change an arrow --> to ->. Notice how the diagram might render partially or with a different style, and the editor might flag an issue.
6. Experiment with the syntax. Try changing the text in the brackets or the labels on the arrows. This direct feedback loop is the fastest way to learn.

For a more detailed reference on using the editor, you can consult the official documentation.

Mermaid User Guide

This section of the Mermaid User Guide provides a quick summary of the Live Editor's key features.

Read the section titled "Using the Mermaid Live Editor" to reinforce your understanding of its panels and capabilities, such as Code, Preview, Configuration, and Actions.

Rendering in Markdown: The Developer Workflow

While the Live Editor is great for learning, in practice, you'll most often embed diagrams directly into Markdown files (.md). This is how you create rich documentation in README.md files on GitHub, in wikis, or in static site generators.

The standard syntax is a fenced code block with the language identifier mermaid.

```mermaid
graph TD
  A --> B
```

Many platforms, including GitHub and GitLab, have native support for this and will automatically render the diagram. For a seamless local development experience, you can configure your code editor to do the same. Since you work as a front-end developer, we'll focus on Visual Studio Code.

A typical developer setup showing Mermaid code in a `.md` file on the left and the rendered diagram preview on the right.

To enable this preview in VS Code, you usually need to install an extension. The following guide walks you through the process.

How to Use Mermaid in Visual Studio Code: A Step-by-Step Guide for Developers – JimmyIoT

This article by Jimmy Wong provides a clear, step-by-step guide for setting up Mermaid in VS Code.

First, follow the instructions in Step 1 to install the "Markdown Preview Mermaid Support" extension. Then, read through Step 2 to understand the process of creating a Markdown file and opening the preview pane. You don't need to read Step 3 for now, as we'll cover specific diagram types in later lessons.

Exercise 2: Render a Diagram in a Local Markdown File

  1. Following the guide above, ensure you have the recommended VS Code extension installed.

  2. Create a new file in VS Code named test-diagram.md.

  3. Add the following content to the file. This is a sequence diagram, which we'll study in detail later. For now, we're just using it to test the rendering.

    # My First Local Diagram
    
    Here is a sequence diagram of a basic API call:
    
    ```mermaid
    sequenceDiagram
        participant User
        participant Frontend
        participant Backend
    
        User->>Frontend: Clicks 'Load Data'
        Frontend->>Backend: GET /api/data
        Backend-->>Frontend: [Data Payload]
        Frontend-->>User: Displays data
    ```
    
  1. Open the Markdown preview in VS Code (typically using the shortcut Ctrl+Shift+V or Cmd+Shift+V).
  2. You should see your heading, the paragraph, and the rendered sequence diagram. If the diagram doesn't appear, you're ready for the final step: troubleshooting.

A Troubleshooting Checklist

When a diagram doesn't render, the cause almost always falls into one of two categories: a syntax error in your Mermaid code or a problem with the rendering environment.

  1. Validate Your Syntax: Copy your Mermaid code (the part between the ```mermaid fences) and paste it into the Mermaid Live Editor. If it doesn't render there, your syntax is the problem. Correct it in the editor and then paste it back into your Markdown file.
  2. Check Your Environment: If the syntax is valid in the Live Editor but not in your local preview, the issue is with your setup.

The article we used for setup also contains an excellent troubleshooting guide.

How to Use Mermaid in Visual Studio Code: A Step-by-Step Guide for Developers – JimmyIoT

Let's revisit the article to review its troubleshooting section.

Read through Step 4. Pay close attention to the common causes: incorrect code block formatting (e.g., extra spaces) and issues with the VS Code extension itself.

Conclusion

In this lesson, you've moved from theory to practice, acquiring the fundamental skills to create and display diagrams from text. We've established a workflow that should feel natural for a developer, treating diagrams as code that can be written, validated, and versioned.

Here are the key takeaways:

  • Diagrams as Code: Mermaid allows you to define diagrams in plain text, enabling them to be versioned, reviewed, and maintained alongside your software projects.
  • The Live Editor is for Validation: Use the Mermaid Live Editor as your primary tool for learning syntax, rapid prototyping, and debugging syntax errors.
  • Markdown is for Integration: Embed diagrams into your documentation using ```mermaid fenced code blocks for rendering in platforms like GitHub and local previews in editors like VS Code.
  • A Two-Step Troubleshooting Process: When a diagram fails, first validate the syntax in the Live Editor. If the syntax is correct, then investigate your rendering environment (e.g., your VS Code extension or platform support).

In our next lesson, we will begin our deep dive into specific diagram types. We'll start with the "Requirements and Business Workflows" module, where you will learn to use Mermaid's flowchart syntax to create approximations of UML use-case and activity diagrams to capture system requirements and business processes.

Can't find a good explanation? Sign up and we'll make it for you

Sign up