Skip to main content

Diagram extension for Python-Markdown using Kroki server.

Project description

markdown-kroki

PyPI version License Python versions

Diagram extension for Python-Markdown using Kroki server.

This extension converts various diagram code blocks into Base64 encoded data: URI or direct image link. This enables PDF generation with tools like MkDocs to PDF/WeasyPrint without requiring JavaScript(e.g. Mermaid)

Install

pip install markdown-kroki

Requirements

Internet access to the public Kroki server

Default setting with no options.

Self-Managed Kroki server (recommended)

Here is a sample Docker Compose file.

ref. Kroki.io > Install > Using Docker or Podman

docker compose up -d

The default port used by MkDocs (mkdocs serve) may conflict with the default port of a Dockerized Kroki instance. Consequently, you will need to change the port configuration for one of them.

Usage

```{diagram language} formant=[svg|png] {img tag attribute}="value" {diagram option}="value"
```
  • format (optional): Output image format default to svg
  • img tag attribute (optional): alt, width, height, class, id, style, title
  • diagram option (optional): refer to Diagram options

MkDocs Integration

# mkdocs.yml
markdown_extensions:
  - markdown_kroki:
      kroki_url: http://localhost:18000  # default: https://kroki.io
      img_src: link                      # default: data/data URI, link/direct link

Pelican Integration

# pelicanconf.py
MARKDOWN = {
    'extension_configs': {
        'markdown.extensions.codehilite': {'css_class': 'highlight'},
        'markdown.extensions.extra': {},
        'markdown_kroki': {                         # Add these
            'kroki_url': 'http://localhost:18000',
            'img_src': 'link'
        },
    },
    'output_format': 'html5',
}

Python code

import markdown
from markdown_kroki import KrokiDiagramExtension

markdown_text = """```plantuml format="svg" theme="sketchy-outline" width="300"
@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml
```"""

html_output = markdown.markdown(markdown_text, extensions=[
                                KrokiDiagramExtension(kroki_url='https://kroki.io')])

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

Process flow

data URI

sequenceDiagram
    participant application as MkDocs, Pelican<br/>or your application
    participant markdown as Python Markdown
    participant extension as KrokiDiagramExtension
    participant engine as Kroki Server

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

Direct link (GET API)

sequenceDiagram
    participant application as MkDocs, Pelican<br/>or your application
    participant markdown as Python Markdown
    participant extension as KrokiDiagramExtension
    participant engine as Kroki Server

    application->>markdown: Markdown + Diagrams
    markdown->>extension: Preprocessor
    extension-->>extension: Encoded code<br/>base64+deflate
    extension-->>markdown: Markdown + Kroki direct link
    markdown-->>application: HTML + Kroki direct kink
    application->>engine: GET API<br/><img src="http[s]">
    engine-->>application: Image data

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_kroki-0.4.0.tar.gz (99.8 kB view details)

Uploaded Source

Built Distribution

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

markdown_kroki-0.4.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file markdown_kroki-0.4.0.tar.gz.

File metadata

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

File hashes

Hashes for markdown_kroki-0.4.0.tar.gz
Algorithm Hash digest
SHA256 cb5452c4a5841a7cfb525b292e5b7e7717c7483d70a63c85d9be95fcefb733f4
MD5 c272990d04294232baaf12498bb6b165
BLAKE2b-256 db2af946b5dd4886141377cc313bf7fe722f8550dede5bc911310933c48a2aa3

See more details on using hashes here.

Provenance

The following attestation bundles were made for markdown_kroki-0.4.0.tar.gz:

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

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_kroki-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: markdown_kroki-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for markdown_kroki-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ee81f2fe918bc59f3f17bb94ac027cdd04ae723ef0bfc98525ba7c013db8225
MD5 7fc5b07cedb996700c6bff93732a14f4
BLAKE2b-256 31c1f880c09c32bffa16096438aaaec3ca6eb0c7bc17036cc7cf22f05f84c343

See more details on using hashes here.

Provenance

The following attestation bundles were made for markdown_kroki-0.4.0-py3-none-any.whl:

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

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