feat: Add JSON-aware mermaid diagram validation#295
Conversation
Fixes issue where mermaid validation couldn't detect diagrams embedded in JSON string values. Previously, the regex pattern only matched literal markdown code blocks, missing diagrams with escaped newlines in JSON. Changes: - Add extractMermaidFromJson() to detect mermaid in JSON strings - Update extractMermaidFromMarkdown() to auto-detect JSON responses - Add replaceMermaidDiagramsInJson() for proper JSON diagram replacement - Update replaceMermaidDiagramsInMarkdown() to handle both formats - Add comprehensive test coverage for JSON mermaid extraction The validation now works for diagrams in: - Regular markdown code blocks (existing behavior) - JSON string values with escaped newlines (new) - JSON in code blocks (new) - Nested JSON objects and arrays (new) All 323 mermaid/schema tests pass with no regressions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
🔍 Code Analysis ResultsPR Overview: JSON-aware Mermaid Diagram ValidationSummaryThis PR introduces JSON-aware mermaid diagram validation to fix a critical gap where mermaid diagrams embedded in JSON string values were not being detected or validated. The previous regex-based approach only matched literal markdown code blocks, missing diagrams with escaped newlines ( Files Changed Analysis3 files changed with 428 additions, 11 deletions:
Architecture & Impact AssessmentWhat This PR Accomplishes
Key Technical Changes
System Components Affectedgraph TD
A[AI Response] --> B{Format Detection}
B -->|JSON| C[extractMermaidFromJson]
B -->|Markdown| D[extractMermaidFromMarkdown]
C --> E[validateMermaidResponse]
D --> E
E --> F{Valid?}
F -->|No| G[maid auto-fix]
G --> H{Fixed?}
H -->|No| I[MermaidFixingAgent]
H -->|Yes| J[replaceMermaidDiagramsInJson]
I --> K[replaceMermaidDiagramsInJson]
J --> L[Fixed Response]
K --> L
F -->|Yes| L
Integration Points:
Scope Discovery & Context ExpansionImmediate Impact
Broader System Integration
Related Components to Consider
Testing CoverageNew test scenarios in
Integration test updates:
Backward Compatibility✅ Fully backward compatible - All existing functionality preserved:
Performance & Reliability
This enhancement significantly improves the robustness of mermaid diagram validation while maintaining the existing architecture and performance characteristics. Metadata
🐛 Debug InformationProvider: anthropic Debug Details
🔗 Download Link: visor-debug-595 Powered by Visor from Probelabs Last updated: 2025-11-16T17:31:18.839Z | Triggered by: opened | Commit: 5766baa 💡 TIP: You can chat with Visor using |
🔍 Code Analysis ResultsSecurity Issues (5)
Architecture Issues (1)
|
Summary
Fixes the issue where mermaid validation couldn't detect diagrams embedded in JSON string values. Previously, the regex pattern only matched literal markdown code blocks, missing diagrams with escaped newlines (
\n) in JSON responses.Problem
The mermaid validation used a pattern
/```mermaid([^\n]*)\n([\s\S]*?)```/githat only matched literal code blocks, not escaped ones inside JSON strings:Before (not detected):
{ "text": "```mermaid\ngraph TD\n A --> B\n```" }After (detected and validated):
{ "text": "```mermaid\ngraph TD\n A --> B\n```" }Changes
Core Functionality
extractMermaidFromJson()- New function to detect mermaid diagrams in JSON string values\n) and literal newlinesdata.visualization)extractMermaidFromMarkdown()- Enhanced to auto-detect JSON responsesreplaceMermaidDiagramsInJson()- Properly replaces diagrams in JSONreplaceMermaidDiagramsInMarkdown()- Auto-detects formatTest Coverage
Added comprehensive tests in
mermaidValidation.test.js:Updated
validationFlow.test.jsto reflect new behavior where diagrams in JSON are now correctly detected.Testing
All tests pass:
Backward Compatibility
✅ Fully backward compatible - existing markdown mermaid validation continues to work as before. This enhancement adds JSON support without changing existing behavior.
🤖 Generated with Claude Code