Skip to content

fix(markdown): refresh Mermaid after a syntax error is fixed - #18449

Closed
innocarpe wants to merge 1 commit into
stablyai:mainfrom
innocarpe:fix/floating-mermaid-refresh-after-syntax-error
Closed

innocarpe wants to merge 1 commit into
stablyai:mainfrom
innocarpe:fix/floating-mermaid-refresh-after-syntax-error

Conversation

@innocarpe

Copy link
Copy Markdown
Contributor

Description

Fixing a Mermaid syntax error in floating-window markdown now re-renders the diagram without toggling code/edit mode.

After a parse failure the error UI replaced the SVG host, so a later successful mermaid.render() found a null ref and never cleared the stale error.

Focused fix

  • In scope: keep the Mermaid SVG host mounted (hidden) during a syntax error so a subsequent successful parse can write the diagram and clear the error.
  • Out of scope (explicit): mermaid engine/parser changes; preview vs code/edit chrome; SSH/remote diagram execution (preview is renderer-local).

Preserves

  • Error banner and source remain visible while the diagram is invalid.
  • Preview vs code/edit mode behavior is unchanged.
  • Sanitized SVG path, serialized render queue, and theme/htmlLabels initialization are unchanged.
  • Mermaid rendering stays in the local renderer; SSH is not on this path.

Evidence

  • Test: pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/components/editor/MermaidBlock.test.tsx
  • Without the fix: invalid source shows the error, then valid source leaves that error on screen until the block remounts (mode toggle).
  • With the fix: valid source after an error shows the diagram and clears the error; a later invalid source shows the error again and drops the previous SVG.

User-regression-tradeoffs

  • None intended. Invalid diagrams still show the error; recovery no longer requires a mode toggle.

Fixes #18370

@coderabbitai

coderabbitai Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8063185e-7f44-4f82-87d8-76ac42699d25
📥 Commits

Reviewing files that changed from the base of the PR and between 2fc1c7c1e15fa9a4b9f285343a697d8055145fe5 and 3f2a3aa.

📒 Files selected for processing: 1
  • src/renderer/src/components/editor/MermaidBlock.test.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.

📝 Walkthrough

The change adds a regression test for Mermaid rendering. After a successful render, the test changes the source to partial content and makes Mermaid reject the render. It checks that the error and source appear and that the prior SVG is removed.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 3f2a3

The new test checks the error state, and an existing test protects recovery when the Mermaid source is corrected. No actionable mergeability risk remains.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly identifies the Mermaid rendering fix after syntax errors and matches the primary change.
Description check Passed The description clearly explains the user impact, root cause, focused fix, preserved behavior, testing, and linked issue. It does not follow every template heading and does not include the required vi…
Linked Issues check Passed Issue #18370 requires Mermaid to recover after invalid source becomes valid without a mode toggle. The current MermaidBlock implementation updates rendered with the new successful SVG and replaces…
Out of Scope Changes check Passed The whole-PR diff adds only a regression test in src/renderer/src/components/editor/MermaidBlock.test.tsx. The test covers the Mermaid error and recovery state transitions. It does not change the Me…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.
Full details: Description check

Explanation

The description clearly explains the user impact, root cause, focused fix, preserved behavior, testing, and linked issue. It does not follow every template heading and does not include the required visual proof or explicit N/A statement, but it contains the critical information and is mostly complete.

Full details: Linked Issues check

Explanation

Issue #18370 requires Mermaid to recover after invalid source becomes valid without a mode toggle. The current MermaidBlock implementation updates rendered with the new successful SVG and replaces the error view. src/renderer/src/components/editor/MermaidBlock.test.tsx already verifies the invalid-to-valid transition. The new test also verifies that a later invalid source shows the error and removes the old SVG.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff adds only a regression test in src/renderer/src/components/editor/MermaidBlock.test.tsx. The test covers the Mermaid error and recovery state transitions. It does not change the Mermaid engine, mode chrome, or remote execution path.

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.

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes

  • MermaidBlock keeps the SVG host mounted — the error branch no longer returns early and unmounts the container. The host <div> stays mounted but hidden={error !== null}, and setError(null) is now reached even when the host had been empty, so a later successful mermaid.render() clears the stale error.
  • Error path clears the host — on a syntax error the catch branch wipes containerRef.current.innerHTML so a previously rendered SVG can't linger behind the hidden host.
  • Regression test — a new happy-dom test file covers error display, error→valid recovery without remount, and valid→error re-failure. The middle case genuinely fails against the old guard (setError(null) was previously inside if (!cancelled && containerRef.current), so a null ref blocked recovery).

The fix is well-scoped: the wrapper .mermaid-block class move is inert for existing descendant CSS (.mermaid-block svg, [&_.mermaid-block_pre]), the hidden attribute behaves correctly with no author CSS overriding the inner host, and the renderer stays local (no SSH/wire surface touched).

Pullfrog  | Fix it ➔ | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

nwparker added a commit that referenced this pull request Oct 5, 2026
Extend Wooseong Kim’s #18449 recovery coverage with delayed success/error, unmount, and empty parser error controls. Keep the current formatted-label sanitizer when adopting the original change.
Keep the SVG host mounted while a diagram error is shown so a later
successful parse can write the diagram and clear the stale error
without toggling code/edit mode.

Fixes stablyai#18370
@innocarpe
innocarpe force-pushed the fix/floating-mermaid-refresh-after-syntax-error branch from 2fc1c7c to 3f2a3aa Compare October 22, 2026 03:42
@nwparker

Copy link
Copy Markdown
Contributor

Thanks @innocarpe for the clear diagnosis of the null SVG host after a parse failure! This is already fixed on main by #26480 (79a8cff): MermaidBlock no longer writes into a ref'd container, it keeps the render outcome (SVG or error) in state, so a successful render after a syntax error replaces the error with the diagram without toggling modes, and main's tests cover the invalid-then-valid recovery. If you still see a Mermaid diagram stuck on its error after fixing the source on the latest build, please comment or reopen.

@nwparker nwparker closed this Oct 26, 2026
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.

[Bug]: The floating window markdown Mermaid diagram does not display after fixing a syntax error unless you switch modes back and forth

2 participants