Write diagram syntax

Mermaid syntax is plain text with a defined structure. Once you understand the basics, you can build and edit any diagram without touching the AI panel — and you’ll know exactly what to change when a diagram needs updating.

This guide covers flowchart syntax in depth. For other diagram types, the same principles apply; the OSS docs have full syntax references for every type. Browse the OSS syntax docs →

How syntax works

Every diagram starts with a type declaration that tells Mermaid what kind of diagram to render:

flowchart LR
sequenceDiagram
erDiagram
mindmap
gantt

Everything that follows defines the content — nodes, connections, labels, styles, and groupings. The diagram renders in real time as you type in the editor.

Flowchart direction

For flowcharts, the direction comes right after the type declaration:

DirectionCodeWhen to use
Left to rightLRProcesses, workflows, timelines
Top to bottomTD or TBHierarchies, org charts, decision trees
Bottom to topBTReverse hierarchies
Right to leftRLLess common — specific layout needs

Direction has a bigger impact on readability than most people expect. The same diagram can be clear or cluttered depending on which way it flows. If a layout looks off, try changing the direction before adjusting anything else.

Nodes

Nodes are the building blocks of a flowchart. Shape carries meaning — use it deliberately.

SyntaxShapeWhen to use
A[Label]RectangleDefault — steps, actions, tasks
A(Label)Rounded rectangleSofter steps, start and end points
A{Label}DiamondDecisions with branching paths
A((Label))CircleEvents, endpoints, loop connectors
A[/Label/]ParallelogramInput or output
A[(Label)]CylinderDatabases, data stores
A[[Label]]SubroutineSubprocess or referenced flow
A>Label]AsymmetricFlags, tags

Node IDs and labels are separate. The ID is used to reference the node in connections — the label is what renders in the diagram. Define the label once and use the ID everywhere else:

flowchart LR A[Content brief] --> B[Draft] B --> C{Ready for review?} C -->|Yes| D[Send to reviewer] C -->|No| B D --> E[Published]

Tip: Don't use the word end in all lowercase as a node ID or label — it's a reserved word and breaks the parser. Use End, END, or a different word entirely.

Connections

Connection type carries meaning. Use the right one for what you’re showing.

SyntaxRenders asWhen to use
—>ArrowDefault directional flow
---LineRelationship without direction
-.->Dotted arrowOptional, async, or conditional path
==>Thick arrowPrimary or critical path
—oCircle endAggregation or “zero or many” relationship
—xCross endTermination or blocking step
<—>Bidirectional arrowTwo-way relationship

Tip: If a connection label starts with the letter o or x, add a space before it or capitalize it. -->|ok| renders as a circle edge — use -->| ok| or -->|Ok| instead.

Subgraphs

Group related nodes into a labeled section using subgraph. Useful for showing stages in a process, grouping by system or team, or isolating a subprocess from the main flow.

flowchart LR subgraph Intake A[Request received] --> B{Complete?} B -->|No| C[Request more info] end subgraph Review D[Assign reviewer] --> E{Approved?} E -->|Yes| F[Publish] E -->|No| G[Return to author] end B -->|Yes| D

Subgraphs can be connected to nodes outside them using the subgraph ID. You can also set direction inside a subgraph independently from the parent diagram.

Code snippets

The snippets panel in the editor has pre-written blocks for common patterns — a useful shortcut when you know the shape you want but not the exact syntax. Click Code snippets in the left panel to browse them, then click any snippet to drop it into the editor.

Use the diagram documentation panel

While editing, click Diagram documentation in the left panel to open the full syntax reference for the diagram type you’re working on in a new tab — without losing your place in the editor. Useful when you’re working with a less familiar diagram type or need to look up a specific property.

Common questions

  • The diagram isn’t rendering. There’s a syntax error somewhere. Mermaid highlights the line causing the issue — check for mismatched brackets, missing arrows, or node IDs that don’t match. You can also open the Mermaid AI panel and click Fix with AI to let it identify and repair the issue automatically.

  • Labels with special characters aren’t rendering correctly. Wrap the label in quotes: A["Label with (parentheses) and, commas"]. This applies to commas, semicolons, and any character Mermaid might try to parse as syntax.

  • My subgraph connections aren’t working. Make sure you’re referencing the correct node IDs, not the subgraph label. Nodes inside a subgraph are still referenced by their own IDs.

  • The word “end” is breaking my diagram. Capitalize it: End or END. Lowercase end is reserved.

What’s next?