From eb55fdb29fa534eb964b6151f926fecc049ced1a Mon Sep 17 00:00:00 2001 From: Alphonse Paix Date: Sat, 20 Sep 2025 16:45:16 +0200 Subject: [PATCH] markdown with GFM extension --- src/domain/post.rs | 9 +++++---- src/routes/posts.rs | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/domain/post.rs b/src/domain/post.rs index d263e0e..84b17a0 100644 --- a/src/domain/post.rs +++ b/src/domain/post.rs @@ -1,4 +1,5 @@ use chrono::{DateTime, Utc}; +use markdown::message::Message; use uuid::Uuid; pub struct PostEntry { @@ -15,10 +16,10 @@ impl PostEntry { self.published_at.format("%B %d, %Y").to_string() } - pub fn to_html(self) -> Self { - Self { - content: markdown::to_html(&self.content), - ..self + pub fn to_html(self) -> Result { + match markdown::to_html_with_options(&self.content, &markdown::Options::gfm()) { + Ok(content) => Ok(Self { content, ..self }), + Err(e) => anyhow::bail!(e), } } } diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 00e9fe7..ef51f79 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -52,7 +52,8 @@ pub async fn see_post( .await .context(format!("Failed to fetch post #{}", post_id)) .map_err(AppError::unexpected_page)? - .to_html(); + .to_html() + .context("Could not render markdown with extension.")?; let template = PostTemplate { post }; Ok(Html(template.render().unwrap()).into_response()) }