add generic metada type for md files + comments

This commit is contained in:
Mylloon 2024-01-24 12:06:59 +01:00
parent 1467740f60
commit cb67c85040
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -6,6 +6,7 @@ use ramhorns::Content;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use std::fs; use std::fs;
/// Metadata for blog posts
#[derive(Default, Deserialize, Content, Debug)] #[derive(Default, Deserialize, Content, Debug)]
pub struct FileMetadataBlog { pub struct FileMetadataBlog {
pub title: Option<String>, pub title: Option<String>,
@ -16,37 +17,7 @@ pub struct FileMetadataBlog {
pub toc: Option<bool>, pub toc: Option<bool>,
} }
#[derive(Default, Deserialize, Content, Debug)] /// A tag, related to post blog
pub struct FileMetadataContact {
pub title: String,
pub custom: Option<bool>,
pub user: Option<String>,
pub link: Option<String>,
pub newtab: Option<bool>,
pub description: Option<String>,
}
#[derive(Default, Deserialize, Content, Debug)]
pub struct FileMetadataPortfolio {
pub title: Option<String>,
pub link: Option<String>,
pub description: Option<String>,
pub language: Option<String>,
}
pub enum TypeFileMetadata {
Blog,
Contact,
Portfolio,
}
#[derive(Default, Deserialize, Content, Debug)]
pub struct FileMetadata {
pub blog: Option<FileMetadataBlog>,
pub contact: Option<FileMetadataContact>,
pub portfolio: Option<FileMetadataPortfolio>,
}
#[derive(Content, Debug, Clone)] #[derive(Content, Debug, Clone)]
pub struct Tag { pub struct Tag {
pub name: String, 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<bool>,
pub user: Option<String>,
pub link: Option<String>,
pub newtab: Option<bool>,
pub description: Option<String>,
}
/// Metadata for portfolio cards
#[derive(Default, Deserialize, Content, Debug)]
pub struct FileMetadataPortfolio {
pub title: Option<String>,
pub link: Option<String>,
pub description: Option<String>,
pub language: Option<String>,
}
/// 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<FileMetadataBlog>,
pub contact: Option<FileMetadataContact>,
pub portfolio: Option<FileMetadataPortfolio>,
}
/// Global metadata
#[derive(Content, Debug)] #[derive(Content, Debug)]
pub struct Metadata { pub struct Metadata {
pub info: FileMetadata, pub info: FileMetadata,
@ -75,6 +84,7 @@ pub struct Metadata {
pub syntax_highlight: bool, pub syntax_highlight: bool,
} }
/// File description
#[derive(Content, Debug)] #[derive(Content, Debug)]
pub struct File { pub struct File {
pub metadata: Metadata, pub metadata: Metadata,
@ -214,7 +224,11 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileM
match root match root
.children() .children()
.find_map(|node| match &node.data.borrow().value { .find_map(|node| match &node.data.borrow().value {
// Extract metadata from frontmatter
NodeValue::FrontMatter(text) => Some(match mtype { NodeValue::FrontMatter(text) => Some(match mtype {
TypeFileMetadata::Generic => FileMetadata {
..FileMetadata::default()
},
TypeFileMetadata::Blog => FileMetadata { TypeFileMetadata::Blog => FileMetadata {
blog: Some(deserialize_metadata(text)), blog: Some(deserialize_metadata(text)),
..FileMetadata::default() ..FileMetadata::default()
@ -240,7 +254,11 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileM
_ => None, _ => None,
}) { }) {
Some(data) => data, Some(data) => data,
// No metadata
None => match mtype { None => match mtype {
TypeFileMetadata::Generic => FileMetadata {
..FileMetadata::default()
},
TypeFileMetadata::Blog => FileMetadata { TypeFileMetadata::Blog => FileMetadata {
blog: Some(FileMetadataBlog::default()), blog: Some(FileMetadataBlog::default()),
..FileMetadata::default() ..FileMetadata::default()