Skip to content

feat(ui): add lazy-loaded mermaid diagram rendering#23475

Closed
StrikeOner wants to merge 3 commits into
ggml-org:masterfrom
StrikeOner:pr/mermaid
Closed

feat(ui): add lazy-loaded mermaid diagram rendering#23475
StrikeOner wants to merge 3 commits into
ggml-org:masterfrom
StrikeOner:pr/mermaid

Conversation

@StrikeOner
Copy link
Copy Markdown

@StrikeOner StrikeOner commented May 21, 2026

Overview

What this does

Close #23477

Adds Mermaid diagram rendering to the built-in server UI. Code blocks annotated with ```mermaid are rendered as interactive diagrams client-side instead of displaying raw text.

To keep the loading time as is mermaid is loaded lazily - only when a mermaid diagram is actually present in the response. Conversations without diagrams pay zero cost.

Implementation

  • mermaid npm dependency added to tools/ui/package.json
  • mermaid-pre.ts — rehype plugin that converts ```mermaid blocks into <pre class="mermaid"> elements, runs before rehypeEnhanceCodeBlocks
  • mermaid.run() is called post-stream to avoid rendering partial syntax during generation
  • Dark/light theme aware

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: This feature was developed with AI assistance
    All code has been reviewed, tested, and is understood by me.

@StrikeOner StrikeOner requested a review from a team as a code owner May 21, 2026 12:40
@ggml-gh-bot
Copy link
Copy Markdown

ggml-gh-bot Bot commented May 21, 2026

Hi @StrikeOner, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • AI-generated content: This project does not accept PRs, descriptions or commit messages that are fully or predominantly AI-generated. If you have used AI to assist you in writing code, please make sure to disclose that explicitly.

  • Large PR: Large changes require prior discussion (e.g. an issue or RFC) and maintainers may not be able to review this PR as-is. Consider splitting it into smaller, focused PRs.


Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

- add mermaid-pre rehype plugin for detecting mermaid code blocks
- integrate plugin into MarkdownContent component
- add mermaid dependency to package.json

chore: add mermaid patch file for standalone distribution

docs: add README for mermaid patch

chore: remove patches folder (not for upstream)
@StrikeOner
Copy link
Copy Markdown
Author

In action:

Screenshot From 2026-05-21 16-02-18

@allozaur
Copy link
Copy Markdown
Contributor

Very nice! I wanted to implement Mermaid for quite some time. Will review, test a bit and get back to u

Copy link
Copy Markdown
Contributor

@allozaur allozaur left a comment

Choose a reason for hiding this comment

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

Just QA'ed it and before we dive into the code review, I think these things should be addressed:

  1. The chart item in the MarkdownContent should be zoomable/draggable
  2. We also need to reuse this component inside of DialogMermaidPreview.svelte component to have a full screen preview of mermaid chart with the standard controls
  3. I think it'd be a better UX to see some skeleton UI loading in the place of the chart rather than have a streamed text block that then jumps to change into a Mermaid render when finished.

Lemme know if this is sth you feel confident with implementing or if i should take over this.

@allozaur
Copy link
Copy Markdown
Contributor

Created #23477 to track this feature

@StrikeOner
Copy link
Copy Markdown
Author

heya, thanks for the fast feedback. according the feature requests: 2. and 3. shouldnt be a problem i think ( will look into them tonight ) but i dont see anything where and how the drag and drop/zoom function is implemented so far. did i miss something? do you have something special in mind for that?

@allozaur
Copy link
Copy Markdown
Contributor

heya, thanks for the fast feedback. according the feature requests: 2. and 3. shouldnt be a problem i think ( will look into them tonight ) but i dont see anything where and how the drag and drop/zoom function is implemented so far. did i miss something? do you have something special in mind for that?

I can take care of no. 1 later, no problem.

@StrikeOner
Copy link
Copy Markdown
Author

Points 1 and 2 are done — inline zoom/drag is working and I've added DialogMermaidPreview.svelte with fullscreen preview, zoom controls, and download.

On the skeleton UI — I did experiment with it but landed on keeping the raw syntax visible during streaming instead. My concern is that LLMs fairly often produce invalid or incomplete Mermaid syntax, and a skeleton placeholder doesn't communicate anything useful in those failure cases. The raw code at least tells the user what was generated. Once the stream completes and syntax is valid, the diagram snaps in cleanly which feels acceptable to me.

That said if you have a specific approach in mind for handling the failure states gracefully I'm open to it — just didn't want to ship a skeleton that silently fails.

@StrikeOner
Copy link
Copy Markdown
Author

@allozaur sorry for my late reply, i unfortunately will be out of a computer for some days, how many is not realy clear right now (can be days or months). if you want to overtake, do it, its yours! it would be nice to see this feature added when i return to my computer.

@allozaur
Copy link
Copy Markdown
Contributor

@allozaur sorry for my late reply, i unfortunately will be out of a computer for some days, how many is not realy clear right now (can be days or months). if you want to overtake, do it, its yours! it would be nice to see this feature added when i return to my computer.

sure thing! will do :) thanks for the heads up and good luck with whatever u have in the meantime :)

@allozaur
Copy link
Copy Markdown
Contributor

allozaur commented Jun 2, 2026

Superseded by #24032

@allozaur allozaur closed this Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(ui) Feature: Add Mermaid diagrams support

2 participants