From 0541887c281bb877a7b901399ee05d4f70948346 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 21 Apr 2023 16:48:31 +0200 Subject: [PATCH] use metadata from frontmatter --- src/misc/markdown.rs | 10 +++++----- src/routes/blog.rs | 22 ++++++++++------------ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/misc/markdown.rs b/src/misc/markdown.rs index f220963..f88b5f5 100644 --- a/src/misc/markdown.rs +++ b/src/misc/markdown.rs @@ -30,7 +30,7 @@ pub struct File { } /// Options used for parser and compiler MD --> HTML -fn get_options() -> ComrakOptions { +pub fn get_options() -> ComrakOptions { ComrakOptions { extension: ComrakExtensionOptions { strikethrough: true, @@ -63,13 +63,13 @@ fn get_options() -> ComrakOptions { } pub fn read(raw_text: &str) -> File { - let options = get_options(); - let arena = Arena::new(); + + let options = get_options(); let root = parse_document(&arena, raw_text, &options); // Find metadata - let _metadata = get_metadata(root); + let metadata = get_metadata(root); // Convert to HTML let mut html = vec![]; @@ -77,7 +77,7 @@ pub fn read(raw_text: &str) -> File { File { metadata: Metadata { - info: FileMetadata::default(), + info: metadata, math: false, mermaid: false, syntax_highlight: false, diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 87ae146..0e11613 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -1,13 +1,14 @@ use actix_web::{get, web, HttpResponse, Responder}; use cached::proc_macro::once; use chrono::{DateTime, Datelike, Utc}; +use comrak::{parse_document, Arena}; use ramhorns::Content; use crate::{ config::Config, misc::{ date::Date, - markdown::{read_file, File, FileMetadata}, + markdown::{get_metadata, get_options, read_file, File, FileMetadata}, }, template::Infos, }; @@ -65,22 +66,19 @@ fn get_posts(location: &str) -> Vec { 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(); - let mut metadata = get_md_metadata(md_nodes); + Ok(text) => { + let arena = Arena::new(); + + let options = get_options(); + let root = parse_document(&arena, &text, &options); + let mut metadata = get_metadata(root); metadata.title = match metadata.title { - Some(t) => Some(t), + Some(title) => Some(title), None => Some(file_without_ext.to_string()), }; - metadata */ - FileMetadata { - title: Some(file_without_ext.to_string()), - link: None, - date: None, - } + metadata } Err(_) => FileMetadata { title: Some(file_without_ext.to_string()),