Skip to content

feat: add mermaid diagram rendering support#2943

Open
rbizos wants to merge 1 commit intoprometheus:mainfrom
rbizos:feature/mermaid
Open

feat: add mermaid diagram rendering support#2943
rbizos wants to merge 1 commit intoprometheus:mainfrom
rbizos:feature/mermaid

Conversation

@rbizos
Copy link
Copy Markdown

@rbizos rbizos commented Mar 31, 2026

Render mermaid code blocks as interactive SVG diagrams via a homemade rehype plugin with https://github.com/mermaid-js/mermaid.

This was somewhat simpler than using rehype-mermaid. The result looks like this
image

Render mermaid code blocks as interactive SVG diagrams via a rehype
plugin that transforms them into client-side React components.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Raphael Bizos <r.bizos@criteo.com>
@jan--f jan--f requested a review from juliusv April 1, 2026 08:54
Copy link
Copy Markdown
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks generally ok, the harm (if dependencies are ok) is small if things go wrong with this, so I'd try this 👍🏽

Let's wait on @juliusv here for any objections (also needs rebase)

"typescript": "^5"
}
},
"node_modules/@antfu/install-pkg": {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not an expert for JS, but this dep feels pointless (: I assume mermaid package needs it.

Other dependencies like d3 are heavy too.

@juliusv would that be a problem?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also no real JS or JS deps expert, but I do see more data being downloaded on every documentation page, even if it does not contain a Mermaid diagram. For example, checking the total page download size for /docs/introduction/first_steps/ in the Chrome Network tab in an incognito window (building both main and the PR with npm run build-all and then npx serve out):

  • main: 941kB transferred
  • feature/mermaid: 1.1MB transferred

We definitely want to avoid that extra overhead for every page for a niche feature that's only used on one page so far.

Asked about that, Claude gives the following feedback, which might worth looking into (but again, I don't have much experience with this myself):

Image

I was thinking server-side rendering of Mermaid diagrams would be nice, but I guess that's not supported yet (mermaid-js/mermaid#3650).

@bwplotka
Copy link
Copy Markdown
Member

bwplotka commented Apr 1, 2026

Looks good!

image

Copy link
Copy Markdown
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our OM2 WG we also mention we are ok with static generation or text based, so other options are on the table too.

import { useEffect, useId, useState } from "react";

function initializeMermaid() {
const isDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use the useComputedColorScheme hook from Mantine here? See examples from the Prometheus codebase: https://github.com/search?q=repo%3Aprometheus%2Fprometheus%20useComputedColorScheme&type=code

And maybe the right place to do that is in the component below, and then re-init Mermaid when the color scheme changes, so the diagram is updated.

Currently the color scheme also doesn't work correctly for me, I'm always getting the light variant of the diagram, even in dark mode:

Image

"typescript": "^5"
}
},
"node_modules/@antfu/install-pkg": {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also no real JS or JS deps expert, but I do see more data being downloaded on every documentation page, even if it does not contain a Mermaid diagram. For example, checking the total page download size for /docs/introduction/first_steps/ in the Chrome Network tab in an incognito window (building both main and the PR with npm run build-all and then npx serve out):

  • main: 941kB transferred
  • feature/mermaid: 1.1MB transferred

We definitely want to avoid that extra overhead for every page for a niche feature that's only used on one page so far.

Asked about that, Claude gives the following feedback, which might worth looking into (but again, I don't have much experience with this myself):

Image

I was thinking server-side rendering of Mermaid diagrams would be nice, but I guess that's not supported yet (mermaid-js/mermaid#3650).

@juliusv
Copy link
Copy Markdown
Member

juliusv commented Apr 1, 2026

In our OM2 WG we also mention we are ok with static generation or text based, so other options are on the table too.

This might actually be a sensible approach to this in the context of the Prometheus docs repo, unless we plan on using Mermaid diagrams in many more places?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants