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": {
                "code": """
                    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.3.1.tar.gz (92.6 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.3.1-cp313-abi3-win_amd64.whl (17.5 MB view details)

Uploaded CPython 3.13+Windows x86-64

dinja-0.3.1-cp313-abi3-manylinux_2_34_x86_64.whl (20.3 MB view details)

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

dinja-0.3.1-cp313-abi3-macosx_11_0_arm64.whl (18.3 MB view details)

Uploaded CPython 3.13+macOS 11.0+ ARM64

dinja-0.3.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.3.1.tar.gz.

File metadata

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

File hashes

Hashes for dinja-0.3.1.tar.gz
Algorithm Hash digest
SHA256 38e997eb9abdd0ac35ddaca23d1d7f23af21022df182c782be346e206df848dd
MD5 3e434bc64ad7553574a45f028092bca9
BLAKE2b-256 04c0a70be0c919b177c3eac777a6ffcffbca673a2f4a5137c1de0281eb19692f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: dinja-0.3.1-cp313-abi3-win_amd64.whl
  • Upload date:
  • Size: 17.5 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.3.1-cp313-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4bf191f73d12803b36fc5e583eb2b7258e17f7949b976054cfb93b064b24b15a
MD5 c6a4a4268af92412a8a0d9b23ebeebac
BLAKE2b-256 cfe7f18f878ec6c667e9e04b05c0f0dbb5458cf48dee2921723dda9d3bdb1b2a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.3.1-cp313-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c0703d4c741d5f3f3f89e338bc2d5a0a103842b8154bf2302bf6b495bee8c30d
MD5 892315f643e712b856647b6b292e7b41
BLAKE2b-256 9b3c00a9ce8ba4c71c88e2cf2a04bede4d45fbe24f59c8154a62dbbead23409b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.3.1-cp313-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b6ab2d113abf92816aacfc8379855e007b5828ec3f2c62eed1755b549c2cf60c
MD5 158406847197a11ad96899df018bf951
BLAKE2b-256 cc828ca84654865c4e19a81ae4a015f6d03284815ff6453765bace1432dfc3ed

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.3.1-cp313-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 326d13183ad746e90da658b5888c57dbd952f36ba7940bf116632d6261dea881
MD5 afcc6427cf7cabcf6a7d2e660631dd55
BLAKE2b-256 8bb30424f15b0b90355e97457b59deb7cc91ec8675861b940e977920a4f56733

See more details on using hashes here.

Provenance

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