Skip to main content

Python-Markdown extension to add Mermaid graph

Project description

PRs Welcome Conventional Commits Github Actions PyPI Package latest release PyPI Package download count (per month) Supported versions

markdown-mermaidjs

Python-Markdown extension to add Mermaid graph

Getting Started

Prerequisites

Installation

For pip installation (only python version >=3.x) :

pip install markdown-mermaidjs

Usage

With Python Script

import markdown


text = """
# Title

Some text.

​```mermaid
graph TB
    A --> B
    B --> C
​```

Some other text.

​```mermaid
graph TB
    D --> E
    E --> F
​```
"""

html = markdown.markdown(text, extensions=["markdown-mermaidjs"])

print(html)

Expected output

<h1>Title</h1>
<p>Some text.</p>
<pre class="mermaid">
graph TB
    A --> B
    B --> C
</pre>

<p>Some other text.</p>
<pre class="mermaid">
graph TB
    D --> E
    E --> F
</pre>

<script type="module">
    import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
    mermaid.initialize({ startOnLoad: true });
</script>

Adding Custom Icon Packs

Some newer MermaidJS diagram types (most notably Architecture), support referencing custom icon packs that are registered (i.e. https://mermaid.js.org/config/icons.html).

To register packs, you can add them to the extension config with a structure of icon_packs: {"pack_name" : "pack_url" }, i.e.:

import markdown

html = markdown.markdown(
    text,
    extensions=["markdown-mermaidjs"],
    extension_configs={
        "markdown_mermaidjs": {
            "icon_packs": {
                "logos": "https://unpkg.com/@iconify-json/logos@1/icons.json",
                "hugeicons": "https://unpkg.com/@iconify-json/hugeicons@1/icons.json",
            }
        }
    },
)

The resulting HTML should be nearly identical, but the icon packs should be registered, e.g.:

<script type="module">
    import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
    mermaid.registerIconPacks([
        { name: 'logos', loader: () => fetch('https://unpkg.com/@iconify-json/logos@1/icons.json').then((res) => res.json()) },
        { name: 'hugeicons', loader: () => fetch('https://unpkg.com/@iconify-json/hugeicons@1/icons.json').then((res) => res.json()) }
    ]);
    mermaid.initialize({ startOnLoad: true });
</script>

Use it with Pelican

Add "markdown_mermaidjs": {} to MARKDOWN["extension_configs"] in your pelicanconf.py. For the default MARKDOWN configuration, it will look like the string below:

MARKDOWN = {
    "extension_configs": {
        "markdown.extensions.codehilite": {"css_class": "highlight"},
        "markdown.extensions.extra": {},
        "markdown.extensions.meta": {},
        "markdown_mermaidjs": {},  # <------ Our addition!
    },
    "output_format": "html5",
}

Icon Packs via Pelican

Similarly, with the extension config, you can add it in the pelicanconf.py.

MARKDOWN = {
    "extension_configs": {
        "markdown_mermaidjs": {
            "icon_packs": {
                "logos": "https://unpkg.com/@iconify-json/logos@1/icons.json",
                "hugeicons": "https://unpkg.com/@iconify-json/hugeicons@1/icons.json",
            }
        },
    },
}

Contributing

See Contributing

Authors

Wei Lee weilee.rx@gmail.com

This is a forked project of oruelle/md_mermaid

Created from Lee-W/cookiecutter-python-template version 3.0.0

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_mermaidjs-2.0.0.tar.gz (87.9 kB view details)

Uploaded Source

Built Distribution

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

markdown_mermaidjs-2.0.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file markdown_mermaidjs-2.0.0.tar.gz.

File metadata

  • Download URL: markdown_mermaidjs-2.0.0.tar.gz
  • Upload date:
  • Size: 87.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for markdown_mermaidjs-2.0.0.tar.gz
Algorithm Hash digest
SHA256 96e875437a16cc936f04a3a007b65dbecd8d31e7dff36d5ad2aa7aefe64bb969
MD5 484ec7b75025330cfc11b890029f3529
BLAKE2b-256 7b1e1373dc6b128d80b479f7d902218b53b6828fcb9230194af3ae3ed052e05b

See more details on using hashes here.

File details

Details for the file markdown_mermaidjs-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for markdown_mermaidjs-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5590143c43d017ef2b325769331e3c095dea272ea06cdeead7ca4191e77099e8
MD5 6aa7aac6615b368767597a81246d2fdb
BLAKE2b-256 999c83475905bfb297f4a907c78d4e8baf7825ef422764d43be61fa731b3287a

See more details on using hashes here.

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