Using Mermaid diagrams with Statiq.
TLDR: The sample repository is available at: https://github.com/dpvreony/article-statiq-mermaid.
Introduction
Mermaid is a toolset for producing diagrams using markdown notation. Statiq is a .NET based static website generation toolset. This article details a way to integrate the pair to display diagrams inside a statically generated website.
Getting started
-
Create a .NET Core 3.1 (or NET5) console app project and add Statiq as a dependency.
dotnet add package Statiq.Web
-
Update the Program Entry point to utilize Statiq.
using System.Threading.Tasks; using Statiq.App; using Statiq.Web; namespace StatiqMermaid.Website { public static class Program { public static async Task<int> Main(string[] args) { return await Bootstrapper .Factory .CreateWeb(args) .RunAsync(); } } }
-
Mermaid has a NodeJS Command Line Interface tool which can be installed as an development dependency. You will need a recent version of NodeJS installed then you can install the CLI:
npm install @mermaid-js/mermaid-cli -D
You're now ready to get running with Mermaid
Preparing a Mermaid Markdown file
This sample makes use of the flowchart example from the Mermaid website, where you can find more samples and instructions on the markdown notation.
- Create a folder structure in your console app project of "input\img\mermaid"
- Add a text file to the mermaid folder with an .mmd file extension. For example flowchart.mmd
Add the following markdown notation to the file:
graph TD; A-->B; A-->C; B-->D; C-->D;
Transforming the Mermaid diagram to an image
You may want to consider changing your .gitignore to include the following lines:
src/StatiqMermaid.Website/input/img/mermaid/*.svg
src/StatiqMermaid.Website/input/img/mermaid/*.png
src/StatiqMermaid.Website/output
src/StatiqMermaid.Website/input/img/mermaid/*.png
src/StatiqMermaid.Website/output
- In the folder containing your solution create a batch file along the lines of build-mermaid.cmd
Add the following commands:
pushd StatiqMermaid.Website\input\img\mermaid ..\..\..\node_modules\.bin\mmdc -i flowchart.mmd -o flowchart.svg popd
Building Statiq and Viewing the result
The following script can sometimes error in Kestrel with the message "An attempt was made to access a socket in a way forbidden" if the port it is trying to use has been reserved by the operating system. Try changing the port number.
For more detailed information check out https://ardalis.com/attempt-made-to-access-socket/
- In the folder containing your solution create a batch file along the lines of run-testserver.cmd
-
Add the following command:
dotnet run --project StatiqMermaid.Website -- preview --port 30080
Going further
Now you have the basic flow you can consider:- Playing with more diagrams and different image output styles and file formats.
- Using a foreach loop in a batch file or powershell script to transfrom multiple markdown files.
- Using a shellexecute stage during your Statiq Console app build phase.
- Setting up a CI\CD flow to automate the build and deploy.
References
Article Status | Released |
---|---|
Article Version | 1.2 |
First Written | 2021-03-27 |
Last Revision | 2021-04-18 |
Next Review | 2022-04-18 |
License | MIT |