Skip to main content

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

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

BSD 3-Clause License — see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown_diagrams-1.0.0.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdown_diagrams-1.0.0-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

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

Hashes for markdown_diagrams-1.0.0.tar.gz
Algorithm Hash digest
SHA256 15371e9c7629386aeab43033c81065d0310cab22a11b6ba743b78c4a36fd2a33
MD5 a5e4ca70e24d61f1496527b4c3654d9e
BLAKE2b-256 e1c4a792d5784f47463b2413780564dd2ae81e48139ba3540480b0fa91e894f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for markdown_diagrams-1.0.0.tar.gz:

Publisher: publish.yml on bryankemp/markdown-diagrams

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file markdown_diagrams-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for markdown_diagrams-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f28403d40d96051b4cbe01e60c600fd90eda08516e025656ae79225c433a1598
MD5 a474a360aff60655a6d79199f665454b
BLAKE2b-256 651237dac289d9dc59b32a078640c3d3e48e64ab8502b45ce5eb4330d6b330c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for markdown_diagrams-1.0.0-py3-none-any.whl:

Publisher: publish.yml on bryankemp/markdown-diagrams

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page