Skip to content

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

Draft
theletterf wants to merge 2 commits intomainfrom
replace-beautiful-mermaid-with-mermaider
Draft

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

Conversation

@theletterf
Copy link
Contributor

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
Comment on lines +299 to +306
catch (Exception e)
{
block.EmitWarning($"Failed to render Mermaid diagram: {e.Message}");
_ = renderer.Write("<pre class=\"mermaid-error\"><code>");
_ = renderer.WriteEscape(mermaidText);
_ = renderer.Write("</code></pre>");
return;
}

_ = renderer.WriteEscape(slice);
_ = renderer.WriteLine();
_ = sb.AppendLine(slice.ToString());
Co-authored-by: Claude <noreply@anthropic.com>
Made-with: Cursor
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.

1 participant