[DH] Converting Mermaid Diagrams to Image with mermaid-cli
Table of contents
Overview
I usually work on upcoming Hashnode articles on my local machine in Markdown. And I am a big fan in using Mermaid.js diagrams to illustrate workflows in the form of flowcharts and other available format.
Hashnode editor currently do not support the rendering of Mermaid diagrams.
Tools to help
I use Mermaid CLI tool, mmdc
to help convert the Mermaid diagrams in my Markdown documents in place.
This reduces the manual effort required to take screenshots of the Mermaid diagrams I have crafted while retaining the correct position in the document.
This also makes it easier to reference when I transfer the article to Hashnode itself.
Installation
Install the NPM package at the global level, so that you can access the
mmdc
commandPlease note that the mermaid-cli should be > v8.9.1 or at least v10.9.1 at the time of writing.
npm install -g @mermaid-js/mermaid-cli
mmdc --version
Usage
Referencing to the solution from this Stack Overflow post. These are the commands I used to convert my Mermaid diagrams into images while creating a copy of the Markdown document using these images.
Convert Mermaid diagrams into images (No Markdown document)
mmdc
will prefix the images with incremental numercial value if there are more than 1 diagram in the Markdown document.
mmdc -i <MARKDOWN_DOC_FILENAME>.md -o IMAGE_NAME_PREFIX.png
- Convert Mermaid diagrams into images with Markdown document using them
mmdc -i <MARKDOWN_DOC_FILENAME>.md --outputFormat=png \
-o <ANOTHER_MARKDOWN_DOC_FILENAME>.md
Subscribe to my newsletter
Read articles from Bernice Choy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Bernice Choy
Bernice Choy
A fledgling engineer dabbling into areas of DevOps, AWS and automation. I enjoy tinkering with technology frameworks and tools to understand and gain visibility in the underlying mechanisms of the "magic" in them. In the progress of accumulating nuggets of wisdom in the different software engineering disciplines!