KevsRobots Learning Platform
By Kevin McAleer, 5 Minutes
The easiest place to learn Mermaid is the free Mermaid Live Editor at mermaid.live. There’s nothing to install and you don’t need an account.
The screen has two main parts:
If your code has a mistake, the diagram stops updating and an error appears under the code. Fix the mistake and it springs back to life. The editor also remembers your diagram in the page address, so you can bookmark it or send the link to a friend.
Let’s draw a tiny flowchart for a robot that avoids obstacles. Type this into the editor:
flowchart TD
%% A robot that backs away from walls
start([Power on]) --> check{Wall ahead?}
check -- Yes --> reverse[Reverse and turn]
check -- No --> forward[Drive forward]
reverse --> check
forward --> check
Here’s what it draws:
flowchart TD
%% A robot that backs away from walls
start([Power on]) --> check{Wall ahead?}
check -- Yes --> reverse[Reverse and turn]
check -- No --> forward[Drive forward]
reverse --> check
forward --> check
Every Mermaid diagram follows the same pattern:
flowchart, and TD means “top down”. Other diagrams start with words like sequenceDiagram, stateDiagram-v2 or pie.%% are comments. Mermaid ignores them, so use them to leave notes for yourself.Indenting the lines isn’t required, but it makes your diagram much easier to read.
Look at check{Wall ahead?}. The word check is the node’s ID, a short name you use to refer to it. The text in the brackets, Wall ahead?, is its label, the words shown in the box. Once you’ve given a node a label, you only need its ID afterwards, like reverse --> check.
If you only write an ID, such as Sensors, Mermaid uses the ID as the label too.
To add a diagram to a GitHub README, an Obsidian note or a page on your own site, wrap it in a code block and set the language to mermaid:
# My Obstacle Avoiding Robot
Here's how it decides where to go:
```mermaid
flowchart TD
check{Wall ahead?} -- Yes --> reverse[Reverse and turn]
check -- No --> forward[Drive forward]
```
When GitHub or Obsidian shows that file, the code block is replaced by the drawn diagram. Anyone reading the raw file still sees readable text, which is a nice bonus. If you’re new to Markdown, the Markdown course covers code blocks and much more.
The Actions panel in the Live Editor lets you:
We’ll look at exporting in more detail in the Exporting and Sharing lesson.
forward to Drive forward slowly, without changing its ID.forward: forward --> battery{Battery low?}, then connect battery back to check for “No” and to a new stop[Stop and beep] node for “Yes”.flowchart TDWhy: Mermaid reads the first line to decide which rules to use for the rest of the diagram
Check and check are different nodesWhy: Mermaid treats every new ID as a new node
%% at the very start of the comment, on its own line%% are treated as commentsQuick quiz 1 of 3
What does the first line of a Mermaid diagram do?
flowchart TD or sequenceDiagram choose the diagram type, and with it the rules for the rest of the code.Quick quiz 2 of 3
In check{Wall ahead?}, what is check?
check is the ID. Wall ahead? is the label that appears in the diagram.Quick quiz 3 of 3
How do you write a comment in Mermaid?
%%.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments