Integrate Mermaid with NimblePublisher in two lines of code
by Tomasz Kowal
José Valim, the creator of Elixir programming language and Livebook, also participated in a suite of “Nimble*” libraries. The common trait of those projects is minimalism and focus.
One of those libraries is NimblePublisher. The library integrates with Phoenix framework, creating a blogging platform that is very pleasant to use by developers. You can specify blog posts in markdown, and it supports syntax highlighting for code snippets out of the box. It is super fast because it compiles blog posts to in-memory HTML. I used it to build this blog.
I also like using charts when communicating ideas, so I wanted to integrate Mermaid. Mermaid allows specifying diagrams in plain text and rendering them on the web. Many well-known services like GitHub, Notion or Obsidian use it.
There are a couple of ways to integrate Mermaid, but given how fast NimblePublisher is, I didn’t want it to make requests to external API. I tried to bundle it with my blog. And it was more straightforward than I expected!
Here are the steps:
- Install node (at least v16)
- Install yarn
-
cd assets
-
yarn add mermaid
-
Add those two lines in
assets/js/app.js
That is it!import mermaid from "mermaid"; mermaid.initialize({ startOnLoad: true });
You can now define the chart in your post using the following syntax:
```mermaid
sequenceDiagram
Joe->>Mike: Hello, Mike!
Mike-->>Joe: Hello, Joe!
Mike->>Joe: System working?
Joe-->>Mike: Seems to be!
Mike->>Joe: OK, fine.
Joe-->>Mike: OK.
```
sequenceDiagram
Joe->>Mike: Hello, Mike!
Mike-->>Joe: Hello, Joe!
Mike->>Joe: System working?
Joe-->>Mike: Seems to be!
Mike->>Joe: OK, fine.
Joe-->>Mike: OK.
Did you like it? Follow me on Mastodon or Twitter (for as long as it lasts)