From 0c2e4b7624bf91f5c3ceb6accdacbf68075ac82e Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Wed, 2 Apr 2025 18:19:06 +0200 Subject: [PATCH] Add markdown rendering for recipe descriptions using comrak --- backend/Cargo.toml | 1 + backend/src/html_templates.rs | 10 ++++++++++ backend/templates/recipe_view.html | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 3b214fd..e399a92 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -28,6 +28,7 @@ clap = { version = "4", features = ["derive"] } sqlx = { version = "0.8", features = ["sqlite", "runtime-tokio", "chrono"] } askama = "0.13" +comrak = "0.37" argon2 = { version = "0.5", features = ["default", "std"] } rand_core = { version = "0.9", features = ["std"] } diff --git a/backend/src/html_templates.rs b/backend/src/html_templates.rs index 138265a..4e71a2b 100644 --- a/backend/src/html_templates.rs +++ b/backend/src/html_templates.rs @@ -18,6 +18,16 @@ impl Recipes { } } +mod filters { + use askama::filters::Safe; + pub fn markdown(s: T) -> askama::Result> { + Ok(Safe(comrak::markdown_to_html( + &s.to_string(), + &comrak::ComrakOptions::default(), + ))) + } +} + #[derive(Template)] #[template(path = "home.html")] pub struct HomeTemplate { diff --git a/backend/templates/recipe_view.html b/backend/templates/recipe_view.html index 54ce4e3..01979e9 100644 --- a/backend/templates/recipe_view.html +++ b/backend/templates/recipe_view.html @@ -51,7 +51,7 @@ {% if !recipe.description.is_empty() %}
- {{ recipe.description }} + {{ recipe.description | markdown }}
{% endif %}