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

Uploaded CPython 3.13+Windows x86-64

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

Uploaded CPython 3.13+macOS 11.0+ ARM64

dinja-0.4.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.4.0.tar.gz.

File metadata

  • Download URL: dinja-0.4.0.tar.gz
  • Upload date:
  • Size: 95.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.4.0.tar.gz
Algorithm Hash digest
SHA256 cd6955e986e169425f2582b02d50b355081e73d8d769b4ac58741a8c5922a9a8
MD5 d2863236eb42c57230679b488fee5741
BLAKE2b-256 73a5d51070465a5026da5e1ff1a5dde3d9afc7777d324589e9f6b5cbb47c0470

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: dinja-0.4.0-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.0-cp313-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0f8856e3f373beac372106b9a772acb7b90ba43f4d7560967aae21cc59750f2a
MD5 4c84720a43faeded82286d8c08fa295c
BLAKE2b-256 d5475d9a03ded2e80dffd2bd13f75eda148dd0af578c388532e18aca06420838

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.4.0-cp313-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 7966271355847a759de27d025f6b5eb4f774b293d13af417a0f29529eb199287
MD5 602294d911deb8187a755e925d54626a
BLAKE2b-256 495094c7ea24c9819097f2f825c00a031bda085722bfa7ee0b20420d1cf7eeb2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.4.0-cp313-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1475dbf4683bf01805a3a2abe0366931b1047f821ed2d7e6c2d5f3f3903d3601
MD5 7b131ebaaede27a655c66104227a74d9
BLAKE2b-256 b2d94d7b4ecc29e98b95a3d61f156da83c07a8589053880ac5d4922942d3a923

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for dinja-0.4.0-cp313-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 436051d357a37ac11583394e4f399901a93c4bd57428a762a5f762d0a3338fd2
MD5 10f0495dc88c6bb9074c8af9de5a0691
BLAKE2b-256 467ed9e96e74fc45204cf0a00394d10e4d574c57a8ce97e179b38d92007b739e

See more details on using hashes here.

Provenance

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