maciejhirsz/ramhorns

Native Markdown support

maciejhirsz opened this issue · 0 comments

I think more often than not, when rendering text into the templates these days, markdown is actually what we want. That would mean processing the markdown separately, outside of the template engine, and then pasting the resulting HTML in.

Given this is an experimental crate, I can use it to make some experiments and adding markdown support by default could be a cool feature.

The result of this:

#[derive(Content)]
struct Post<'a> {
    title: &'a str,
    body: &'a str,
}

let tpl = Template::new("<h1>{{title}}</h1><div>{{body}}</div>").unwrap();
let result = tpl.render(&Post {
    title: "Hello, _Ramhorns_!",
    body: "Now with native **Markdown** support!",
});

Would be:

<h1>Hello, <i>Ramhorns</i>!</h1><div>Now with native <b>Markdown</b> support!</div>

This can be done behind a (enabled by default) feature flag. It can also eliminate having to do extra allocations and copying buffers around, as the markdown can be parsed and then written to the output buffer by Ramhorns directly.


Other options of doing that would be either using an optional attribute on the struct:

#[derive(Content)]
struct Post<'a>
    #[md] title: &'a str,
    #[md] body: &'a str,
}

A newtype Markdown(pub T) wrapper around T: Content, such as:

#[derive(Content)]
struct Post<'a>
    title: Markdown(&'a str),
    body: Markdown(&'a str),
}

Or introducing a new syntax into the template (this is my least favorite option), such as {{* render_as_markdown }}