Publishing Pipeline - inline Mermaid code
Source: Dev.to
What is Mermaid and Why It’s Gaining Traction
Mermaid is a JavaScript‑based diagramming and charting tool that lets you create diagrams entirely from text using a simple, Markdown‑friendly syntax. A tiny block of text like the one below becomes a clean, professional‑looking flowchart when rendered:
graph TD
A[Start] --> B{Is it working?}
B -- Yes --> C[Great!]
B -- No --> D[Fix it]
D --> B
Diagram Types Supported Out of the Box
- Flowcharts (
graph) - Sequence diagrams
- Class diagrams
- State diagrams
- Entity‑Relationship diagrams
- Gantt charts
- Pie charts
- Requirement diagrams
- Git graphs
- …and more
Comparison to Traditional Diagramming Tools
| Feature | Mermaid | Visio / Lucidchart / draw.io |
|---|---|---|
| Version‑control friendly | 100 % text → perfect for Git | Binary files or proprietary formats |
| Diffs & reviews | Easy to see changes in PRs | Almost impossible without special viewers |
| Editing speed | Extremely fast (just type) | Requires opening a GUI tool |
| Cost | Completely free & open source | Paid (Visio) / subscription (Lucidchart) |
| Dependencies | Just a JS library or CLI | Needs installed software or an account |
| Integration with Markdown | Native (GitHub, GitLab, Obsidian, etc.) | Requires exporting images manually |
| Maintainability | Change one line → diagram updates | Must manually reposition elements |
| Collaboration | Works in any text editor + Git | Requires shared accounts or export/import cycles |
| Reproducibility | Same text → same diagram every time | Risk of “font substitution” or layout shifts |
Why Publishing Mermaid Directly Is Still a Problem
Many popular blogging platforms do not render Mermaid natively:
- WordPress – plugins are inconsistent or outdated
- Medium – no Mermaid support
- Dev.to – partial support only
- Hashnode – works but can be finicky with themes
- Company wikis (Confluence, older platforms) – usually no native rendering
Pasting a Mermaid code block into such platforms results in plain text that is useless to readers.
The Elegant Solution: Render Mermaid to PNG During the Publishing Pipeline
-
Detect Mermaid code blocks – look for fenced blocks that start with
mermaid.```mermaid graph TD A --> B ``` -
Generate a unique hash based on the exact diagram content. Identical diagrams are rendered only once, saving storage and processing time.
-
Render the Mermaid code to a high‑quality PNG using a headless browser, the Mermaid CLI, or a Node.js renderer.
-
Upload the PNG to the blog’s media library (e.g., WordPress) or an S3 bucket.
-
Replace the original code block with standard Markdown image syntax.
 -
Fallback behavior – if rendering fails, keep the original code block and log an error. This guarantees that the post never shows a broken diagram.
Benefits
- Works on every platform, even those with zero Mermaid support.
- No broken diagrams – readers always see a crisp image.
- Still version‑controlled – the source Mermaid lives in your repo.
- Deduplication – identical diagrams reuse the same image file.
- High resolution – PNGs look sharp on retina displays.
- SEO‑friendly – images can have proper
alttext.
Use Cases & Recommended Tools
| Documentation Need | Recommended Tool | Why? |
|---|---|---|
| Infrastructure / architecture docs | Mermaid | Lives in Git, easy to update, perfect for CI/CD pipelines |
| Quick flowcharts in READMEs | Mermaid | Native GitHub rendering |
| Complex interactive dashboards | Lucidchart / draw.io | Better for drag‑and‑drop and heavy collaboration |
| Official company org charts / presentations | Visio / PowerPoint | Polished look, integrates with Microsoft 365 |
| One‑off pretty diagrams for blog posts | draw.io (export PNG) | Fast to create, great styling options |
| Long‑lived, frequently updated docs | Mermaid | Minimizes maintenance pain |
Conclusion
Mermaid isn’t trying to replace heavyweight GUI tools like Visio or Lucidchart; it solves a different problem: keeping diagrams in sync with code, version‑controlled, and easy to maintain forever. By rendering Mermaid diagrams to PNGs during the publishing process, you get:
- The maintainability and reproducibility of text‑based diagrams
- The universal compatibility of plain images
The pipeline described above is now live for all upcoming series (e.g., the Puppet HA setup with Foreman, compile masters, and PuppetDB). No matter where readers view the blog, the diagrams will render beautifully.
Happy diagramming! 🚀