hardbreak option for blogs, true for generic, false in other cases

This commit is contained in:
Mylloon 2024-11-06 19:15:38 +01:00
parent e54cd44714
commit 58c1b8a21a
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 21 additions and 13 deletions

View file

@ -2,7 +2,7 @@ use chrono::{Datelike, NaiveDate};
use ramhorns::Content; use ramhorns::Content;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
#[derive(Content, Default, Debug)] #[derive(Content, Clone, Default, Debug)]
pub struct Date { pub struct Date {
pub day: u32, pub day: u32,
pub month: u32, pub month: u32,

View file

@ -12,8 +12,9 @@ use std::fs;
use std::path::Path; use std::path::Path;
/// Metadata for blog posts /// Metadata for blog posts
#[derive(Content, Debug, Default, Deserialize)] #[derive(Content, Clone, Debug, Default, Deserialize)]
pub struct FileMetadataBlog { pub struct FileMetadataBlog {
pub hardbreaks: Option<bool>,
pub title: Option<String>, pub title: Option<String>,
pub date: Option<Date>, pub date: Option<Date>,
pub description: Option<String>, pub description: Option<String>,
@ -86,6 +87,7 @@ pub enum TypeFileMetadata {
/// Usually all fields are None except one /// Usually all fields are None except one
#[derive(Content, Debug, Default, Deserialize)] #[derive(Content, Debug, Default, Deserialize)]
pub struct FileMetadata { pub struct FileMetadata {
pub hardbreaks: bool,
pub blog: Option<FileMetadataBlog>, pub blog: Option<FileMetadataBlog>,
pub contact: Option<FileMetadataContact>, pub contact: Option<FileMetadataContact>,
pub index: Option<FileMetadataIndex>, pub index: Option<FileMetadataIndex>,
@ -151,7 +153,7 @@ pub fn get_options<'a>() -> ComrakOptions<'a> {
// options.render.broken_link_callback = ...; // options.render.broken_link_callback = ...;
// Renderer // Renderer
options.render.hardbreaks = true; options.render.hardbreaks = false;
options.render.github_pre_lang = false; options.render.github_pre_lang = false;
options.render.full_info_string = true; options.render.full_info_string = true;
options.render.width = 0; // 0 mean disabled? options.render.width = 0; // 0 mean disabled?
@ -292,14 +294,12 @@ pub fn read_md(
// Find metadata // Find metadata
let metadata = get_metadata(root, metadata_type); let metadata = get_metadata(root, metadata_type);
// Update comrak render properties
opt.render.hardbreaks = metadata.hardbreaks;
let mermaid_name = "mermaid"; let mermaid_name = "mermaid";
hljs_replace(root, mermaid_name); 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 // Convert to HTML
let mut html = vec![]; let mut html = vec![];
format_html(root, &opt, &mut html).unwrap(); 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 { .find_map(|node| match &node.data.borrow().value {
// Extract metadata from frontmatter // Extract metadata from frontmatter
NodeValue::FrontMatter(text) => Some(match mtype { NodeValue::FrontMatter(text) => Some(match mtype {
TypeFileMetadata::Blog => FileMetadata { TypeFileMetadata::Blog => {
blog: Some(deserialize_metadata(text)), let metadata: FileMetadataBlog = deserialize_metadata(text);
FileMetadata {
blog: Some(metadata.clone()),
hardbreaks: metadata.hardbreaks.unwrap_or_default(),
..FileMetadata::default() ..FileMetadata::default()
}, }
}
TypeFileMetadata::Contact => { TypeFileMetadata::Contact => {
let mut metadata: FileMetadataContact = deserialize_metadata(text); 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() ..FileMetadata::default()
} }
} }
TypeFileMetadata::Generic => FileMetadata::default(), TypeFileMetadata::Generic => FileMetadata {
hardbreaks: true,
..FileMetadata::default()
},
TypeFileMetadata::Index => FileMetadata { TypeFileMetadata::Index => FileMetadata {
index: Some(deserialize_metadata(text)), index: Some(deserialize_metadata(text)),
..FileMetadata::default() ..FileMetadata::default()