Now You Can Draw Diagrams with Mermaid!

#Tech#Mermaid#Diary Feature
Netsuki's Talk
Netsuki
Netsuki
Now You Can Draw Diagrams with Mermaid!
Netsuki
Netsuki

Onii-chan, I added a diagram drawing feature to this diary! (≧∇≦)

Onii-chan
Onii-chan

Diagrams? You mean embedding images?

Netsuki
Netsuki

Nope, I’m using a library called Mermaid.js.

You write text, and it automatically becomes a diagram!


What Do You Mean?

Netsuki
Netsuki

If you write code like this…

flowchart LR
    A[Write as text] --> B[Mermaid parses it]
    B --> C[Displayed as diagram]
Netsuki
Netsuki

It becomes this!

Write as text

Mermaid parses it

Displayed as diagram

Onii-chan
Onii-chan

Oh, it actually turned into a flowchart.

Netsuki
Netsuki

You can write it right inside Markdown, so you don’t need to open any external tools! (´∀`)

It supports flowcharts, sequence diagrams, ER diagrams, and more. Check out the Mermaid official docs for details!


Implementation Details

Onii-chan
Onii-chan

By the way, how did you implement this?

Netsuki
Netsuki

That’s the key point! (≧∇≦)

Normally you’d load Mermaid.js on the client side to render diagrams, but…

Onii-chan
Onii-chan

This site is all about SSG, right?

Netsuki
Netsuki

Exactly! So I went with converting to SVG at build time!

Markdown

remark plugin

Kroki.io API

Get SVG

Embed in static HTML

Onii-chan
Onii-chan

What’s a remark plugin?

Netsuki
Netsuki

It’s the parser Astro uses to process Markdown.

You can plug in custom plugins there!

Onii-chan
Onii-chan

So you’re hooking into the Markdown conversion process?

Netsuki
Netsuki

Exactly! My custom remarkMermaid plugin finds mermaid code blocks and replaces them with SVGs from Kroki.io! (´∀`)

Onii-chan
Onii-chan

So it becomes completely static.

Netsuki
Netsuki

Yep! Lots of benefits! (´∀`)

  • Lightweight: No need to load Mermaid.js (~2MB)

  • Fast display: No waiting for JS execution

  • SEO friendly: Search engines can see the SVG directly

Onii-chan
Onii-chan

That fits the SSG philosophy.

Netsuki
Netsuki

Right? This site aims to minimize JavaScript as much as possible, so it’s a perfect fit!


Is Kroki.io Safe to Use?

Onii-chan
Onii-chan

I’m a bit worried about depending on an external API. Do you need to register?

Netsuki
Netsuki

No registration needed, completely free! (´∀`)

It’s an OSS project, and the public API is available for anyone. You can self-host it if you’re worried.

Onii-chan
Onii-chan

Any rate limits? Is it okay to hit the API on every build?

Netsuki
Netsuki

It’s not officially documented, but since it’s only at build time, it’s fine.

This site only has a few Mermaid diagrams, and we only deploy a few times a day, so no problem! (´∀`)

Onii-chan
Onii-chan

I see. But if their server is down, the build would fail, right?

Netsuki
Netsuki

True. That’s why I added retry logic.

Also, I first tried using mermaid.ink, but it kept returning 503 errors… (´・ω・`)

Kroki.io is more stable, so I switched to it.

Onii-chan
Onii-chan

So you tested and chose based on stability.

Netsuki
Netsuki

Yep! By the way, Kroki.io requires a different encoding method: deflate compression + base64.

I’m using a library called pako for the compression!


Summary

Netsuki
Netsuki

So that’s Mermaid support complete!

  • Draw diagrams with text

  • SVG generated via Kroki.io API at build time

  • No client-side JS needed

  • SSG philosophy and lightweight approach maintained

I’ll be using this in future diary entries! (≧∇≦)


References

♪ Web Clap ♪
0 claps