-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: Mermaid display bug fix #426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: Mermaid display bug fix #426
Conversation
Summary of ChangesHello @danielfrey63, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a common issue where Mermaid diagrams fail to render or display incorrectly due to unescaped or unquoted labels containing special characters. By introducing a preprocessing step, the system now intelligently quotes labels and escapes problematic characters before rendering, significantly enhancing the stability and reliability of Mermaid diagram display across the application. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a preprocessing step to fix Mermaid rendering issues by quoting and escaping labels. The overall approach is sound and addresses the problem described. However, I've identified a couple of areas for improvement. The regular expression for finding labels does not correctly handle nested brackets, which could lead to malformed diagrams in some edge cases. Additionally, the logic for handling already-quoted labels can be simplified for better readability and maintainability. I've provided specific comments and suggestions to address these points.
src/components/Mermaid.tsx
Outdated
| import mermaid from 'mermaid'; | ||
| // We'll use dynamic import for svg-pan-zoom | ||
|
|
||
| const MERMAID_LABELS_RE = /(\|)([^|\n]+)(\|)|(\()([^()\n]+)(\))|(\[)([^\[\]\n]+)(\])|(\{)([^{}\n]+)(\})/g; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current regular expression does not handle nested brackets within labels. For example, for a label like (label with (nested) parens), the sub-expression (\()([^()\n]+)(\)) will incorrectly match up to the first closing parenthesis, resulting in (label with (nested). This leads to a malformed diagram string after processing. While handling balanced delimiters with regex is complex, this limitation could break diagrams that use such structures. The original diagram might have been invalid, but the transformation does not fix it and could make debugging harder. Please consider if a more robust parsing method is feasible or if this limitation should be documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we verify this before merging?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the hint. It was endeed a problem. I updated the PR.
src/components/Mermaid.tsx
Outdated
| const trimmedInner = inner.trim(); | ||
| const isAlreadyQuoted = | ||
| trimmedInner.length >= 2 && | ||
| trimmedInner.startsWith('"') && | ||
| trimmedInner.endsWith('"'); | ||
|
|
||
| const quotedInnerMatch = inner.match(/^\s*"(.*)"\s*$/); | ||
| const unquotedInner = isAlreadyQuoted && quotedInnerMatch ? quotedInnerMatch[1] : inner; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic to determine if a label is already quoted and to extract its content can be simplified. The current implementation uses both a manual check (startsWith/endsWith) and a regex match, which is a bit complex. A single regex match can both check for quoting and extract the content more robustly.
This simplification improves readability and maintainability.
| const trimmedInner = inner.trim(); | |
| const isAlreadyQuoted = | |
| trimmedInner.length >= 2 && | |
| trimmedInner.startsWith('"') && | |
| trimmedInner.endsWith('"'); | |
| const quotedInnerMatch = inner.match(/^\s*"(.*)"\s*$/); | |
| const unquotedInner = isAlreadyQuoted && quotedInnerMatch ? quotedInnerMatch[1] : inner; | |
| const quotedInnerMatch = inner.match(/^\s*"(.*)"\s*$/); | |
| const unquotedInner = quotedInnerMatch ? quotedInnerMatch[1] : inner; |
5b23111 to
4ae86d9
Compare
Summary
This PR fixes a Mermaid rendering issue caused by unescaped / unquoted node labels. The Mermaid component now preprocesses diagram text to safely quote labels and escape embedded quotes before rendering, preventing diagrams from failing to render or breaking when labels contain special characters.
Background / Problem
Mermaid diagrams can fail to render (or render inconsistently) when node or connection labels contain characters that Mermaid interprets as syntax—especially double quotes and certain label formats used inside:
|...|(...)[...]{...}In these cases, Mermaid parsing may throw errors and the UI ends up with a broken or missing diagram.
Fix / Approach
Changes in
src/components/Mermaid.tsx:Added a preprocessing step (
preprocessMermaidLabels) that:"characters as".Rendering now uses the preprocessed chart text:
mermaid.render(id, preprocessedChart)Error output now displays the same preprocessed diagram text that was used for rendering, making failures easier to diagnose.
After rendering,
mermaid.contentLoaded()is called (delayed) to ensure Mermaid completes initialization reliably.Scope
src/components/Mermaid.tsxHow to Test
A[This is a label with a "quote"]( … ),[ … ],{ … }, or| … |Notes
This change is intentionally scoped to label preprocessing to avoid broader behavioral changes in Mermaid rendering.