Render markdown from pulldown-cmark
to a template context.
#2422
-
ProblemI'm parsing markdown text to html and attempting to pass it into a template's I'm wondering if there is a way to pass raw html into the template context and have it render as expected? My setupmain.rs #[macro_use] extern crate rocket;
use rocket::{fs::FileServer};
use rocket_dyn_templates::Template;
mod ssr; // Server side rendering module.
#[launch]
fn rocket() -> _ {
rocket::build()
.attach(Template::fairing())
.mount("/", routes![ssr::index])
.mount("/public", FileServer::from("public"))
.mount("/content", FileServer::from("content"))
} ssr/mod.rs use rocket_dyn_templates::{Template, context};
use rocket::fs::relative;
#[get("/")]
pub fn index() -> Template {
let markdown = std::fs::read_to_string(relative!("/content/markdown-test.md")).unwrap();
let parser = pulldown_cmark::Parser::new(&markdown);
// Write to a new String buffer.
let mut html_output = String::new();
pulldown_cmark::html::push_html(&mut html_output, parser);
Template::render("index", context!{
title: "Home",
markdown: html_output
})
} |
Beta Was this translation helpful? Give feedback.
Answered by
SergioBenitez
Dec 11, 2022
Replies: 1 comment
-
Of course. Depends on which engine you use. In tera, you need simply mark it safe: {{ markdown | safe }} In handlebars, use: |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
2-3-5-41
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Of course. Depends on which engine you use. In tera, you need simply mark it safe:
In handlebars, use: