Skip to content

Fix stacked Mermaid error graphics while typing - #165

Open
mohit-nayak wants to merge 1 commit into
tanabe:mainfrom
mohit-nayak:main
Open

mohit-nayak wants to merge 1 commit into
tanabe:mainfrom
mohit-nayak:main

Conversation

@mohit-nayak

Copy link
Copy Markdown

Fixes #156

While you type a Mermaid diagram, mermaid's built-in "Syntax error in text"
graphic gets added to the page on every keystroke and never cleaned up. They pile
up until they cover the screen and push the editor out of view, and the only way
out is a refresh.

The cause: we call mermaid.render() without a container element, so mermaid
creates a temp div in document.body and removes it at the end of the render. When
parsing fails it draws its error graphic into that div and rethrows before the
cleanup runs, so the div is left behind. The preview re-renders on a 150ms
debounce and half-typed syntax fails to parse on most keystrokes, so they add up
fast. Since these leftovers are direct children of body, which is a flex column,
each one steals vertical space from the editor instead of just sitting below it.

The fix is suppressErrorRendering: true in configureMermaid(). With that flag
mermaid cleans up its temp element and skips drawing its own graphic. Our inline
error message still shows, and it now includes mermaid's real parse error with
the line number instead of the generic "Syntax error in text".

A few supporting changes:

  • Check with mermaid.parse() before rendering and skip the render if it fails.
  • Keep the last good diagram while you're mid-edit instead of blanking the chart
    on every keystroke. A diagram that never rendered still shows its error at once.
  • Cache rendered SVGs, since convert() rebuilds #output on every keystroke and
    every diagram was being re-rendered on every character.
  • Use deterministic render ids so mermaid can reclaim its own leftovers.
  • Remove orphaned artifacts explicitly on failure, so this doesn't depend on
    mermaid internals staying as they are.

The CSS adds styles for the stale diagram and error note, and keeps mermaid's
temp containers out of the body flex column. I left the flex sizing itself alone,
since .split-container relies on the default flex-shrink for its normal layout
and Monaco sizes against its height: 100%.

dist/ isn't rebuilt here. It's committed to the repo but looks like something you
build yourself. Happy to add it if you'd prefer.

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: Error message covers the whole screen

1 participant