Skip to content

Conversation

@ammar-agent
Copy link
Collaborator

Fixes the issue where mermaid.js injects giant 'Syntax error in text' messages into the DOM when diagram syntax is invalid.

Problem

Mermaid has a known issue (mermaid-js/mermaid#4358) where it inserts error SVG nodes directly into the DOM tree when mermaid.render() fails. This breaks React applications by polluting the page with visible error messages.

Solution

Multi-layered defense:

  1. Parse-first validation - Call mermaid.parse() before mermaid.render() to catch syntax errors without DOM manipulation
  2. JavaScript cleanup - Remove any error elements by ID and query selector patterns
  3. Container clearing - Explicitly clear innerHTML on error to prevent stale content
  4. CSS safety net - Hide any error messages that slip through with display: none !important
  5. Lifecycle cleanup - Remove orphaned elements on component unmount

During streaming, shows "Rendering diagram..." placeholder instead of errors. After streaming completes, displays a styled error message in a pink box.

Testing

  • All 773 existing tests pass
  • Added 5 new unit tests validating error handling behavior
  • Verified against official mermaid.js best practices

Generated with cmux

Add multi-layered error handling to prevent mermaid from injecting
error messages into the DOM when diagram syntax is invalid.

- Parse diagrams before rendering to catch syntax errors early
- Clean up any error DOM elements mermaid creates (by ID and query)
- Clear container innerHTML on error to prevent stale content
- Add CSS safety net to hide any error messages that slip through
- Add cleanup on component unmount to prevent memory leaks

This fixes the known mermaid.js issue where error SVG nodes are
inserted directly into the DOM tree during render failures.

Generated with `cmux`
Add missing parse() method to mermaidStub and configure Storybook
to use the stub to avoid mermaid initialization issues.

The previous PR added a call to mermaid.parse() before rendering,
but the mock stub didn't implement this method, causing Storybook
to fail when rendering mermaid diagrams.

Changes:
- Add parse() method to mermaidStub.ts that returns resolved Promise
- Configure Storybook to use mermaid stub via alias in main.ts
- Ensures mermaid diagrams render correctly in Storybook stories

Generated with `cmux`
Remove mermaid stub alias so diagrams render with actual mermaid.js.
This allows visual verification of the error handling fixes.

The stub is still updated with parse() method for test environments
that use VITE_DISABLE_MERMAID=1.

Generated with `cmux`
The CSS rule [id^="d"][id*="mermaid"] was too broad and hid actual
diagrams, not just error messages. The JavaScript cleanup that searched
for error messages by selector was also unnecessary.

Simplified approach:
- Removed CSS rule that hid all mermaid-related elements
- Only clean up the specific element with our generated ID
- Let mermaid render naturally and show errors via React state

This allows diagrams to display in Storybook while still handling
errors gracefully through the error state UI.

Generated with `cmux`
@ammario ammario added this pull request to the merge queue Oct 27, 2025
Merged via the queue into main with commit 9eb6db0 Oct 27, 2025
13 checks passed
@ammario ammario deleted the mermaid-bug branch October 27, 2025 03:13
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.

2 participants