Skip to main content

Safe, deterministic MDX rendering powered by a Rust core with batteries-included Python bindings

Project description

Dinja Logo

Documentation Python Rust GitHub stars License

Dinja

Safe, deterministic MDX rendering powered by a Rust core with batteries-included Python bindings.

Links

Installation

Target Command
Python package uv add dinja
Rust crate cargo add dinja-core

Usage

Rust

use dinja_core::service::{RenderService, RenderServiceConfig};

fn main() -> anyhow::Result<()> {
    let config = RenderServiceConfig::default();
    let service = RenderService::new(config)?;

    let html = service.render_string("example.mdx", "# Hello **dinja**")?;
    println!("{html}");
    Ok(())
}

Python

from dinja import Renderer, Input, Settings

# Create a renderer instance (engine loads once)
renderer = Renderer()

# Render MDX content with type-safe dataclasses
result = renderer.render(
    Input(
        mdx={"example.mdx": "---\ntitle: Demo\n---\n# Hello **dinja**"},
        settings=Settings(output="html", minify=True),
    )
)

entry = result["files"]["example.mdx"]

if entry["status"] == "success":
    rendered = entry["result"]
    metadata = rendered.get("metadata", {})
    print("title:", metadata.get("title"))
    print("html:", rendered.get("output"))
else:
    print("error:", entry.get("error"))

# Reuse the same instance for multiple renders with different modes
result1 = renderer.render(
    Input(
        mdx={"page1.mdx": "# Page 1"},
        settings=Settings(output="html"),
    )
)

result2 = renderer.render(
    Input(
        mdx={"page2.mdx": "# Page 2"},
        settings=Settings(output="schema"),
    )
)

Accessing Metadata in MDX

Metadata from YAML frontmatter is available via the context function:

result = renderer.render(
    Input(
        mdx={
            "page.mdx": """
---
title: Welcome
author: Alice
---
# {context('title')}

By {context('author')}
"""
        },
        settings=Settings(output="html"),
    )
)

The context function supports nested paths: context('author.name') for nested metadata.

Using Global Utils

You can inject global JavaScript utilities that are available in all components:

result = renderer.render(
    Input(
        mdx={"page.mdx": "<Greeting name='Alice' />"},
        settings=Settings(
            output="html",
            utils="export default { greeting: 'Hello', emoji: '👋' }",
        ),
        components={
            "Greeting": """
                export default function Component(props) {
                    return <div>{utils.greeting} {props.name} {utils.emoji}</div>;
                }
            """,
        },
    )
)

The utils object must be exported using export default { ... } and will be available globally as utils in all component code. Invalid utils code is silently ignored.

rendered["output"] contains HTML, JavaScript, or schema code depending on settings.output.

More examples live in python-bindings/examples/.

License

BSD 3-Clause. See LICENSE.

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

dinja-0.4.3.tar.gz (103.4 kB view details)

Uploaded Source

Built Distributions

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

dinja-0.4.3-cp313-abi3-win_amd64.whl (17.6 MB view details)

Uploaded CPython 3.13+Windows x86-64

dinja-0.4.3-cp313-abi3-manylinux_2_34_x86_64.whl (20.4 MB view details)

Uploaded CPython 3.13+manylinux: glibc 2.34+ x86-64

dinja-0.4.3-cp313-abi3-macosx_11_0_arm64.whl (18.4 MB view details)

Uploaded CPython 3.13+macOS 11.0+ ARM64

dinja-0.4.3-cp313-abi3-macosx_10_12_x86_64.whl (19.1 MB view details)

Uploaded CPython 3.13+macOS 10.12+ x86-64

File details

Details for the file dinja-0.4.3.tar.gz.

File metadata

  • Download URL: dinja-0.4.3.tar.gz
  • Upload date:
  • Size: 103.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dinja-0.4.3.tar.gz
Algorithm Hash digest
SHA256 d29f6279879319248356f9764fd3a40cb4b4b4668ec57886ed526e4b5ced0ba0
MD5 51c94649476c4dde4eb62899147a3b16
BLAKE2b-256 3da6a7289bf7c1eb50b1b391699634357d97ba5b6045a0d62cbc50f7932f1cf9

See more details on using hashes here.

Provenance

The following attestation bundles were made for dinja-0.4.3.tar.gz:

Publisher: release.yml on hlop3z/dinja

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

File details

Details for the file dinja-0.4.3-cp313-abi3-win_amd64.whl.

File metadata

  • Download URL: dinja-0.4.3-cp313-abi3-win_amd64.whl
  • Upload date:
  • Size: 17.6 MB
  • Tags: CPython 3.13+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dinja-0.4.3-cp313-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 12eebd6188edc09b240ba370fb1268f9f0e579c8ffe6e20d2edbdafc53bee9d0
MD5 22bd33346bfdf34e72e837472db63097
BLAKE2b-256 a260594f3a7ac8ff3109c7ecf986ceccf4beb32df8e408a00b560f72a9fdb32e

See more details on using hashes here.

Provenance

The following attestation bundles were made for dinja-0.4.3-cp313-abi3-win_amd64.whl:

Publisher: release.yml on hlop3z/dinja

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

File details

Details for the file dinja-0.4.3-cp313-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for dinja-0.4.3-cp313-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 e723c2b913815342dac8cde3f17d580291273b40f0472ef04865a49f568bec63
MD5 7e53ef7e4fcb4e6a3ddd5c91159108dd
BLAKE2b-256 2e0f96ba4c6c5dfbb1cbfa8df075f4abdbc2167c08777f98cfa2da1bc9f1ed0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for dinja-0.4.3-cp313-abi3-manylinux_2_34_x86_64.whl:

Publisher: release.yml on hlop3z/dinja

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

File details

Details for the file dinja-0.4.3-cp313-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dinja-0.4.3-cp313-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a7029f18eab7f2e0ed37f1efeef4b14d574f5a692ab9259b980eb9ddfca5cbe1
MD5 70a5129fb755a45b575359749cbaa349
BLAKE2b-256 a67ceeb124c3ff514b01e134ca45ab44df443d7c2f824941a6b48659ae2018e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for dinja-0.4.3-cp313-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on hlop3z/dinja

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

File details

Details for the file dinja-0.4.3-cp313-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for dinja-0.4.3-cp313-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 99320ed928162c0ca6389ca2c160534dd1b55f9cf50d4f42aa259ba9dc055df9
MD5 4d1a524f6eb79e1440f17de6455d8767
BLAKE2b-256 2a95a341974ac90ec677a2d3b75aa468f6431e7b2a5788c9028aff63c281c372

See more details on using hashes here.

Provenance

The following attestation bundles were made for dinja-0.4.3-cp313-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on hlop3z/dinja

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