In the world of technical documentation and diagramming, Mermaid has become an invaluable tool for creating clear, version-controllable diagrams using simple text-based syntax. However, sharing these diagrams outside of Markdown-rendering environments or in high-resolution formats can be challenging.
MermaidPix bridges this gap by automatically converting Mermaid diagrams in your Markdown files into high-resolution PNG images. This tool ensures that your diagrams are:
- Easily shareable in any context.
- Rendered in high quality for presentations or publications.
- Consistently generated, allowing for version control of both the diagram code and the resulting image.
MermaidPix is a Python-based command-line tool that:
- Scans Markdown files for Mermaid diagram code blocks.
- Converts each Mermaid diagram to a high-resolution PNG image.
- Replaces the Mermaid code in the Markdown with a link to the generated image.
- Maintains idempotency by generating consistent filenames based on diagram content.
- High-resolution output (4K) with transparent backgrounds.
- Deterministic file naming for easy version control.
- Efficient processing with caching of previously generated images.
- Verbose logging option for detailed conversion information.
- Python 3.7 or higher.
- pipx (for installing Python applications globally).
- Poetry (for dependency management and packaging).
- Mermaid CLI (mmdc).
If you haven't installed pipx yet, you can do so using pip:
pip install --user pipx
pipx ensurepath
Install using Pypi:
pipx install mermaidpix
Use pipx to install Poetry:
pipx install poetry
Install Mermaid CLI globally using npm:
npm install -g @mermaid-js/mermaid-cli
Clone the repository and install MermaidPix using Poetry:
git clone https://github.com/raphaelmansuy/mermaidpix.git
cd mermaidpix
poetry install
This will create a virtual environment and install all the necessary dependencies.
To use MermaidPix, you can run it directly through Poetry:
poetry run python mermaid_pix/main.py <input_file> <output_file> [-v]
Alternatively, you can activate the Poetry shell and run the script:
poetry shell
python mermaid_pix/main.py <input_file> <output_file> [-v]
<input_file>
: Path to the input Markdown file.<output_file>
: Path where the processed Markdown file will be saved.-v
or--verbose
: (Optional) Enable verbose logging.
poetry run python mermaid_pix/main.py docs/architecture.md docs/architecture_with_images.md -v
This command will:
- Read
docs/architecture.md
. - Convert any Mermaid diagrams to PNGs and save them in the same directory as the output file.
- Create a new file
docs/architecture_with_images.md
with image links replacing Mermaid code blocks. - Provide verbose output of the conversion process.
Input Markdown (input.md
):
# System Architecture
Here's our current system architecture:
graph TD
A[Client] -->|HTTP Request| B(Load Balancer)
B -->|Forward| C{Web Server}
C -->|Query| D[(Database)]
C -->|Cache| E((Redis))
This diagram shows the basic flow of our system.
After running MermaidPix:
poetry run python mermaid_pix/main.py input.md output.md
Output Markdown (output.md
):
# System Architecture
Here's our current system architecture:
![Mermaid Diagram](images/diagrams/mermaid_<hash>.png)
This diagram shows the basic flow of our system.
The Mermaid diagram is now converted to a high-resolution PNG image, making it easily viewable in any Markdown reader or web browser.
To set up MermaidPix for development:
-
Clone the repository:
git clone https://github.com/yourusername/mermaidpix.git cd mermaidpix
-
Install dependencies:
poetry install
-
Activate the virtual environment:
poetry shell
-
Make your changes and run tests:
poetry run pytest
We welcome contributions to MermaidPix! Please see our CONTRIBUTING.md for details on how to submit pull requests, report issues, or request features.
MermaidPix is released under the MIT License. See the LICENSE file for details.