Repository navigation
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?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,46 @@ import React, { useEffect, useRef, useState } from 'react'; | |||||||||||||||||||||
| import mermaid from 'mermaid'; | ||||||||||||||||||||||
| // We'll use dynamic import for svg-pan-zoom | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const MERMAID_LABELS_RE = /(\|)([^|\n]+)(\|)|(\()([^()\n]+)(\))|(\[)([^\[\]\n]+)(\])|(\{)([^{}\n]+)(\})/g; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| function preprocessMermaidLabels(input: string): string { | ||||||||||||||||||||||
| return input.replace( | ||||||||||||||||||||||
| MERMAID_LABELS_RE, | ||||||||||||||||||||||
| ( | ||||||||||||||||||||||
| match, | ||||||||||||||||||||||
| o1: string | undefined, | ||||||||||||||||||||||
| i1: string | undefined, | ||||||||||||||||||||||
| c1: string | undefined, | ||||||||||||||||||||||
| o2: string | undefined, | ||||||||||||||||||||||
| i2: string | undefined, | ||||||||||||||||||||||
| c2: string | undefined, | ||||||||||||||||||||||
| o3: string | undefined, | ||||||||||||||||||||||
| i3: string | undefined, | ||||||||||||||||||||||
| c3: string | undefined, | ||||||||||||||||||||||
| o4: string | undefined, | ||||||||||||||||||||||
| i4: string | undefined, | ||||||||||||||||||||||
| c4: string | undefined | ||||||||||||||||||||||
| ) => { | ||||||||||||||||||||||
| const open = o1 ?? o2 ?? o3 ?? o4; | ||||||||||||||||||||||
| const inner = i1 ?? i2 ?? i3 ?? i4; | ||||||||||||||||||||||
| const close = c1 ?? c2 ?? c3 ?? c4; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (!open || !close || inner == null) return match; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 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; | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( This simplification improves readability and maintainability.
Suggested change
|
||||||||||||||||||||||
| const escapedInner = unquotedInner.replace(/"/g, '"'); | ||||||||||||||||||||||
| return `${open}"${escapedInner}"${close}`; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Initialize mermaid with defaults - Japanese aesthetic | ||||||||||||||||||||||
| mermaid.initialize({ | ||||||||||||||||||||||
| startOnLoad: true, | ||||||||||||||||||||||
|
|
@@ -365,8 +405,10 @@ const Mermaid: React.FC<MermaidProps> = ({ chart, className = '', zoomingEnabled | |||||||||||||||||||||
| setError(null); | ||||||||||||||||||||||
| setSvg(''); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const preprocessedChart = preprocessMermaidLabels(chart); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Render the chart directly without preprocessing | ||||||||||||||||||||||
| const { svg: renderedSvg } = await mermaid.render(idRef.current, chart); | ||||||||||||||||||||||
| const { svg: renderedSvg } = await mermaid.render(idRef.current, preprocessedChart); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (!isMounted) return; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -392,7 +434,7 @@ const Mermaid: React.FC<MermaidProps> = ({ chart, className = '', zoomingEnabled | |||||||||||||||||||||
| if (mermaidRef.current) { | ||||||||||||||||||||||
| mermaidRef.current.innerHTML = ` | ||||||||||||||||||||||
| <div class="text-red-500 dark:text-red-400 text-xs mb-1">Syntax error in diagram</div> | ||||||||||||||||||||||
| <pre class="text-xs overflow-auto p-2 bg-gray-100 dark:bg-gray-800 rounded">${chart}</pre> | ||||||||||||||||||||||
| <pre class="text-xs overflow-auto p-2 bg-gray-100 dark:bg-gray-800 rounded">${preprocessMermaidLabels(chart)}</pre> | ||||||||||||||||||||||
| `; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
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.