diff --git a/src/misc/date.rs b/src/misc/date.rs index ae58932..b9a44a4 100644 --- a/src/misc/date.rs +++ b/src/misc/date.rs @@ -2,7 +2,7 @@ use chrono::{Datelike, NaiveDate}; use ramhorns::Content; use serde::{Deserialize, Deserializer}; -#[derive(Content, Default, Debug)] +#[derive(Content, Clone, Default, Debug)] pub struct Date { pub day: u32, pub month: u32, diff --git a/src/misc/markdown.rs b/src/misc/markdown.rs index e83848b..dcf1e2f 100644 --- a/src/misc/markdown.rs +++ b/src/misc/markdown.rs @@ -12,8 +12,9 @@ use std::fs; use std::path::Path; /// Metadata for blog posts -#[derive(Content, Debug, Default, Deserialize)] +#[derive(Content, Clone, Debug, Default, Deserialize)] pub struct FileMetadataBlog { + pub hardbreaks: Option, pub title: Option, pub date: Option, pub description: Option, @@ -86,6 +87,7 @@ pub enum TypeFileMetadata { /// Usually all fields are None except one #[derive(Content, Debug, Default, Deserialize)] pub struct FileMetadata { + pub hardbreaks: bool, pub blog: Option, pub contact: Option, pub index: Option, @@ -151,7 +153,7 @@ pub fn get_options<'a>() -> ComrakOptions<'a> { // options.render.broken_link_callback = ...; // Renderer - options.render.hardbreaks = true; + options.render.hardbreaks = false; options.render.github_pre_lang = false; options.render.full_info_string = true; options.render.width = 0; // 0 mean disabled? @@ -292,14 +294,12 @@ pub fn read_md( // Find metadata let metadata = get_metadata(root, metadata_type); + // Update comrak render properties + opt.render.hardbreaks = metadata.hardbreaks; + let mermaid_name = "mermaid"; hljs_replace(root, mermaid_name); - if let TypeFileMetadata::Blog = metadata_type { - // Change by metadata could be good for compatibility - opt.render.hardbreaks = true; - } - // Convert to HTML let mut html = vec![]; format_html(root, &opt, &mut html).unwrap(); @@ -338,10 +338,15 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: &TypeFileMetadata) -> File .find_map(|node| match &node.data.borrow().value { // Extract metadata from frontmatter NodeValue::FrontMatter(text) => Some(match mtype { - TypeFileMetadata::Blog => FileMetadata { - blog: Some(deserialize_metadata(text)), - ..FileMetadata::default() - }, + TypeFileMetadata::Blog => { + let metadata: FileMetadataBlog = deserialize_metadata(text); + + FileMetadata { + blog: Some(metadata.clone()), + hardbreaks: metadata.hardbreaks.unwrap_or_default(), + ..FileMetadata::default() + } + } TypeFileMetadata::Contact => { let mut metadata: FileMetadataContact = deserialize_metadata(text); @@ -355,7 +360,10 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: &TypeFileMetadata) -> File ..FileMetadata::default() } } - TypeFileMetadata::Generic => FileMetadata::default(), + TypeFileMetadata::Generic => FileMetadata { + hardbreaks: true, + ..FileMetadata::default() + }, TypeFileMetadata::Index => FileMetadata { index: Some(deserialize_metadata(text)), ..FileMetadata::default()