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.0.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.0-cp313-abi3-win_amd64.whl (17.5 MB view details)

Uploaded CPython 3.13+Windows x86-64

dinja-0.3.0-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.0-cp313-abi3-macosx_11_0_arm64.whl (18.3 MB view details)

Uploaded CPython 3.13+macOS 11.0+ ARM64

dinja-0.3.0-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.0.tar.gz.

File metadata

  • Download URL: dinja-0.3.0.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.0.tar.gz
Algorithm Hash digest
SHA256 fbc260833bc3f47484cb343093146b5fae2577276c3dea2f964ccd1b651104d4
MD5 84cb4c970e9fd35d87ec0bec9a9b36ff
BLAKE2b-256 941fe1f38d42a1f18667fa687e2cac7e24289820279729c3ac27bcebe6f5ba68

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: dinja-0.3.0-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.0-cp313-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b075267fdd3203f6dbee0bc2cfdb8884f7ba73731a0470b99b9f598d994bc690
MD5 031e02c59566dd2178adfea655a76ca5
BLAKE2b-256 3afc82d3350aa7c59c8badb0bac0c1e3080df752f923062a9ba8c82f22d506fb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.3.0-cp313-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 1130f8b9e09486fbdf8f4cb2d1564d1c08ece131ea0888ad9c3660f2b622c817
MD5 f3a1992895342dddc38e38ffa30722cc
BLAKE2b-256 1c3ce93845a453d9125ce16c62d8664c688a7e5d3980af84250331536f4b279f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.3.0-cp313-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 638a2b4e1ea0124eaa300e1661303f94dcc73b2966be6abe9a1891fa9b42f658
MD5 8181e03068b001053f6925035fda3c97
BLAKE2b-256 65231866a92cde945f36cfde5a8d9c588711d582b0cd66a8da1c55e7f6b43053

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.3.0-cp313-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 898552ec112a3bdac5d698650d26bd5b4691f926dd4e1080bd5c4aeaeb9851dd
MD5 35b2835468208839023c7468a31c19af
BLAKE2b-256 20fdc2e73936f3f777280c46a58b99bed6629e83fd7f6fec8166f1227ebe1dd0

See more details on using hashes here.

Provenance

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