Skip to content
whiteboard.tools
  • Mermaid
  • Diagrams as code

Mermaid or a drawing? Keep both, and know which one is the source

When diagrams-as-code is the right home for a diagram, when it needs to become a drawing, and how to convert Mermaid without losing the original.

By 4 min read

Mermaid flowchart code on the left turning into a hand-drawn, rearranged flowchart on the right

Mermaid is one of the best things to happen to engineering documentation. A flowchart that lives in a Markdown file, renders on GitHub and changes in the same pull request as the code is a flowchart that stays true.

It also has a ceiling. The layout engine decides where everything goes, and when the automatic layout puts the most important step in a corner with crossing arrows all around it, your options are limited to reordering lines and hoping. At some point a diagram stops being documentation and becomes something you present, annotate and argue over. That is when it needs to become a drawing.

The trick is to do that without losing the text version.

When a diagram belongs in Mermaid

Keep a diagram as Mermaid code when most of these are true:

  • It sits next to code, in a README, an architecture decision record or a design doc in the repository.
  • It changes when the code changes, and you want those changes reviewed in a diff.
  • The reader cares about structure (what connects to what), not layout.
  • It has fewer than about twenty nodes. Past that, automatic layout tends to produce long crossing edges that no amount of reordering fixes.

Sequence diagrams are a particularly good fit. Their layout is a timeline, so there is not much the renderer can get wrong, and they are tedious to draw by hand.

When it needs to become a drawing

Convert a diagram into editable shapes when:

  • You are presenting it, and the layout needs to lead the eye in a particular order.
  • You want to annotate it: circle a bottleneck, add a sticky note with an open question, cross out a component you plan to remove.
  • People who do not read Mermaid need to edit it.
  • The automatic layout is fighting you. Rearranging six boxes by hand takes thirty seconds. Coaxing a layout engine into the same result can take an afternoon.

Converting without retyping

Redrawing a twenty-node flowchart from scratch is slow and error-prone, and you will miss an edge. Converting is better: each Mermaid node becomes a real shape with its label, and each edge becomes an arrow that stays attached when you move things around.

Take this flowchart:

flowchart TD
  A[Visitor lands on pricing] --> B{Has an account?}
  B -- yes --> C[Sign in]
  B -- no --> D[Create account]
  C --> E[Choose a plan]
  D --> E
  E --> F{Payment succeeds?}
  F -- yes --> G[Activate subscription]
  F -- no --> H[Show card error]
  H --> E

Paste it into the Mermaid to Excalidraw converter and you get rectangles for the steps, diamonds for the two decisions, and bound arrows carrying the "yes" and "no" labels. From there you can drag the error branch to the side so the happy path runs straight down the middle, which is exactly the kind of change Mermaid's layout will not make for you.

Flowcharts, sequence diagrams, class diagrams, ER diagrams and state diagrams all convert to shapes. Other Mermaid types, such as Gantt charts and pie charts, are not built from boxes and arrows in the same way, so the converter places them as an image instead. If you only need a clean rendered image of those, the Mermaid editor previews any diagram type live and exports SVG or PNG.

Decide which version is the source

This is the part teams skip, and it is the reason diagrams drift. After converting, you have two versions. Pick one to be the source of truth and write it down.

Mermaid stays the source when the diagram describes something that changes with the code. Treat the drawing as a snapshot for one presentation or one review. When the presentation is over, the drawing is done; the next change happens in Mermaid, and you convert again if you need another snapshot.

The drawing becomes the source when the diagram has outgrown text: it has annotations, deliberate layout, colour that means something. Delete the Mermaid block, or replace it with a link to the exported SVG, so nobody updates the old version by mistake.

What does not work is keeping both alive and editing whichever one is closer at hand. Within a month they disagree, and nobody knows which one is right.

Small things that make conversions cleaner

Keep labels short in the Mermaid source. Long labels make wide boxes, and wide boxes make crowded layouts in both versions. "Validate card" is better than "Validate the customer's card details with the provider".

Use subgraphs sparingly. A subgraph comes across as a labelled box around its nodes, which is useful, but boxes nested three deep are hard to rearrange afterwards.

Convert late. Get the structure right in Mermaid, where adding a node is one line, and convert only when you are ready to work on layout and annotation.

If you do not know Mermaid yet

You do not need it to get a flowchart from text. The text to flowchart generator takes plain lines like these:

(Start) -> Enter email -> Valid address?
Valid address? -> Send magic link: yes
Valid address? --> Show error: no

Round brackets make a start or end shape, a trailing question mark makes a decision, -> is a solid arrow, --> is dashed, and the text after a colon labels the arrow. It can also give you the equivalent Mermaid code, which is a gentle way to learn the syntax by example.

Try it on a board

More articles