better metadata
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-19 21:16:39 +02:00
parent 2bc0f14475
commit a293ef4332
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 46 additions and 20 deletions

View file

@ -5,7 +5,7 @@ use ramhorns::Content;
use crate::{ use crate::{
config::Config, config::Config,
template::{read_md_file, File, Infos}, template::{get_md_asm, get_md_metadata, read_md_file, File, Infos},
}; };
#[get("/blog")] #[get("/blog")]
@ -26,7 +26,8 @@ struct Post {
#[once(time = 60)] #[once(time = 60)]
pub fn get_index(config: Config) -> String { pub fn get_index(config: Config) -> String {
let paths = glob("data/blog/*.md") let location = "data/blog";
let paths = glob(&format!("{location}/*.md"))
.unwrap() .unwrap()
.map(|f| { .map(|f| {
let filename = f let filename = f
@ -36,9 +37,19 @@ pub fn get_index(config: Config) -> String {
.to_string_lossy() .to_string_lossy()
.to_string(); .to_string();
let file_without_ext = filename.split_at(filename.len() - 3).0; let file_without_ext = filename.split_at(filename.len() - 3).0;
let file_metadata = match std::fs::read_to_string(format!("{location}/{filename}")) {
Ok(text) => {
let md_tree = get_md_asm(&text);
let md_nodes = md_tree.children().unwrap();
get_md_metadata(md_nodes).title
}
_ => None,
};
Post { Post {
title: file_without_ext.to_string(),
url: file_without_ext.to_string(), url: file_without_ext.to_string(),
title: file_metadata.unwrap_or(file_without_ext.to_string()),
} }
}) })
.collect::<Vec<Post>>(); .collect::<Vec<Post>>();

View file

@ -87,8 +87,21 @@ pub fn read_md_file(filename: &str) -> Option<File> {
} }
} }
pub fn read_md(raw_text: &str) -> File { pub fn get_md_metadata(vec: &[Node]) -> FileMetadata {
let parse_option = markdown::ParseOptions { if vec.is_empty() {
FileMetadata::default()
} else {
match &vec[0] {
Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(),
Node::Toml(v) => FrontMatter::Toml(&v.value).parse(),
Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(),
_ => FileMetadata::default(),
}
}
}
fn get_parse_option() -> markdown::ParseOptions {
markdown::ParseOptions {
constructs: markdown::Constructs { constructs: markdown::Constructs {
frontmatter: true, frontmatter: true,
math_text: true, math_text: true,
@ -96,32 +109,34 @@ pub fn read_md(raw_text: &str) -> File {
..markdown::Constructs::gfm() ..markdown::Constructs::gfm()
}, },
..markdown::ParseOptions::gfm() ..markdown::ParseOptions::gfm()
}; }
}
let compile_option = markdown::CompileOptions { fn get_compile_option() -> markdown::CompileOptions {
markdown::CompileOptions {
allow_dangerous_html: true, allow_dangerous_html: true,
..markdown::CompileOptions::gfm() ..markdown::CompileOptions::gfm()
}; }
}
let md_tree = markdown::to_mdast(raw_text, &parse_option).unwrap(); pub fn get_md_asm(raw_text: &str) -> Node {
let parse_option = get_parse_option();
markdown::to_mdast(raw_text, &parse_option).unwrap()
}
pub fn read_md(raw_text: &str) -> File {
let md_tree = get_md_asm(raw_text);
let md_nodes = md_tree.children().unwrap(); let md_nodes = md_tree.children().unwrap();
let metadata; let metadata = get_md_metadata(md_nodes);
let presence_mermaid; let presence_mermaid;
let presence_math; let presence_math;
let presence_code; let presence_code;
if md_nodes.is_empty() { if md_nodes.is_empty() {
metadata = FileMetadata::default();
presence_mermaid = false; presence_mermaid = false;
presence_math = false; presence_math = false;
presence_code = false; presence_code = false;
} else { } else {
metadata = match &md_nodes[0] {
Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(),
Node::Toml(v) => FrontMatter::Toml(&v.value).parse(),
Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(),
_ => FileMetadata::default(),
};
// Find if document contains maths // Find if document contains maths
presence_math = check_math(md_nodes); presence_math = check_math(md_nodes);
@ -137,8 +152,8 @@ pub fn read_md(raw_text: &str) -> File {
let html = markdown::to_html_with_options( let html = markdown::to_html_with_options(
raw_text, raw_text,
&markdown::Options { &markdown::Options {
parse: parse_option, parse: get_parse_option(),
compile: compile_option, compile: get_compile_option(),
}, },
) )
.unwrap(); .unwrap();