Keats / tera

A template engine for Rust based on Jinja2/Django
http://keats.github.io/tera/
MIT License
3.43k stars 280 forks source link

[Feature Request] Markdown blocks #411

Closed 64 closed 5 years ago

64 commented 5 years ago

It would be great to have some kind of feature flag on this crate which enables support for markdown rendering. You'd do something like:

tera = { version = "x.y.z", features = ["markdown"] }

Then inside a template, you could do:

{% markdown %}
    # Hello world!
{% endmarkdown %}

And it would render as:

<h1>Hello world!</h1>

You could use pulldown-cmark for this.

Keats commented 5 years ago

I think that's pretty unlikely to be added in Tera itself as it can be added to whatever app you have in 5 minutes https://github.com/getzola/zola/blob/master/components/templates/src/filters.rs#L7-L31

64 commented 5 years ago

Fair enough. I don't mind if you close this issue.

philpax commented 9 months ago

For those visiting from the future: Zola's Markdown logic (as linked to there) is much more complex now as it interfaces with Tera.

If you're just looking for basic Markdown rendering and don't need anything more complex, you can do this:

[dependencies]
markdown = "1.0.0-alpha.14" # or 1.0, if released when you're reading this
tera.register_filter(
    "markdown",
    |value: &tera::Value, _: &_| -> tera::Result<tera::Value> {
        let value = tera::from_value::<String>(value.clone())?;
        let value = markdown::to_html(&value);
        Ok(tera::to_value(value)?)
    },
);
{{ package.description | markdown }}