Render Mermaid diagrams as SVGs or ASCII art
Render Mermaid diagrams as SVGs or ASCII art

### From Code to Clarity: Rendering Mermaid Diagrams as SVG and ASCII Art
Mermaid.js has revolutionized how we create and share diagrams. By allowing us to define complex flowcharts, sequence diagrams, and Gantt charts using simple, Markdown-like text, it embodies the “diagrams as code” philosophy. This means our diagrams can be version-controlled, easily edited, and embedded anywhere.
But what happens when you need a static, portable version of your diagram? A live-rendering plugin isn’t always available. You might need an image for a presentation, a static website, or even a command-line interface. This is where rendering your Mermaid diagrams into standard formats like SVG and ASCII art becomes essential.
Let’s explore how to transform your Mermaid text into these two powerful formats.
—
### The Professional Standard: Rendering to SVG
SVG (Scalable Vector Graphics) is the perfect format for web and print. It’s a vector image, meaning it can be scaled to any size without losing quality, and its text remains searchable and selectable. It’s the ideal choice for documentation, static site generators, and high-quality visuals.
The most robust way to generate SVGs from Mermaid code is by using the official Mermaid CLI.
#### Using the Mermaid CLI (`mmdc`)
The Mermaid team maintains a command-line interface, `@mermaid-js/mermaid-cli`, specifically for this purpose.
**1. Installation:**
First, you’ll need Node.js and npm installed. Then, install the CLI globally with this command:
“`bash
npm install -g @mermaid-js/mermaid-cli
“`
**2. Creating Your Diagram File:**
Save your Mermaid diagram syntax in a file with an `.mmd` or `.mermaid` extension. For example, create a file named `flowchart.mmd`:
“`mermaid
graph TD;
A[Start] –> B{Is it working?};
B — Yes –> C[Great!];
B — No –> D[Check logs];
D –> B;
C –> E[End];
“`
**3. Rendering the SVG:**
Now, run the `mmdc` command, specifying your input file (`-i`) and your desired output file (`-o`):
“`bash
mmdc -i flowchart.mmd -o flowchart.svg
“`
That’s it! You will now have a crisp, scalable `flowchart.svg` file in the same directory, perfect for embedding in your website, documentation, or any other platform that supports images. The CLI can also output other formats like PNG and PDF if needed.
—
### The Retro Charm: Rendering to ASCII Art
Sometimes, a full-fledged image is overkill or simply not supported. Imagine wanting to display a diagram in a terminal, within a code comment, or in a plain text email. This is where ASCII art shines. It provides a visual representation using nothing but text characters.
While the official Mermaid CLI doesn’t natively support ASCII output, the community has created tools to fill this gap. One excellent option is `mermaid-to-ascii`.
#### Using `mermaid-to-ascii`
This tool functions as a simple command-line utility to convert Mermaid diagrams directly into text-based art.
**1. Installation:**
Like the official CLI, this is an npm package. Install it globally:
“`bash
npm install -g mermaid-to-ascii
“`
**2. Rendering the ASCII:**
Using the same `flowchart.mmd` file from before, you can generate the ASCII art directly in your terminal. Just point the tool at your input file:
“`bash
mermaid-to-ascii -i flowchart.mmd
“`
**3. The Output:**
The command will print the following text-based diagram directly to your console:
“`
+——-+
| Start |
+——-+
|
v
+—————–+
| Is it working? |
+—————–+\
| | \
| Yes | No \
| | \
v v |
+——-+ +———-+
| Great!| |Check logs|
+——-+ +———-+
| |
| |
v /
+—–+ /
| End | /
+—–+ /
“`
You can now copy and paste this text anywhere you need it. It’s a fantastic way to keep your documentation light and universally compatible.
### A Powerful Workflow
By mastering these tools, you can integrate diagram generation directly into your development workflow.
* **Version Control:** Keep your `.mmd` source files in your Git repository.
* **Automation:** Add a script to your `package.json` or `Makefile` to automatically build all SVG diagrams for your project’s documentation whenever you make a change.
* **CI/CD:** Your continuous integration pipeline can regenerate and deploy diagrams as part of your build process, ensuring your documentation is never out of date.
Whether you need a pixel-perfect SVG for your company’s knowledge base or a simple ASCII flowchart for a `README.md` file, Mermaid’s ecosystem provides the tools to get it done. By moving beyond live editors and embracing command-line rendering, you unlock the full potential of “diagrams as code.”
