If you've ever needed to include a flowchart, sequence diagram, or architecture overview inside a technical document, you know the pain of switching between a drawing tool and your text editor. Learning how to write diagram codes in markdown syntax solves that problem by letting you describe diagrams using simple text-based code right inside your documents. This means version control, easy collaboration, and no more broken image files. Whether you're documenting a system, explaining a process, or building a wiki, diagram-as-code in markdown keeps everything in one place.
What does it mean to write diagram codes in markdown syntax?
Writing diagram codes in markdown syntax means using a special code block inside a markdown file to describe a visual diagram. Instead of drawing shapes with a mouse, you type structured text that a rendering tool converts into an image. The most common syntaxes supported are Mermaid, PlantUML, and Ditaa.
You start a diagram code block using triple backticks followed by the diagram language name. For example:
```mermaid
graph TD
A[Start] --> B[End]
```
When rendered in a tool that supports it this produces a simple top-down flowchart with two connected nodes. That's the core idea. You write text; it becomes a diagram.
How do you create a flowchart in markdown?
Flowcharts are the most common type of diagram people write in markdown. Mermaid's graph syntax makes this straightforward.
Here's a basic example:
```mermaid
graph LR
A[Login Page] --> B{Valid Credentials?}
B -->|Yes| C[Dashboard]
B -->|No| D[Error Message]
D --> A
```
The direction keyword (LR for left-to-right, TD for top-down) controls layout. Square brackets [] create rectangles, curly braces {} create diamond decision shapes, and pipes |text| label the connecting arrows.
If you're looking for a good way to test these before pasting them into your documentation, an online diagram code generator with live preview can save a lot of trial and error.
How do you make a sequence diagram in markdown?
Sequence diagrams show interactions between actors or systems over time useful for documenting API calls, authentication flows, or message passing.
Using Mermaid syntax:
```mermaid
sequenceDiagram
participant User
participant Server
participant Database
User->>Server: Send login request
Server->>Database: Query user
Database-->>Server: Return user data
Server-->>User: Auth token
```
The ->> arrow represents a solid line (synchronous call), while -->> represents a dashed line (response). You declare participants at the top and then define messages between them in order.
For more complex sequence diagrams with loops, conditions, and notes, refer to the Mermaid sequence diagram documentation.
How do you build a Gantt chart in markdown code?
Gantt charts help with project planning. Mermaid supports them directly in markdown:
```mermaid
gantt
title Project Timeline
dateFormat YYYY-MM-DD
section Design
Wireframes: a1, 2025-01-06, 7d
Mockups: a2, after a1, 5d
section Development
Backend: b1, after a2, 10d
Frontend: b2, after a2, 8d
```
Each task has an ID, a start date or dependency, and a duration. The section keyword groups tasks visually. This is especially handy in project README files or sprint planning docs.
What other diagram types can you write in markdown?
Beyond flowcharts, sequence diagrams, and Gantt charts, Mermaid and PlantUML support several other diagram types:
- Class diagrams for object-oriented design and UML modeling
- State diagrams to represent finite state machines
- Entity-relationship diagrams for database schema visualization
- Pie charts for simple data visualization
- Git graph diagrams to visualize branching and merging
- Architecture/C4 diagrams using PlantUML for system architecture
PlantUML is often preferred for enterprise-level documentation because it supports a wider range of diagram types and has been around longer. If you're deciding between tools, a diagram code editor comparison for system engineers can help you pick the right one for your workflow.
Where do markdown diagram codes actually render?
This is a common point of confusion. Writing the code is one thing seeing the diagram is another. Markdown diagram code blocks render in tools that support them:
- GitHub supports Mermaid diagrams natively in markdown files since 2022
- GitLab supports Mermaid and PlantUML in wikis and markdown files
- Notion supports Mermaid via code blocks
- VS Code with the Mermaid or PlantUML extension, you can preview diagrams while editing
- Hugo/Jekyll static sites with the right plugin, diagram code renders into images at build time
- Obsidian renders Mermaid diagrams natively in notes
If your platform doesn't support rendering, the raw code block will just display as plain text. That's why previewing with a dedicated diagram code editor is useful before committing to a specific platform.
What are the most common mistakes when writing diagram code in markdown?
Diagram code is picky about syntax. Small errors can break the entire render or produce confusing output. Here are the mistakes I see most often:
- Missing or wrong code fence language writing
```codeinstead of```mermaid. The language identifier must match. - Indentation errors Mermaid and PlantUML are sensitive to indentation, especially in sub-graphs and nested blocks.
- Special characters in labels parentheses, quotes, and brackets inside node labels can break parsing. Wrap labels in quotes when in doubt:
A["Label (with parens)"]. - Arrow direction confusion mixing up
-->and-.-or using wrong arrow styles. - Overloading a single diagram cramming too many nodes and connections into one diagram makes it unreadable. Split complex flows into multiple smaller diagrams.
- Forgetting the closing backticks three backticks must open and close the block. Missing the closing pair renders nothing.
Tips for writing cleaner diagram code in markdown
These habits will save you time and frustration:
- Start simple. Get two nodes connected first, then add complexity. Don't write 50 lines and hope it renders correctly on the first try.
- Use descriptive node IDs. Instead of
A,B,C, use short meaningful names likelogin,auth,dash. - Comment your code. Mermaid supports
%%for comments. Use them to explain non-obvious sections. - Keep diagrams focused. One diagram should answer one question or show one flow. If you need to show five flows, make five diagrams.
- Version control your diagrams. One of the main benefits of diagram-as-code is that changes are tracked in git. Use meaningful commit messages when you update a diagram.
- Preview before committing. Use a live preview tool or extension to catch syntax issues before they end up in your repository.
How does Mermaid compare to PlantUML for markdown diagrams?
Both are popular choices, but they differ in important ways:
- Syntax style Mermaid is simpler and more concise. PlantUML is more verbose but more expressive.
- Platform support Mermaid is natively supported on GitHub and GitLab. PlantUML usually requires a server-side renderer or plugin.
- Diagram variety PlantUML supports more diagram types out of the box, including C4 architecture and wireframes.
- Learning curve Mermaid is easier to pick up. PlantUML has a steeper curve but more power for complex documentation.
- Community and ecosystem Both have active communities. Mermaid has grown quickly due to GitHub adoption. PlantUML has been around longer in enterprise settings.
The right choice depends on your platform and your needs. If your docs live on GitHub, Mermaid is the obvious pick. If you need advanced UML support in an enterprise wiki, PlantUML may be better.
How do you embed external images as a fallback?
Sometimes diagram code won't render on your target platform. In those cases, you can generate the diagram image separately and embed it with standard markdown image syntax:

Some workflows combine both approaches: keep the diagram source code in the repo for editing, and generate PNG or SVG output for platforms that don't support live rendering. Tools like the Mermaid CLI let you export diagram code to image files automatically.
Practical next steps: a checklist to start writing diagram code today
- Pick your diagram language. Start with Mermaid if you're on GitHub or GitLab. Use PlantUML if you need advanced UML support.
- Set up a preview tool. Use a diagram code editor or a VS Code extension with live preview.
- Write your first diagram. Start with a simple three-node flowchart using the examples above.
- Test rendering on your platform. Push a markdown file to GitHub or open it in your wiki to confirm the diagram displays correctly.
- Iterate and expand. Add nodes, connections, and styles. Try a sequence diagram or Gantt chart next.
- Document your conventions. If you're working on a team, agree on diagram style, naming, and placement in your project's contributing guide.
Start small. One simple flowchart in your next README is all it takes to get comfortable with diagram code in markdown. Once you see the diagram render from plain text, the workflow clicks and you'll rarely go back to drag-and-drop tools for technical documentation.
Best Diagram Code Editors for Software Architects in 2025
Diagram Code Editor Comparison for System Engineers
Collaborative Uml Diagram Code Editor for Teams
Online Diagram Code Generator with Live Preview - Diagram Code Editor
How to Create a Mermaid Diagram in Markdown Using Mermaid Syntax
Mermaid Gantt Chart Syntax Explained: Complete Guide to Creating Gantt Diagrams