Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/components/Mermaid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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.

high

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.

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.

Can we verify this before merging?

Copy link
Copy Markdown
Contributor Author

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.


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;

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.

medium

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.

Suggested change
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;

const escapedInner = unquotedInner.replace(/"/g, '"');
return `${open}"${escapedInner}"${close}`;
}
);
}

// Initialize mermaid with defaults - Japanese aesthetic
mermaid.initialize({
startOnLoad: true,
Expand Down Expand Up @@ -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;

Expand All @@ -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>
`;
}
}
Expand Down