๐ What's Mermaid Fixer
October 6, 2025 ยท View on GitHub
Mermaid Fixer
๐ง High-performance AI-driven Mermaid diagram syntax fixer built with Rust
๐ Automatically detects and fixes syntax errors in Mermaid diagrams within Markdown files
๐ Help me develop this software better by sponsoring on GitHub
๐ What's Mermaid Fixer
Mermaid Fixer is a high-performance AI-driven tool that automatically detects and fixes syntax errors in Mermaid diagrams within Markdown files. Built with Rust for performance and reliability, Mermaid Fixer helps teams maintain accurate and valid Mermaid diagrams with minimal effort.
Mermaid Fixer employs a multi-stage workflow combining static syntax validation with AI-powered intelligent fixing. The system render mermaid in a JS sandbox environment to validate sytax correctnessโ and integrates with large language models (LLMs) to intelligently repair broken diagrams while preserving their original intent and structure.
๐บ Why use Mermaid Fixer
- Save hours on manual diagram debugging
- Keep Mermaid diagrams syntactically correct
- Maintain documentation quality automatically
- Fix complex syntax issues intelligently
๐ For:
- Technical documentation teams
- Open source projects with diagrams
- Enterprise software developers
- Anyone who uses Mermaid in Markdown!
โค๏ธ Like Mermaid Fixer? Star it ๐ or Sponsor Me! โค๏ธ
๐ Snapshots
![]() |
![]() |
๐ Features & Capabilities
Core Capabilities
- Automated Scanning: Recursively scans directories for Markdown files
- Precise Detection: Uses JS sandbox environment for accurate syntax validation
- AI-Powered Fixing: Leverages LLMs to intelligently repair syntax errors
- Comprehensive Reporting: Provides detailed before/after comparisons
- Flexible Configuration: Supports multiple LLM providers and custom settings
Advanced Features
- Dry-run mode for safe testing
- Verbose logging for detailed insights
- Configurable timeout and retry mechanisms
- Support for various Mermaid diagram types
- Intelligent node ID normalization
- Smart text label cleaning
๐ก Problem Solved
The Mermaid diagram dilemma:
- Broken diagrams that don't render
- Time-consuming manual syntax debugging
- Inconsistent diagram formatting
- Complex syntax rules hard to remember
Mermaid Fixer solves this by:
- Automatically detecting syntax errors
- Intelligently fixing broken diagrams
- Maintaining diagram semantics while fixing syntax
- Providing clear feedback on changes made
๐ง How it works
Mermaid Fixer's processing workflow is divided into six distinct stages: scanning, extraction, validation, fixing, re-validation, and saving. Each stage is optimized for performance and accuracy.
Six-Stage Processing Pipeline
- Scanning Stage: Recursively discovers all Markdown files in the target directory
- Extraction Stage: Parses Markdown files and extracts Mermaid code blocks
- Validation Stage: Uses JS sandbox environment to validate each diagram's syntax
- Fixing Stage: Employs AI to repair invalid diagrams while preserving intent
- Re-validation Stage: Ensures fixed diagrams are syntactically correct
- Saving Stage: Updates files with corrected diagrams
Processing Workflow
flowchart TD
A[Directory Scanner] --> B[Markdown Parser]
B --> C[Mermaid Extractor]
C --> D[Syntax Validator]
D --> E{Valid?}
E -->|Yes| F[Skip Block]
E -->|No| G[AI Fixer]
G --> H[Re-validate]
H --> I{Fixed?}
I -->|Yes| J[Update Content]
I -->|No| K[Log Error]
F --> L[Next Block]
J --> L
K --> L
L --> M{More Blocks?}
M -->|Yes| D
M -->|No| N[Save File]
Architecture Overview
graph LR
subgraph Input Phase
A[CLI Args] --> B[Config Loader]
B --> C[Directory Scanner]
end
subgraph Processing Phase
C --> D[Markdown Scanner]
D --> E[Mermaid Extractor]
E --> F[Syntax Validator]
F --> G[AI Fixer]
end
subgraph Output Phase
G --> H[File Writer]
H --> I[Statistics Reporter]
end
๐ฅ Getting Started
Prerequisites
- Rust (version 1.70 or later)
- Cargo
- Internet connection (for LLM integration)
- LLM API key (OpenAI, etc.)
Installation
Option 1: Install from crates.io (Recommended)
cargo install mermaid-fixer
After installation, you can use mermaid-fixer command directly:
mermaid-fixer --help
Option 2: Build from Source
- Clone the repository:
git clone https://github.com/sopaco/mermaid-fixer.git - Navigate to the project directory:
cd mermaid-fixer - Build the project:
cargo build --release - The compiled binary will be available in the
target/releasedirectory.
๐ Usage
Mermaid Fixer provides a command-line interface for fixing Mermaid diagrams. Here are some basic usage examples:
Basic Commands
# Fix Mermaid diagrams in current directory
mermaid-fixer -d .
# Scan specific directory
mermaid-fixer -d /path/to/your/docs
# Dry run (detect only, no fixes)
mermaid-fixer -d ./docs --dry-run
# Verbose output
mermaid-fixer -d ./docs --verbose
Configuration
Create a config.toml file in your project root:
[llm]
provider = "openai"
model = "gpt-4"
api_key = "your-api-key-here" # or set via environment variable
max_tokens = 4000
temperature = 0.1
[mermaid]
timeout_seconds = 30
max_retries = 3
Advanced Options
# Custom LLM configuration
mermaid-fixer -d ./docs --llm-provider openai --llm-model gpt-5-mini --max-tokens 8192
# Custom and retries
mermaid-fixer -d ./docs --max-retries 5
# Custom config file
mermaid-fixer -d ./docs --config custom-config.toml
๐ Command Line Options
Basic Options
-d, --directory <DIR>: Target directory to scan (required)-c, --config <FILE>: Configuration file path (default: config.toml)--dry-run: Detect issues only, don't perform fixes-v, --verbose: Enable detailed logging output
LLM Configuration Options
--llm-provider <PROVIDER>: LLM provider (openai)--llm-model <MODEL>: LLM model name--llm-api-key <KEY>: LLM API key--llm-base-url <URL>: LLM API base URL--max-tokens <NUM>: Maximum token count--temperature <NUM>: Temperature parameter (0.0-1.0)
Mermaid Configuration Options
--timeout-seconds <NUM>: Mermaid validation timeout (seconds)--max-retries <NUM>: Maximum retry attempts
Help Options
-h, --help: Show help information-V, --version: Show version information
๐ Processing Workflow
- Scanning Stage: Recursively scan specified directory for
.mdand.markdownfiles - Extraction Stage: Extract Mermaid code blocks from Markdown files
- Validation Stage: Validate each code block syntax using JS sandbox environment
- Fixing Stage: Use AI to repair invalid code blocks
- Re-validation: Verify that fixed code is syntactically correct
- Saving: Write corrected content back to original files
๐ Output Statistics
After processing, Mermaid Fixer provides comprehensive statistics:
๐ Supported Fix Types
- โ Node ID normalization (remove special characters, ensure valid naming)
- โ Node text cleaning (remove special symbols in brackets)
- โ Arrow label normalization (quote Chinese labels)
- โ Syntax structure repair (diagram type declarations, arrow syntax)
- โ Style declaration fixes (color formats, property syntax)
๐ง Example Fixes
Before Fix
graph TD
A[่ทๅๆฐๆฎ] --> B{้ช่ฏๅๅบ็ถๆ(status)}
B -- "ๆฏ" --> C[่ฎฐๅฝๆๆฌไธ่ๆถ]
B -- "ๅฆ" --> D[้่ฏฏๅค็]
After Fix
graph TD
A[่ทๅๆฐๆฎ] --> B{้ช่ฏๅๅบ็ถๆ_Status}
B -- "ๆฏ" --> C[่ฎฐๅฝๆๆฌไธ่ๆถ]
B -- "ๅฆ" --> D[้่ฏฏๅค็]
More Examples
Node ID Issues
graph LR
Astart --> Bprocess --> Cend
Arrow Label Issues
flowchart TD
A --> B
B -- "ๆๅ" --> C
B -- "ๅคฑ่ดฅ" --> D
๐จ Important Notes
- Ensure stable internet connection for LLM API access
- Processing large numbers of files may incur API costs
- Recommend using
--dry-runmode first on important documents - Tool automatically skips common build directories (
node_modules,target, etc.) - Backup your files before running fixes on critical documentation
๐ Troubleshooting
Debug Mode
Enable verbose logging for detailed troubleshooting:
mermaid-fixer -d ./docs --verbose
๐ค Contribute
Help improve Mermaid Fixer by reporting bugs or submitting feature requests through GitHub Issues.
Ways to Contribute
- Syntax Pattern Enhancement: Improve detection of various Mermaid syntax errors
- AI Prompt Optimization: Enhance the AI fixing prompts for better results
- LLM Provider Support: Add support for additional LLM providers
- Performance Optimization: Enhance processing speed and memory usage
- Test Coverage: Add comprehensive test cases for various diagram types
- Documentation: Improve documentation and examples
โ๏ธ Developed with
- rust - Systems programming language for performance and safety
- headless_chrome - A high-level API to control headless Chrome or Chromium over the DevTools Protocol
- clap - Command line argument parser
- serde - Serialization framework
- tokio - Asynchronous runtime for Rust
- reqwest - HTTP client for LLM API calls
๐ชช License
MIT.
๐จ About Me
๐ Help me develop this software better by sponsoring on GitHub
An experienced software developer passionate about developer tools and automation. Currently focused on building high-performance tools that improve developer productivity and code quality.
GitHub: sopaco

