fix generic

This commit is contained in:
Mylloon 2024-11-09 15:43:29 +01:00
parent 8b5c128bfd
commit 8e1b036386
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -335,46 +335,48 @@ fn deserialize_metadata<T: Default + serde::de::DeserializeOwned>(text: &str) ->
/// Fetch metadata from AST /// Fetch metadata from AST
pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: &TypeFileMetadata) -> FileMetadata { pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: &TypeFileMetadata) -> FileMetadata {
root.children() root.children()
.find_map(|node| match &node.data.borrow().value { .map(|node| {
// Extract metadata from frontmatter let generic = FileMetadata {
NodeValue::FrontMatter(text) => Some(match mtype { hardbreaks: true,
TypeFileMetadata::Blog => { ..FileMetadata::default()
let metadata: FileMetadataBlog = deserialize_metadata(text); };
FileMetadata { match &node.data.borrow().value {
blog: Some(metadata.clone()), // Extract metadata from frontmatter
hardbreaks: metadata.hardbreaks.unwrap_or_default(), NodeValue::FrontMatter(text) => match mtype {
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);
// Trim descriptions
if let Some(desc) = &mut metadata.description {
desc.clone_from(&desc.trim().into());
}
FileMetadata {
contact: Some(metadata),
..FileMetadata::default()
}
}
TypeFileMetadata::Generic => generic,
TypeFileMetadata::Index => FileMetadata {
index: Some(deserialize_metadata(text)),
..FileMetadata::default() ..FileMetadata::default()
} },
} TypeFileMetadata::Portfolio => FileMetadata {
TypeFileMetadata::Contact => { portfolio: Some(deserialize_metadata(text)),
let mut metadata: FileMetadataContact = deserialize_metadata(text);
// Trim descriptions
if let Some(desc) = &mut metadata.description {
desc.clone_from(&desc.trim().into());
}
FileMetadata {
contact: Some(metadata),
..FileMetadata::default() ..FileMetadata::default()
} },
}
TypeFileMetadata::Generic => FileMetadata {
hardbreaks: true,
..FileMetadata::default()
}, },
TypeFileMetadata::Index => FileMetadata { _ => generic,
index: Some(deserialize_metadata(text)), }
..FileMetadata::default()
},
TypeFileMetadata::Portfolio => FileMetadata {
portfolio: Some(deserialize_metadata(text)),
..FileMetadata::default()
},
}),
_ => None,
}) })
.next()
.map_or_else( .map_or_else(
|| match mtype { || match mtype {
TypeFileMetadata::Blog => FileMetadata { TypeFileMetadata::Blog => FileMetadata {