This action converts mermaid files into one of the output formats: png, svg, pdf.
Mermaid is a popular diagramming tool written with JS. It uses a unique syntax to generate flow charts, class diagrams, quadrant charts, etc...
Here's an example of mermaid syntax:
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
Mermaid would then convert the previous syntax into the following diagram:
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
One key aspect about Mermaid is that it's a client-side library. It uses the DOM to render diagrams.
This means that mermaid diagrams are rendered on the client, adding additional latency to your app.
Well, technically, the first solution is to not worry about it... However, a webpage will take longer to load on every rerender.
One solution is rendering mermaid diagrams asyncronously and caching them between rerenders. This allows you to render most of your webpage while using placeholders/lazy loading for mermaid diagrams. Then, caching the mermaid diagrams makes it load faster on rerenders.
This is good enough for use-cases where:
-
Initial speed of loading diagrams isn't a priority
-
A given webpage only contains a few, simpler diagrams
-
You don't mind the added complexity (async, caching, placeholders/suspense)
Another solution is using mermaid-js/mermaid-cli to render mermaid diagrams on your machine, before pushing them as static assets to your repo.
This is also good enough for use-cases where:
-
You're working on internal/individual projects. With collaborative (esp. open-source) projects, you need to check and/or protect against scripting attacks with SVGs. You can use PNGs to skip this step.
-
You're willing to figure out how to set up the mermaid cli (downloading headless browsers, sandboxing, generating new mermaid diagrams with every change).
Or... you can use this solution. This Github action checks for any added or modified mermaid source files, and it renders them to your chosen output.
This solution is:
-
Simpler. On your dev environment, you can load mermaid diagrams on the client without async/placeholders/caching. Then, in prod, your webapp can use the generated SVGs as static assets.
-
You don't need to worry about Mermaid's cli, sandboxing puppeteer or downloading web browsers.
-
More secure because SVGs are generated by GitHub actions rather than individual collaborators.
TBD...