Adding mermaid.js with htmx (English)

Adding mermaid.js with htmx

Comments

NOTE: Apart from English (and even then it's questionable, I'm Scottish). These are machine translated in languages I don't read. If they're terrible please contact me.
You can see how this translation was done in this article.

Friday, 02 August 2024

//

3 minute read

Introduction

Mermaid is a simple diagramming format that takes text-based input and generates diagrams in SVG format. It is a great tool for creating flowcharts, sequence diagrams, Gantt charts, and more. In this tutorial, we will explore how to use Mermaid with htmx to create interactive diagrams that update dynamically without page reloads. The Mermaid site is here and has far more information than I can provide here.

Markdown and Mermaid

Mermaid diagrams can be included in your markdown files by using the following syntax:

# My Markdown Title
```mermaid
graph LR
    A[Start] --> B[Look for movie]
    B --> C{Found?}
    C -->|Yes| D[Watch it]
    C -->|No| E[Look for another movie]
    D --> E
```

This allows you to include Mermaid diagrams directly in your markdown files, which will be rendered as SVG images when the file is converted to HTML.

graph LR A[Start] --> B[Look for movie] B --> C{Found?} C -->|Yes| D[Watch it] C -->|No| E[Look for another movie] D --> E

You can also add mermaid diagrams to normal html files by using the following syntax:

<pre class="mermaid">
    graph TD
    A[Start] --> B[Look for movie]
    B --> C{Found?}
    C -->|Yes| D[Watch it]
    C -->|No| E[Look for another movie]
    D --> E
</pre>

Examples of Mermaid diagrams

Mermaid is a powerful tool that lets you build a wide range of diagrams using simple text-based syntax. Here are some examples of the types of diagrams you can create with Mermaid:

-Pie charts:

pie title NETFLIX "Time spent looking for movie" : 90 "Time spent watching it" : 10

-Flowcharts: Flowcharts can specify direction, e.g. LR (left to right), RL (right to left), TB (top to bottom), BT (bottom to top).

flowchart LR A[Start] --> B{Is it?} B -->|Yes| C[OK] C --> D[Rethink] D --> B B ---->|No| E[End]

-Sequence diagrams:

sequenceDiagram participant A participant B A->>B: Hi B, how are you? B-->>A: Fine, thanks!

-Gantt charts:

gantt title A Gantt Diagram dateFormat YYYY-MM-DD section Section A task :a1, 2024-08-01, 30d Another task :after a1 , 20d

-Entity relationship diagrams:

erDiagram CUSTOMER ||--o{ ORDER : places ORDER ||--|{ LINE-ITEM : contains CUSTOMER }|..|{ DELIVERY-ADDRESS : uses

-User journey diagrams:

journey title My working day section Go to work Make tea: 5: Me Go upstairs: 15: Me Do work: 60: Me section Go home Go downstairs: 15: Me Sit down: 5: Me

etc...See this page for more of the MYRIAD of diagrams you can create with Mermaid here

Getting started with Mermaid and htmx

First you need to include the Mermaid library in your HTML file. You can do this by adding the following script tag to your document:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js
"></script>

Next in your _Layout.cshtml file you need to add the following script tag to initialize Mermaid (you normally do this at the bottom of the file)

<script>
    document.addEventListener('DOMContentLoaded', function () {
        mermaid.initialize({ startOnLoad: true });
    });
    document.body.addEventListener('htmx:afterSwap', function(evt) {
        mermaid.run();
        
    });

</script>

This does two things;

  1. It initializes Mermaid when the page loads; so if you directly navigate to a page with a Mermaid diagram (e.g. /blog/mermaidandhtmx ) it will render correctly.
  2. If you use htmx as in our previous tutorial it will re-render the Mermaid diagram after the page has been updated (the htmx:afterswap event).
logo

©2024 Scott Galloway