KevsRobots Learning Platform
By Kevin McAleer, 6 Minutes
There are two ways to get a Mermaid diagram in front of people:
Let’s look at both.
Add a mermaid code block to any Markdown file, such as your project’s README.md, and it’s drawn automatically when someone views the file:
## How it works
```mermaid
flowchart LR
sensor[Distance sensor] --> pico[Pico W] --> motors[Motors]
```
The same works in GitHub issues, pull requests and discussions, which is very handy for explaining a bug or a proposed change. Because the diagram is text, it’s version-controlled with the rest of your code; when you change the wiring, update the diagram in the same commit. The Git course shows you how.
Type the same code block into any note. It’s drawn in reading view and in live preview as soon as you move the cursor out of the block. It’s a lovely way to keep diagrams alongside your project notes; see the Obsidian course for more.
In the Mermaid Live Editor, your whole diagram is stored in the page address. Copy the address (or use the editor’s share option) and anyone who opens it sees your diagram and can edit their own copy. Your original doesn’t change.
The Live Editor can download your diagram as:
PNG or SVG? If you can, choose SVG. It scales without going blurry, the files are tiny, and the text stays crisp. Use PNG when the place you’re putting it doesn’t accept SVG.
If you have lots of diagrams, or want them rebuilt automatically, use mermaid-cli. It’s a command-line tool called mmdc that turns Mermaid files into images. You’ll need Node.js installed first.
Install it:
npm install -g @mermaid-js/mermaid-cli
Save a diagram in a file ending .mmd, such as robot.mmd:
flowchart LR
sensor[Distance sensor] --> pico[Pico W] --> motors[Motors]
Then turn it into an image:
mmdc -i robot.mmd -o robot.svg
Change the ending of the output file to get a different format:
mmdc -i robot.mmd -o robot.png
mmdc -i robot.mmd -o robot.pdf
A few useful options:
| Option | What it does | Example |
|---|---|---|
-t |
Choose a theme | -t dark |
-b |
Background colour | -b transparent |
-w |
Width in pixels | -w 1600 |
-s |
Scale (for sharper PNGs) | -s 2 |
mmdc -i robot.mmd -o robot.png -t dark -b transparent -s 2
mermaid-cli can also read a Markdown file, turn every mermaid code block into an image, and write a new Markdown file that links to those images:
mmdc -i build-guide.template.md -o build-guide.md
That’s handy for documentation sites that don’t support Mermaid themselves.
If you don’t want to install it globally,
npx -p @mermaid-js/mermaid-cli mmdc -i robot.mmd -o robot.svgdownloads and runs it in one go. mermaid-cli uses a hidden copy of the Chrome browser to draw the diagram, so the first run downloads that too and can take a minute.
README.md. Check it’s drawn on the repository’s page.dark theme.mmdc: command not foundnpm install -g @mermaid-js/mermaid-cli, or use the npx command aboveWhy: mmdc is part of the mermaid-cli package, which isn’t installed with Node.js by default
Why: mermaid-cli draws diagrams using a headless Chrome browser, which it downloads separately
mermaidQuick quiz 1 of 3
Why is SVG usually a better export format than PNG?
Quick quiz 2 of 3
Which command turns robot.mmd into a PNG image?
-i is the input file and -o the output file; the ending sets the format.Quick quiz 3 of 3
What’s the main advantage of putting the Mermaid code in a README instead of an image?
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments