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

Uploaded CPython 3.13+Windows x86-64

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

Uploaded CPython 3.13+macOS 11.0+ ARM64

dinja-0.4.2-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.2.tar.gz.

File metadata

  • Download URL: dinja-0.4.2.tar.gz
  • Upload date:
  • Size: 99.9 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.2.tar.gz
Algorithm Hash digest
SHA256 a6854615e579bd0265a20fc197d20e7a754e0039621412749f20512aa9cdd3fa
MD5 b41f17a89b0dab05d8ac4f9207f38ee8
BLAKE2b-256 f6d6724425ed39d9273f7b0177b0825eed66662765fc63241dd81302a55c1d17

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: dinja-0.4.2-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.2-cp313-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 dc66180d0e46a13cacdbf6b0825400c20c9474dd5d44aefa4d347381ac7e825e
MD5 7e9fa5e8157d181bda3871ab3f8cc2c5
BLAKE2b-256 d7c533eb7e2103fe95505ebe17f52b7655feeeed66631a22d18b61c5f7c90387

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.4.2-cp313-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 06b9e8bd16e9c16b79d68f143b217af5236e2f0959c7cd043d0c9920aeb8b054
MD5 18eb55b63af718ac0d9c0f6ddb1ac02c
BLAKE2b-256 8d7862195669c9cb948fa13d8141f6f7021a4493b8a49d473a62f706daac13a4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.4.2-cp313-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8305d50b07f98267021ae0e244301c54d1f7621dd6d87739293e7cae907f7910
MD5 66220a2ba33443cc8a4814554e3a055c
BLAKE2b-256 d7773bba01b946a49751a16298f539266eb83623147da53e6a9db17fbacafec4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.4.2-cp313-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 332b311e04aabc5400cb7817374b7a0098ca8d74d1125f81681ff0fa63d482f1
MD5 5d3f4ace296e78ac5e33f9734fb79374
BLAKE2b-256 664ef0d6a02e0a65df6fac71075aa74aea69b3f414306ed1939c333667a4918c

See more details on using hashes here.

Provenance

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