Skip to content

Replace Beautiful Mermaid with Mermaider for server-side diagram rendering#2838

Open
theletterf wants to merge 10 commits into
mainfrom
replace-beautiful-mermaid-with-mermaider
Open

Replace Beautiful Mermaid with Mermaider for server-side diagram rendering#2838
theletterf wants to merge 10 commits into
mainfrom
replace-beautiful-mermaid-with-mermaider

Conversation

@theletterf

Copy link
Copy Markdown
Member

Summary

  • Move Mermaid diagram rendering from client-side JavaScript (@theletterf/beautiful-mermaid npm package) to server-side C# (Mermaider NuGet package), producing inline SVGs at build time instead of shipping a JS runtime to the browser.
  • Mermaider is a pure .NET port of beautiful-mermaid itself, so diagram compatibility is very close. It supports flowchart, sequence, state, class, and ER diagrams — the same set we use today.
  • SVGs are rendered with strict mode enabled (rejects classDef/style directives) and SVG sanitization (strips XSS vectors), matching the security posture of the existing setup.
  • Unsupported or malformed diagrams emit a build warning and fall back to rendering the raw source in an error block.
  • The mermaid.ts client code is simplified to only attach interactive controls (zoom, pan, fullscreen) to pre-rendered SVG containers — no more lazy-loading a JS rendering library.

Made with Cursor

…ering

Move Mermaid diagram rendering from client-side JavaScript
(beautiful-mermaid npm package) to server-side C# (Mermaider NuGet
package), producing inline SVGs at build time instead of shipping a
JS runtime to the browser.

- Add Mermaider 0.5.0 NuGet package to Elastic.Markdown
- Replace RenderMermaidBlock() to call MermaidRenderer.RenderSvg()
  with strict mode and SVG sanitization
- Emit inline SVG wrapped in mermaid-container/viewport/rendered divs
- Graceful fallback with build warning on parse failures
- Simplify mermaid.ts to only handle zoom/pan/fullscreen controls
  for pre-rendered SVGs (remove all beautiful-mermaid integration)
- Remove @theletterf/beautiful-mermaid npm dependency and copy script
- Update test assertions for inline SVG output

Co-authored-by: Claude <noreply@anthropic.com>
Made-with: Cursor
@theletterf theletterf requested a review from a team as a code owner March 3, 2026 09:43
@theletterf theletterf requested a review from reakaleek March 3, 2026 09:43
@theletterf theletterf marked this pull request as draft March 3, 2026 09:43
Comment thread src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockHtmlRenderer.cs Outdated
Comment thread src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockHtmlRenderer.cs Outdated
Co-authored-by: Claude <noreply@anthropic.com>
Made-with: Cursor
@theletterf theletterf self-assigned this Mar 4, 2026
@theletterf theletterf closed this Apr 16, 2026
@theletterf theletterf reopened this May 20, 2026
Keep the Mermaider branch current with main, address review feedback in the renderer, and sanitize rendered SVG without rejecting supported styled diagrams.

Co-authored-by: GPT-5.5 <noreply@openai.com>
Made-with: Cursor
@theletterf theletterf temporarily deployed to integration-tests May 20, 2026 12:41 — with GitHub Actions Inactive

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockHtmlRenderer.cs`:
- Around line 315-319: MermaidRenderOptions currently only sets Bg and Fg;
explicitly enable strict mode by setting RenderOptions.Strict = true on the same
static object (MermaidRenderOptions) so Mermaider rejects classDef/style
directives; update the MermaidRenderOptions initializer to include Strict = true
to enforce directive validation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 6c27367c-d811-4893-b021-d7f8c7aa7738

📥 Commits

Reviewing files that changed from the base of the PR and between 20b4b96 and 94e01a4.

⛔ Files ignored due to path filters (1)
  • src/Elastic.Documentation.Site/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (7)
  • Directory.Packages.props
  • src/Elastic.Documentation.Site/Assets/markdown/mermaid.css
  • src/Elastic.Documentation.Site/Assets/mermaid.ts
  • src/Elastic.Documentation.Site/package.json
  • src/Elastic.Markdown/Elastic.Markdown.csproj
  • src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockHtmlRenderer.cs
  • tests/Elastic.Markdown.Tests/Directives/MermaidTests.cs

Satisfy the C# formatter after restoring the Mermaid test changes on the updated branch.

Co-authored-by: GPT-5.5 <noreply@openai.com>
Made-with: Cursor
Clarify why Mermaider strict mode stays disabled while SVG sanitization remains enabled for existing styled diagrams.

Co-authored-by: GPT-5.5 <noreply@openai.com>
Made-with: Cursor
@theletterf theletterf temporarily deployed to integration-tests May 21, 2026 07:41 — with GitHub Actions Inactive
@coderabbitai

coderabbitai Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: fbb0394f-2a3d-4cc3-af41-1f59a5a9c0fb

📥 Commits

Reviewing files that changed from the base of the PR and between 3f6c2b0 and 2580f29.

⛔ Files ignored due to path filters (1)
  • src/Elastic.Documentation.Site/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (2)
  • Directory.Packages.props
  • src/Elastic.Documentation.Site/package.json
🚧 Files skipped from review as they are similar to previous changes (1)
  • Directory.Packages.props

📝 Walkthrough

Walkthrough

This PR migrates Mermaid diagram rendering from client-side Beautiful Mermaid to server-side rendering via Mermaider (version 0.8.0). It adds Mermaider package references, updates EnhancedCodeBlockHtmlRenderer to emit sanitized inline SVG or a styled error fallback, removes client-side rendering/post-processing and reduces initMermaid to interactivity wiring, updates package.json build scripts, and changes tests to assert div.mermaid-container and inline SVG output.

Sequence Diagram(s)

sequenceDiagram
  participant Renderer as EnhancedCodeBlockHtmlRenderer
  participant Mermaider
  participant Sanitizer as SvgSanitizer
  participant Output as HTML Output
  Renderer->>Renderer: ExtractMermaidText (dedent)
  Renderer->>Mermaider: RenderSvg(MermaidRenderOptions, mermaidText)
  alt Render success
    Mermaider->>Sanitizer: raw SVG
    Sanitizer->>Output: sanitized SVG wrapped in div.mermaid-container
  else Render failure
    Renderer->>Output: pre.mermaid-error with escaped mermaid text
  end
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 10.71% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the primary change: replacing Beautiful Mermaid (client-side) with Mermaider (server-side) for diagram rendering.
Description check ✅ Passed The description clearly explains the migration from client-side JavaScript to server-side C# rendering, including compatibility, security, fallback behavior, and simplified client code.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch replace-beautiful-mermaid-with-mermaider

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@theletterf

Copy link
Copy Markdown
Member Author

@Mpdreamz I think we're ready to do this. Made numerous updates to Mermaider :)

@Mpdreamz

Mpdreamz commented Jun 2, 2026

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved in 3274c65.

Copilot AI requested a review from Mpdreamz June 2, 2026 09:54
@Mpdreamz

Mpdreamz commented Jun 2, 2026

Copy link
Copy Markdown
Member

@theletterf https://www.nuget.org/packages/mermaider#readme-body-tab

0.8.0 is now released!

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@theletterf

Copy link
Copy Markdown
Member Author

Done, bumped to 0.8.0 in .

@theletterf

Copy link
Copy Markdown
Member Author

@Mpdreamz Ready!

@Mpdreamz

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved in 2580f29.

Copilot AI requested a review from Mpdreamz June 10, 2026 13:27
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.

3 participants