Extract and render Mermaid diagrams from Markdown files
Project description
markdown-diagrams
Extract and render Mermaid diagrams from Markdown files to PNG, SVG, and PDF.
Features
- Extract Mermaid diagrams from any Markdown file
- Render diagrams to PNG, SVG, and PDF formats
- Automatically name output files based on bold-text titles or document headings
- Support for 19 diagram types (flowchart, sequence, ER, class, state, gantt, and more)
- Extensible architecture for additional diagram types
Prerequisites
Rendering requires the Mermaid CLI:
npm install -g @mermaid-js/mermaid-cli
Installation
From PyPI
pip install markdown-diagrams
From source
git clone https://github.com/bryankemp/markdown-diagrams.git
cd markdown-diagrams
pip install -e .
Usage
Extract and render all diagrams
markdown-diagrams extract document.md
Specify an output directory
markdown-diagrams extract document.md -o ./output
Filter by diagram type
markdown-diagrams extract document.md -t mermaid -t flowchart
Choose an output format
# PNG (default)
markdown-diagrams extract document.md -f png
# SVG
markdown-diagrams extract document.md -f svg
# PDF
markdown-diagrams extract document.md -f pdf
Custom dimensions and theme
markdown-diagrams extract document.md -w 1000 -H 800 -T dark
Validate diagrams
# Check all diagrams for syntax errors
markdown-diagrams validate document.md
# Auto-fix common issues (whitespace, unquoted labels)
markdown-diagrams validate document.md --fix
# Preview fixes without writing
markdown-diagrams validate document.md --dry-run
Supported Diagram Types
mermaid · flowchart · sequence · graph · gantt · pie · mindmap ·
quadrantDiagram · er · class · state · journey · gitGraph ·
userJourney · requirement · c4 · entityRelationship · component · active
Filename strategy
When a bold-text title (e.g. **Flowchart – License Injection**) appears
directly above a code fence, it is used as the output filename. Otherwise
the nearest #-heading is used. This means documents that follow the
common pattern of a generic ### Diagrams heading with descriptive bold
titles per diagram will get meaningful filenames automatically.
Python API
from markdown_diagrams import extract_diagrams, render_mermaid_diagram
from pathlib import Path
# Extract diagrams
diagrams = extract_diagrams(Path("document.md"))
for diagram_type, items in diagrams.items():
for item in items:
print(f"[{diagram_type}] {item['heading']}: {len(item['content'])} chars")
# Render a single diagram
render_mermaid_diagram(items[0]["content"], output_dir="./output")
Examples
See examples/all_diagram_types.md for a
complete reference with every supported diagram type.
Development
pip install -e ".[dev]"
pytest
black --check .
ruff check .
Documentation
Full documentation is available at markdown-diagrams.readthedocs.io.
Contributing
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
License
BSD 3-Clause License — see LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 markdown_diagrams-1.0.0.tar.gz.
File metadata
- Download URL: markdown_diagrams-1.0.0.tar.gz
- Upload date:
- Size: 24.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15371e9c7629386aeab43033c81065d0310cab22a11b6ba743b78c4a36fd2a33
|
|
| MD5 |
a5e4ca70e24d61f1496527b4c3654d9e
|
|
| BLAKE2b-256 |
e1c4a792d5784f47463b2413780564dd2ae81e48139ba3540480b0fa91e894f7
|
Provenance
The following attestation bundles were made for markdown_diagrams-1.0.0.tar.gz:
Publisher:
publish.yml on bryankemp/markdown-diagrams
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
markdown_diagrams-1.0.0.tar.gz -
Subject digest:
15371e9c7629386aeab43033c81065d0310cab22a11b6ba743b78c4a36fd2a33 - Sigstore transparency entry: 1108702763
- Sigstore integration time:
-
Permalink:
bryankemp/markdown-diagrams@19732e4795eceb0ff8d3cf4272218095c0977d5a -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/bryankemp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@19732e4795eceb0ff8d3cf4272218095c0977d5a -
Trigger Event:
release
-
Statement type:
File details
Details for the file markdown_diagrams-1.0.0-py3-none-any.whl.
File metadata
- Download URL: markdown_diagrams-1.0.0-py3-none-any.whl
- Upload date:
- Size: 18.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f28403d40d96051b4cbe01e60c600fd90eda08516e025656ae79225c433a1598
|
|
| MD5 |
a474a360aff60655a6d79199f665454b
|
|
| BLAKE2b-256 |
651237dac289d9dc59b32a078640c3d3e48e64ab8502b45ce5eb4330d6b330c4
|
Provenance
The following attestation bundles were made for markdown_diagrams-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on bryankemp/markdown-diagrams
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
markdown_diagrams-1.0.0-py3-none-any.whl -
Subject digest:
f28403d40d96051b4cbe01e60c600fd90eda08516e025656ae79225c433a1598 - Sigstore transparency entry: 1108702816
- Sigstore integration time:
-
Permalink:
bryankemp/markdown-diagrams@19732e4795eceb0ff8d3cf4272218095c0977d5a -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/bryankemp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@19732e4795eceb0ff8d3cf4272218095c0977d5a -
Trigger Event:
release
-
Statement type: