fix-mermaid¶
Validates and repairs Mermaid diagrams in markdown files using mermaid-cli for syntax checking.
Synopsis¶
Description¶
The fix-mermaid command scans markdown files for Mermaid diagram code blocks, validates each diagram using mermaid-cli, and automatically repairs common syntax errors. This helps maintain diagram quality in documentation without manual debugging.
The repair process:
- Scan: Identifies all mermaid code blocks in the file
- Validate: Tests each diagram using mermaid-cli
- Repair: Attempts to fix invalid diagrams (up to 3 iterations per diagram)
- Fallback: Inserts placeholder for diagrams that cannot be repaired
Parameters¶
| Parameter | Position | Description |
|---|---|---|
file-path |
$1 |
Path to markdown file, or - for stdin |
Requirements¶
This command requires Node.js to be installed:
| Dependency | Description |
|---|---|
| Node.js | Required for npx |
| @mermaid-js/mermaid-cli | Fetched automatically via npx |
Automatic Installation
The mermaid-cli package is fetched via npx on first use. No manual installation is required if you have Node.js installed.
Input Modes¶
File Mode¶
Process a markdown file in-place:
The file is updated in-place with repaired diagrams.
Stdin Mode¶
Process a single diagram from stdin (outputs to stdout):
When using stdin mode, provide a single Mermaid diagram (without code fences). The repaired diagram is output to stdout.
Repair Process¶
The command attempts to repair each invalid diagram through iterative fixes:
| Iteration | Action |
|---|---|
| 1 | Parse error message, apply targeted fix |
| 2 | Try alternative syntax corrections |
| 3 | Apply broader structural fixes |
| Fallback | Insert placeholder if all iterations fail |
Common Repairs¶
The repair process handles these common issues:
- Missing or malformed node definitions
- Invalid arrow syntax
- Unescaped special characters in labels
- Missing subgraph closures
- Invalid direction declarations
Placeholder Behavior¶
When a diagram cannot be repaired after 3 iterations, a placeholder is inserted:
This ensures the document remains valid while preserving the location for manual review.
## Output
The command produces a JSON summary of repairs:
```json
{
"file": "docs/architecture.md",
"diagrams_found": 5,
"diagrams_valid": 3,
"diagrams_repaired": 1,
"diagrams_failed": 1,
"repairs": [
{
"index": 2,
"iterations": 2,
"status": "repaired"
},
{
"index": 4,
"iterations": 3,
"status": "placeholder"
}
]
}
Examples¶
Fix Diagrams in Documentation¶
Expected output:
Scanning docs/guides/architecture.md...
Found 4 mermaid diagrams
Diagram 1: Valid
Diagram 2: Invalid - attempting repair...
Iteration 1: Fixed missing arrow syntax
Repaired successfully
Diagram 3: Valid
Diagram 4: Valid
Summary:
- Valid: 3
- Repaired: 1
- Failed: 0
Fix All Documentation¶
Process multiple files:
Error Handling¶
| Condition | Behavior |
|---|---|
| File not found | Returns error message |
| No mermaid blocks | Reports "No diagrams found" |
| Node.js not installed | Returns error with installation instructions |
| All diagrams valid | Reports success, no changes made |
| Some diagrams unfixable | Inserts placeholders, continues processing |
Related Commands¶
write-content- Create technical documentation with diagramsproject-birds-eye-view- Generate project overview with diagrams
See Also¶
- Mermaid.js Documentation - Official Mermaid syntax reference