Skip to main content

Mermaid extension for Python-Markdown using mermaid-cli.

Project description

markdown-mermaid-cli

Mermaid extension for Python-Markdown using Mermaid-CLI.

This extension converts Mermaid diagram code blocks into Base64 encoded data: URI. This enables PDF generation with tools like MkDocs to PDF/WeasyPrint without requiring client-side JavaScript.

Install

pip install markdown-mermaid-cli

Requirements

Mermaid CLI

Chrome or Chromium is required to run Mermaid-CLI.

npm install @mermaid-js/mermaid-cli

or

npm install --global @mermaid-js/mermaid-cli

Usage

  • code block start with ```mermaid
  • code block end with ```

options:

formant=[svg|png] {img attribute}="value" {cli option}="value"`
  • format (optional): Output image format (defaults to svg)
  • img attribute (optional): alt, width, height, class, id, style, title
  • cli option (optional): theme, width, height, backgroundColor, svgId, scale (refer to mmdc -h)

MkDocs Integration

# mkdocs.yml
markdown_extensions:
  - markdown_mermaid_cli

Pelican Integration

# pelicanconf.py
MARKDOWN = {
    'extension_configs': {
        'markdown.extensions.codehilite': {'css_class': 'highlight'},
        'markdown.extensions.extra': {},
        'markdown_mermaid_cli': {},             # Add this
    },
    'output_format': 'html5',
}

Python code

import markdown
from markdown_mermaid_cli import MermaidExtension

markdown_text = """```mermaid
sequenceDiagram
    participant Alice
    participant Bob
    Bob->>Alice: Hi Alice
    Alice->>Bob: Hi Bob
```"""

html_output = markdown.markdown(
    markdown_text, extensions=[MermaidExtension()]
)

print(html_output)
<p><img src="data:image/svg+xml;base64,PHN2ZyBhcmlhLXJvbGVkZXNjcmlwdGlvbj0ic2VxdWVuY2UiIHJvbGU
9ImdyYXBoaWNzLWRvY3VtZW50IGRvY3VtZW50IiB2aWV3Qm94PSItNTAgLTEwIDc1MCA1NzQiIHN0eWxlPSJtYXgtd2lkd
Gg6IDc1MHB4OyBiYWNrZ3JvdW5kLWNvbG9yOiB3aGl0ZTsiIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk
...
...
...
IHgxPSIyNzYiLz48L3N2Zz4=" ></p>

Process flow

sequenceDiagram
    participant application as Application<br/>(eg MkDocs)
    participant markdown as Python Markdown
    participant extension as MermaidDataURIExtension
    participant engine as Mermaid CLI

    application->>markdown: Markdown + Mermaid
    markdown->>extension: Preprocessor
    extension->>engine: Mermaid
    engine-->>engine: Convert
    engine-->>extension: Image Data
    extension-->>extension: Base64 encode
    extension-->>markdown: Markdown + data URI image
    markdown-->>application: HTML + data URI image

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_mermaid_cli-0.2.0.tar.gz (70.0 kB view details)

Uploaded Source

Built Distribution

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

markdown_mermaid_cli-0.2.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file markdown_mermaid_cli-0.2.0.tar.gz.

File metadata

  • Download URL: markdown_mermaid_cli-0.2.0.tar.gz
  • Upload date:
  • Size: 70.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for markdown_mermaid_cli-0.2.0.tar.gz
Algorithm Hash digest
SHA256 713b874dafd128f69a3a7d99e5b738a111b680f58ac5350c0489183005fc55d2
MD5 0e558a91f35a7a9324459adab6abd098
BLAKE2b-256 d1e7e78b3bc5454915969586cbecf4e0438672c1a03190b2fbf2df52472f958c

See more details on using hashes here.

Provenance

The following attestation bundles were made for markdown_mermaid_cli-0.2.0.tar.gz:

Publisher: publish-to-pypi.yml on hkato/markdown-mermaid-cli

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_mermaid_cli-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for markdown_mermaid_cli-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f9f7d13f68b9cfbd46609af502baca9487314e51bc00b365f3a9bc25594c6cd
MD5 9a9a3bb8e623bc9686fee004d30dba9b
BLAKE2b-256 a5593e5c1d851dd412f3df8220bd04139b4b04afe805b6b8586540fbcafb894e

See more details on using hashes here.

Provenance

The following attestation bundles were made for markdown_mermaid_cli-0.2.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on hkato/markdown-mermaid-cli

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