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.1.tar.gz (99.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.1-cp313-abi3-win_amd64.whl (17.6 MB view details)

Uploaded CPython 3.13+Windows x86-64

dinja-0.4.1-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.1-cp313-abi3-macosx_11_0_arm64.whl (18.4 MB view details)

Uploaded CPython 3.13+macOS 11.0+ ARM64

dinja-0.4.1-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.1.tar.gz.

File metadata

  • Download URL: dinja-0.4.1.tar.gz
  • Upload date:
  • Size: 99.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.1.tar.gz
Algorithm Hash digest
SHA256 a543d26c073a075b28bdaa4a0fb05c27b5e41aa5fc2fdd5f5c5893682f374cc9
MD5 69a9d966eea8d445ad54bde3371780f4
BLAKE2b-256 3f023d5e3865a7c169bfad645a9d31d6f64bd0bd8c86d71cbc2eba35ab4ce3bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for dinja-0.4.1.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.1-cp313-abi3-win_amd64.whl.

File metadata

  • Download URL: dinja-0.4.1-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.1-cp313-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f77676557bd6727a6977788f4cc0a2e6ea14c21baded65122c0e5ce88aed4345
MD5 aeb256435a23fc276e4984a6207b7cc0
BLAKE2b-256 1e9fc30c042079f2a592e9f8feea845071d8d5a9c7d8fb49ca380ea961d93c35

See more details on using hashes here.

Provenance

The following attestation bundles were made for dinja-0.4.1-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.1-cp313-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for dinja-0.4.1-cp313-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 82731d6ad1213d3389fa9b5621950c9ee6ff4db87851eb15eb3f947e5d8e2b89
MD5 bddf3aacfe5353de0d89e462e2bd22b0
BLAKE2b-256 d1bda3fca65c691dbe00a3114f6e232ff3070c70dcb02d32d40508b971c0ae06

See more details on using hashes here.

Provenance

The following attestation bundles were made for dinja-0.4.1-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.1-cp313-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dinja-0.4.1-cp313-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5f133b2a63b8a3ad624eaac98ee0046e8d2792458782a7605c9d5c6073fed817
MD5 864fb31c518d2f1ad61d9c5969e7e6f6
BLAKE2b-256 826b0ee904145b1f964490f3678a8fbd7f4e39b03191c4a13742870ec6923f3c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dinja-0.4.1-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.1-cp313-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for dinja-0.4.1-cp313-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c7d9d1518d8018e53749fdd7bd6acb575c29b64f49f0089e3ed535168f042052
MD5 3f0c58c8dfcdb6ca4b810450240a96cf
BLAKE2b-256 d208c4e8ffe1111d4bef1be305a1a4f7162a32ec01b3bbbf8f85448665ceae1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for dinja-0.4.1-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