Python-native Mermaid diagram converter – no browser, Node.js, or npm required. Renders diagrams to SVG using PhantomJS via phasma, perfect for documentation automation and CI/CD pipelines.
Project description
mmdc - Mermaid Diagram Converter
A Python tool for converting Mermaid diagrams to SVG using PhantomJS (via phasma). Perfect for automating diagram generation in documentation pipelines, CI/CD workflows, and static site generation.
Features
- Convert Mermaid to SVG: Transform
.mermaidfiles into high-quality SVG images - Multiple Diagram Types: Supports flowcharts, sequence diagrams, Gantt charts, pie charts, and more
- Command Line & Python API: Use as a CLI tool or import as a Python library
- Configurable Timeout: Set custom timeouts for complex diagrams
- Comprehensive Testing: Fully tested with parametrized tests covering various scenarios
- Logging Support: Built-in logging with verbose mode for debugging
Installation
From PyPI
pip install mmdc
From Source
git clone https://github.com/MohammadRaziei/mmdc.git
cd mmdc
pip install -e .
Usage
Command Line Interface
Convert a Mermaid file to SVG:
mmdc --input diagram.mermaid --output diagram.svg
With custom timeout (in seconds):
mmdc --input diagram.mermaid --output diagram.svg --timeout 60
Enable verbose logging:
mmdc --input diagram.mermaid --output diagram.svg --verbose
Python API
from mmdc import MermaidConverter
from pathlib import Path
converter = MermaidConverter()
# Convert a diagram
success = converter.convert(
input_file=Path("diagram.mermaid"),
output_file=Path("diagram.svg"),
timeout=30 # optional, default is 30 seconds
)
if success:
print("Conversion successful!")
else:
print("Conversion failed.")
Examples
Basic Flowchart
Create a file flowchart.mermaid:
graph TD
A[Start] --> B{Decision}
B -->|Yes| C[Action 1]
B -->|No| D[Action 2]
C --> E[End]
D --> E
Convert to SVG:
mmdc --input flowchart.mermaid --output flowchart.svg
Sequence Diagram
Create a file sequence.mermaid:
sequenceDiagram
participant Alice
participant Bob
Alice->>Bob: Hello Bob, how are you?
Bob-->>Alice: I'm good thanks!
Convert to SVG:
mmdc --input sequence.mermaid --output sequence.svg
Testing
The project includes comprehensive tests using pytest. Run the tests with:
pytest tests/ -v
Or use the hatch test environment:
hatch run test
Test Coverage
Tests cover:
- Various Mermaid diagram types (flowcharts, sequence diagrams, Gantt charts, pie charts)
- Different timeout values
- Error cases (non-existent files, empty diagrams)
- Special characters in diagrams
- Integration with the command line interface
How It Works
mmdc uses PhantomJS via the phasma Python package to render Mermaid diagrams. The process:
- Template Preparation: Uses embedded HTML/JavaScript templates in
mmdc/assets/ - Diagram Rendering: PhantomJS loads the Mermaid library and renders the diagram
- SVG Extraction: The rendered SVG is extracted and saved to the output file
- Cleanup: Temporary files are cleaned up automatically
Development
Setting Up Development Environment
-
Clone the repository:
git clone https://github.com/MohammadRaziei/mmdc.git cd mmdc
-
Install development dependencies:
pip install -e ".[dev]"
-
Run tests:
pytest tests/ -v
Code Quality
The project uses several tools for maintaining code quality:
- Black: Code formatting
- Ruff: Linting
- MyPy: Type checking
- Pytest: Testing with coverage
Run all code quality checks:
hatch run lint:all
Format code:
hatch run lint:fmt
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please ensure your code passes all tests and follows the project's coding standards.
License
mmdc is distributed under the terms of the MIT license.
Acknowledgments
- Mermaid.js for the amazing diagramming library
- PhantomJS for headless browser capabilities
- phasma for the PhantomJS Python integration
Support
If you encounter any problems or have questions, please open an issue on GitHub.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mmdc-0.2.0-py3-none-any.whl.
File metadata
- Download URL: mmdc-0.2.0-py3-none-any.whl
- Upload date:
- Size: 466.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcd8359f7443c1ad64c8b7be43cd1af343d0f46da6cae22ada705be5bb7fed8f
|
|
| MD5 |
a1ce1f24f71a2fc86fe0b44538e89e32
|
|
| BLAKE2b-256 |
578d3609e1afc3465d11c564c533da7b1402fb42822e58e17c1347289e4a715d
|