hardbreak option for blogs, true for generic, false in other cases
This commit is contained in:
parent
e54cd44714
commit
58c1b8a21a
2 changed files with 21 additions and 13 deletions
|
@ -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,
|
||||
|
|
|
@ -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<bool>,
|
||||
pub title: Option<String>,
|
||||
pub date: Option<Date>,
|
||||
pub description: Option<String>,
|
||||
|
@ -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<FileMetadataBlog>,
|
||||
pub contact: Option<FileMetadataContact>,
|
||||
pub index: Option<FileMetadataIndex>,
|
||||
|
@ -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)),
|
||||
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()
|
||||
|
|
Loading…
Reference in a new issue