From cb67c85040001bc6f955495483a64a7da3dfc718 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 24 Jan 2024 12:06:59 +0100 Subject: [PATCH] add generic metada type for md files + comments --- src/misc/markdown.rs | 80 +++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 31 deletions(-) diff --git a/src/misc/markdown.rs b/src/misc/markdown.rs index eb4a6b3..27c16f4 100644 --- a/src/misc/markdown.rs +++ b/src/misc/markdown.rs @@ -6,6 +6,7 @@ use ramhorns::Content; use serde::{Deserialize, Deserializer}; use std::fs; +/// Metadata for blog posts #[derive(Default, Deserialize, Content, Debug)] pub struct FileMetadataBlog { pub title: Option, @@ -16,37 +17,7 @@ pub struct FileMetadataBlog { pub toc: Option, } -#[derive(Default, Deserialize, Content, Debug)] -pub struct FileMetadataContact { - pub title: String, - pub custom: Option, - pub user: Option, - pub link: Option, - pub newtab: Option, - pub description: Option, -} - -#[derive(Default, Deserialize, Content, Debug)] -pub struct FileMetadataPortfolio { - pub title: Option, - pub link: Option, - pub description: Option, - pub language: Option, -} - -pub enum TypeFileMetadata { - Blog, - Contact, - Portfolio, -} - -#[derive(Default, Deserialize, Content, Debug)] -pub struct FileMetadata { - pub blog: Option, - pub contact: Option, - pub portfolio: Option, -} - +/// A tag, related to post blog #[derive(Content, Debug, Clone)] pub struct Tag { pub name: String, @@ -67,6 +38,44 @@ impl<'de> Deserialize<'de> for Tag { } } +/// Metadata for contact entry +#[derive(Default, Deserialize, Content, Debug)] +pub struct FileMetadataContact { + pub title: String, + pub custom: Option, + pub user: Option, + pub link: Option, + pub newtab: Option, + pub description: Option, +} + +/// Metadata for portfolio cards +#[derive(Default, Deserialize, Content, Debug)] +pub struct FileMetadataPortfolio { + pub title: Option, + pub link: Option, + pub description: Option, + pub language: Option, +} + +/// List of available metadata types +pub enum TypeFileMetadata { + Generic, + Blog, + Contact, + Portfolio, +} + +/// Structure who holds all the metadata the file have +/// Usually all fields are None except one +#[derive(Default, Deserialize, Content, Debug)] +pub struct FileMetadata { + pub blog: Option, + pub contact: Option, + pub portfolio: Option, +} + +/// Global metadata #[derive(Content, Debug)] pub struct Metadata { pub info: FileMetadata, @@ -75,6 +84,7 @@ pub struct Metadata { pub syntax_highlight: bool, } +/// File description #[derive(Content, Debug)] pub struct File { pub metadata: Metadata, @@ -214,7 +224,11 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileM match root .children() .find_map(|node| match &node.data.borrow().value { + // Extract metadata from frontmatter NodeValue::FrontMatter(text) => Some(match mtype { + TypeFileMetadata::Generic => FileMetadata { + ..FileMetadata::default() + }, TypeFileMetadata::Blog => FileMetadata { blog: Some(deserialize_metadata(text)), ..FileMetadata::default() @@ -240,7 +254,11 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileM _ => None, }) { Some(data) => data, + // No metadata None => match mtype { + TypeFileMetadata::Generic => FileMetadata { + ..FileMetadata::default() + }, TypeFileMetadata::Blog => FileMetadata { blog: Some(FileMetadataBlog::default()), ..FileMetadata::default()