Spaces:
Paused

dushyantsingh0 Claude Sonnet 4.6 commited on
Commit
d30af1d
·
1 Parent(s): 92c20b5

fix: harden NexusMermaid rendering — skip false-positive parse, show error detail

Browse files

- Remove explicit mermaid.parse() pre-check; go straight to render() which
validates internally. parse() in v10.9 can throw on valid syntax.
- Remove nested xyChart object from themeVariables (not a valid mermaid key;
may corrupt internal config and cause all diagrams to fail)
- Always call mermaid.initialize() per render to avoid stale config state
- Show the actual mermaid error message when a diagram fails (+ console.error)
so failures are diagnosable without opening devtools

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

frontend/packages/shared/src/components/NexusMermaid.tsx CHANGED
@@ -46,7 +46,7 @@ const getMermaidConfig = (theme: 'light' | 'dark') => ({
46
  noteTextColor: theme === 'dark' ? '#cbd5e1' : '#475569',
47
  noteBkgColor: theme === 'dark' ? 'rgba(124, 58, 237, 0.1)' : '#fffbeb',
48
  noteBorderColor: theme === 'dark' ? '#8B5CF6' : '#fcd34d',
49
- // XY Chart Specifics (Both flat and nested configurations for backward/forward compatibility)
50
  xyChartTitleColor: theme === 'dark' ? '#f1f5f9' : '#0f172a',
51
  xyChartXAxisTitleColor: theme === 'dark' ? '#cbd5e1' : '#1e293b',
52
  xyChartYAxisTitleColor: theme === 'dark' ? '#cbd5e1' : '#1e293b',
@@ -57,21 +57,6 @@ const getMermaidConfig = (theme: 'light' | 'dark') => ({
57
  xyChartPlotColor: theme === 'dark' ? '#1e293b' : '#f8fafc',
58
  xyChartLineColor: '#7C3AED',
59
  xyChartBarColor: '#7C3AED',
60
- xyChart: {
61
- titleColor: theme === 'dark' ? '#f1f5f9' : '#0f172a',
62
- backgroundColor: theme === 'dark' ? '#1e293b' : '#f8fafc',
63
- xAxisLabelColor: theme === 'dark' ? '#cbd5e1' : '#1e293b',
64
- xAxisTitleColor: theme === 'dark' ? '#cbd5e1' : '#1e293b',
65
- xAxisTickColor: theme === 'dark' ? '#475569' : '#cbd5e1',
66
- xAxisLineColor: theme === 'dark' ? '#334155' : '#cbd5e1',
67
- yAxisLabelColor: theme === 'dark' ? '#cbd5e1' : '#1e293b',
68
- yAxisTitleColor: theme === 'dark' ? '#cbd5e1' : '#1e293b',
69
- yAxisTickColor: theme === 'dark' ? '#475569' : '#cbd5e1',
70
- yAxisLineColor: theme === 'dark' ? '#334155' : '#cbd5e1',
71
- plotColorPalette: theme === 'dark'
72
- ? '#8B5CF6, #EC4899, #10B981, #3B82F6, #F59E0B, #EF4444'
73
- : '#7C3AED, #EC4899, #10B981, #3B82F6, #F59E0B, #EF4444',
74
- },
75
  },
76
  securityLevel: 'loose',
77
  fontFamily: 'Inter, system-ui, sans-serif',
@@ -263,11 +248,10 @@ export const NexusMermaid: React.FC<MermaidProps> = ({ chart, theme: propTheme,
263
  // 0. Ensure we are in the browser
264
  if (typeof window === 'undefined') return;
265
 
266
- // 1. Re-initialize if theme changed
267
- if (lastInitializedTheme !== activeTheme) {
268
- mermaid.initialize(getMermaidConfig(activeTheme) as any);
269
- lastInitializedTheme = activeTheme;
270
- }
271
 
272
  // 2. Clean and Pre-process
273
  let sanitized = chart
@@ -288,26 +272,16 @@ export const NexusMermaid: React.FC<MermaidProps> = ({ chart, theme: propTheme,
288
  }
289
 
290
  try {
291
- // 1. Validate Syntax FIRST (Prevents Mermaid from injecting error UI into DOM)
292
- try {
293
- await mermaid.parse(sanitized);
294
- } catch (parseErr: any) {
295
- // If parse fails, we try a desperate fallback: Maybe the AI forgot the header entirely
296
- // and it's not a graph TD? But mostly we just show the error.
297
- throw new Error(`Syntax validation failed: ${parseErr.message || "Invalid diagram structure"}`);
298
- }
299
-
300
- // 2. Generate Unique ID
301
  const id = `mermaid-${Math.random().toString(36).substring(2, 9)}`;
302
 
303
- // 3. Render (Mermaid 10.x compat)
304
  const { svg: renderedSvg } = await mermaid.render(id, sanitized);
305
 
306
  if (isMounted) {
307
  if (!renderedSvg || renderedSvg.includes('Syntax error')) {
308
- throw new Error("Internal rendering error: Syntax is incompatible.");
309
  }
310
- // Aggressive Clean: Remove hardcoded width/height to let CSS handle scaling properly
311
  const cleanSvg = renderedSvg
312
  .replace(/width="[^"]*"/, 'width="100%"')
313
  .replace(/height="[^"]*"/, 'height="auto"')
@@ -317,7 +291,9 @@ export const NexusMermaid: React.FC<MermaidProps> = ({ chart, theme: propTheme,
317
  }
318
  } catch (err: any) {
319
  if (isMounted) {
320
- setError(err.message || "Diagram synthesis interrupted.");
 
 
321
  }
322
  } finally {
323
  if (isMounted) {
@@ -389,7 +365,7 @@ export const NexusMermaid: React.FC<MermaidProps> = ({ chart, theme: propTheme,
389
  borderRadius: 10, fontSize: 12,
390
  }}>
391
  <span style={{ fontSize: 14, lineHeight: 1 }}>⚠️</span>
392
- <span style={{ color: activeTheme === 'dark' ? '#fbbf24' : '#92400e', fontWeight: 600, flex: 1 }}>
393
  Diagram could not be rendered
394
  </span>
395
  <button
@@ -408,15 +384,26 @@ export const NexusMermaid: React.FC<MermaidProps> = ({ chart, theme: propTheme,
408
  </button>
409
  </div>
410
  {showRaw && (
411
- <pre style={{
412
- margin: '6px 0 0 0', padding: '14px 16px',
413
- background: activeTheme === 'dark' ? '#0f172a' : COLORS.gray50,
414
- borderRadius: 10, fontSize: 11, textAlign: 'left', whiteSpace: 'pre-wrap',
415
- fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
416
- color: activeTheme === 'dark' ? '#94a3b8' : COLORS.gray600,
417
- border: `1px solid ${activeTheme === 'dark' ? '#1e293b' : COLORS.gray200}`,
418
- lineHeight: 1.6, overflowX: 'auto'
419
- }}>{chart}</pre>
 
 
 
 
 
 
 
 
 
 
 
420
  )}
421
  </div>
422
  );
 
46
  noteTextColor: theme === 'dark' ? '#cbd5e1' : '#475569',
47
  noteBkgColor: theme === 'dark' ? 'rgba(124, 58, 237, 0.1)' : '#fffbeb',
48
  noteBorderColor: theme === 'dark' ? '#8B5CF6' : '#fcd34d',
49
+ // XY Chart Specifics (flat keys only — nested xyChart object is not a valid mermaid themeVariable)
50
  xyChartTitleColor: theme === 'dark' ? '#f1f5f9' : '#0f172a',
51
  xyChartXAxisTitleColor: theme === 'dark' ? '#cbd5e1' : '#1e293b',
52
  xyChartYAxisTitleColor: theme === 'dark' ? '#cbd5e1' : '#1e293b',
 
57
  xyChartPlotColor: theme === 'dark' ? '#1e293b' : '#f8fafc',
58
  xyChartLineColor: '#7C3AED',
59
  xyChartBarColor: '#7C3AED',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  },
61
  securityLevel: 'loose',
62
  fontFamily: 'Inter, system-ui, sans-serif',
 
248
  // 0. Ensure we are in the browser
249
  if (typeof window === 'undefined') return;
250
 
251
+ // Always initialize — ensures config is applied even after HMR or module re-evaluation.
252
+ // The call is cheap (synchronous) and idempotent; theme guard removed to prevent stale state.
253
+ mermaid.initialize(getMermaidConfig(activeTheme) as any);
254
+ lastInitializedTheme = activeTheme;
 
255
 
256
  // 2. Clean and Pre-process
257
  let sanitized = chart
 
272
  }
273
 
274
  try {
275
+ // Skip explicit mermaid.parse — render() validates internally and
276
+ // parse() in v10 can throw false negatives on valid syntax.
 
 
 
 
 
 
 
 
277
  const id = `mermaid-${Math.random().toString(36).substring(2, 9)}`;
278
 
 
279
  const { svg: renderedSvg } = await mermaid.render(id, sanitized);
280
 
281
  if (isMounted) {
282
  if (!renderedSvg || renderedSvg.includes('Syntax error')) {
283
+ throw new Error("Internal rendering error: SVG output is empty or corrupt.");
284
  }
 
285
  const cleanSvg = renderedSvg
286
  .replace(/width="[^"]*"/, 'width="100%"')
287
  .replace(/height="[^"]*"/, 'height="auto"')
 
291
  }
292
  } catch (err: any) {
293
  if (isMounted) {
294
+ const msg = err?.message || String(err) || "Diagram synthesis interrupted.";
295
+ console.error("[NexusMermaid] render failed:", msg, "\n---\n", sanitized);
296
+ setError(msg);
297
  }
298
  } finally {
299
  if (isMounted) {
 
365
  borderRadius: 10, fontSize: 12,
366
  }}>
367
  <span style={{ fontSize: 14, lineHeight: 1 }}>⚠️</span>
368
+ <span style={{ color: activeTheme === 'dark' ? '#fbbf24' : '#92400e', fontWeight: 600, flex: 1 }}>
369
  Diagram could not be rendered
370
  </span>
371
  <button
 
384
  </button>
385
  </div>
386
  {showRaw && (
387
+ <>
388
+ <pre style={{
389
+ margin: '6px 0 0 0', padding: '10px 14px',
390
+ background: 'rgba(245,158,11,0.05)',
391
+ borderRadius: 8, fontSize: 10, textAlign: 'left', whiteSpace: 'pre-wrap',
392
+ fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
393
+ color: activeTheme === 'dark' ? '#fbbf24' : '#92400e',
394
+ border: '1px solid rgba(245,158,11,0.2)',
395
+ lineHeight: 1.5, overflowX: 'auto'
396
+ }}>{error}</pre>
397
+ <pre style={{
398
+ margin: '4px 0 0 0', padding: '14px 16px',
399
+ background: activeTheme === 'dark' ? '#0f172a' : COLORS.gray50,
400
+ borderRadius: 10, fontSize: 11, textAlign: 'left', whiteSpace: 'pre-wrap',
401
+ fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
402
+ color: activeTheme === 'dark' ? '#94a3b8' : COLORS.gray600,
403
+ border: `1px solid ${activeTheme === 'dark' ? '#1e293b' : COLORS.gray200}`,
404
+ lineHeight: 1.6, overflowX: 'auto'
405
+ }}>{chart}</pre>
406
+ </>
407
  )}
408
  </div>
409
  );