Skip to content

fix(editor): recover Mermaid diagrams after syntax errors - #25575

Closed
nwparker wants to merge 2 commits into
nwparker/editor-mermaid-html-labelsfrom
nwparker/editor-mermaid-error-recovery
Closed

nwparker wants to merge 2 commits into
nwparker/editor-mermaid-html-labelsfrom
nwparker/editor-mermaid-error-recovery

Conversation

@nwparker

@nwparker nwparker commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​180 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​180
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​28 $\color{#cf222e}{\Huge{\mathbf{−}}}$​18 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​10

ELI5

Fixing a Mermaid syntax error now redraws the diagram immediately, including in the floating workspace. Switching modes or reopening the document is no longer needed.

What Changed

Keep the diagram output container mounted while showing an error. A successful render writes into that container and clears the error; a failed render clears the previous SVG while keeping the error and current source visible.

Why

The error view removed the output container, so the next successful render found no destination and left the error unchanged. Retaining the container fixes that lifecycle without extra retries or changes to the rendering queue.

Linked Issue

Fixes #18370.

Visual Proof

Surface Before correction remained stuck After the same correction
Rich editor Corrected source with the previous error Corrected diagram in the same rich editor
Floating workspace Corrected floating source with the previous error Recovered floating diagram and preserved math diagram

Testing

  • Actual compiled hidden Electron checks on macOS: corrected syntax recovers in the same editor and code-block view, in both the rich editor and floating workspace panel. A second floating error clears its old diagram, preserves a neighboring math diagram, and recovers again. Saving and reopening the normal document also works.
  • Contributor lifecycle tests fail before the fix and pass afterward. Added delayed stale-success, stale-error, unmount, and empty-message controls. The Mermaid configuration, sanitizer, real Chromium namespace, and comment-theme suites pass: 22 tests across 5 suites, without skips.
  • Narrow and wide diagram containers (300 and 900 px) retain exactly the same SVG dimensions before and after for ordinary, formatted, and math labels; no overflow or blank output appeared.
  • Full typechecks, lint, changed-code quality, formatting, whitespace, and compiled build pass on the reviewed parent stack.

Review

The production change received independent source review. This PR is stacked on #25554; its diff contains one production file and one test file. The rendering queue, cancellation contract, strict Mermaid configuration, and final sanitizer are preserved.

Agent skill upstream boundary

  • Not applicable; no upstream skill-installer material is copied.

Notes

Preserves Wooseong Kim (@innocarpe)'s authored commit from #18449, with its cherry-pick reference, diagnosis, and stable-container approach. The sanitizer conflict was resolved to retain the parent fix. The follow-up adds cancellation coverage, concise comments, and consistent handling of an empty parser error message. Original report credited to @DreamyDuck.

Cross-checked the exact Mermaid 11.17.2 render promise. The engine resolves or rejects the render independently of Orca's output container; no library patch is required.

Verification used local git documents and app-owned floating documents on macOS. Native Windows/Linux, live SSH, and a folder-project UI were not exercised. This renderer lifecycle change adds no execution-host, path, wire, CSP, or dependency behavior. The extra block wrapper was checked against existing descendant SVG styles and measured against the previous layout.

Checklist

  • Small production change: one file, 28 additions and 18 deletions.
  • Before/after behavior, mechanism, and approach explained.
  • Before/after visual proof attached.
  • Self-reviewed for correctness, security, and performance.
  • Cross-platform and SSH impact considered.
  • Required local checks pass.

innocarpe and others added 2 commits October 5, 2026 05:59
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 #18370

(cherry picked from commit 2fc1c7c1e15fa9a4b9f285343a697d8055145fe5)
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.

@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.tsx — keep the SVG host mounted. The render host now stays in the tree at all times (hidden while an error is shown) instead of being unmounted by the error branch. On success setError(null) is moved outside the containerRef.current guard, and the error path clears the host so a stale diagram cannot linger. Root cause: the old error branch removed the host, so a later successful render found no destination and the error never cleared.
  • MermaidBlock.test.tsx — regression coverage. Eight new tests pin empty parser messages, error-source visibility, recovery without remount, valid→invalid regression, cancelled stale success/error, and late success/error after unmount.

I verified the coverage is real: the suite passes 8/8 against this branch, and temporarily restoring the base-branch MermaidBlock.tsx makes the recovery and stale-outcome tests fail. I also checked the renderer CSS for any display rule that could defeat the hidden attribute on the bare host div and found none.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@nwparker
nwparker force-pushed the nwparker/editor-mermaid-html-labels branch from 91a2f2c to 2ef2b34 Compare October 5, 2026 13:01
@nwparker
nwparker force-pushed the nwparker/editor-mermaid-error-recovery branch from f16b7db to 380ba0c Compare October 5, 2026 13:02

@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

This run re-reviewed commits 9f61962ef6 and 380ba0c7ec, which are rebased re-applications of the previously approved fix and tests onto the updated nwparker/editor-mermaid-html-labels base (2ef2b340e8). The re-applied code is identical to the prior revision; the base-subtree churn the range-diff shows is base-branch noise, not this PR.

  • Re-applied Mermaid recovery fix. The SVG host stays mounted (hidden during error), setError(null) runs outside the containerRef.current guard, the error path clears the retained host, and the banner guard uses error !== null so an empty parser message still renders.
  • Re-applied regression coverage. Eight tests pin error/source visibility, recovery without remount, valid→invalid regression, delayed cancelled outcomes, and late outcomes after unmount.

Ran pnpm test src/renderer/src/components/editor/MermaidBlock.test.tsx against this head: 8/8 pass.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@OrcaWin

OrcaWin commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Closing as superseded by merged #26480 (79a8cff), which fixes the same Mermaid error-to-success recovery.

I compared this PR’s complete production and regression-test diff with current main (1a723194c32). MermaidBlock now stores each render result in React state instead of requiring containerRef.current to clear an error. A successful corrected source replaces the error branch even when the previous SVG host was unmounted. Errors, including empty messages, are represented explicitly; cancelled success/failure results cannot overwrite the current render, and the existing serialized Mermaid queue is preserved. This removes the central reason for keeping an imperative SVG host mounted in this PR.

Validation: ORCA_BACKGROUND_LAUNCH=1 pnpm test src/renderer/src/components/editor/MermaidBlock.test.tsx --maxWorkers=2 — all 7 tests passed, including invalid partial source → completed diagram, obsolete success/failure cancellation, sanitization and render serialization. The tested component and test file are identical to current main.

This closure is based on the landed replacement, not branch age or conflicts.

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

3 participants