was i stupid?
This commit is contained in:
parent
4ae660983f
commit
fe0d15509c
1 changed files with 31 additions and 53 deletions
|
@ -118,68 +118,46 @@ fn deserialize_metadata<T: Default + serde::de::DeserializeOwned>(text: &str) ->
|
|||
/// Fetch metadata from AST
|
||||
pub fn get<'a>(root: &'a AstNode<'a>, mtype: MType) -> MFile {
|
||||
root.children()
|
||||
.map(|node| {
|
||||
let generic = MFile {
|
||||
hardbreaks: true,
|
||||
..MFile::default()
|
||||
};
|
||||
|
||||
match &node.data.borrow().value {
|
||||
// Extract metadata from frontmatter
|
||||
NodeValue::FrontMatter(text) => match mtype {
|
||||
MType::Blog => {
|
||||
let metadata: FileMetadataBlog = deserialize_metadata(text);
|
||||
MFile {
|
||||
blog: Some(metadata.clone()),
|
||||
hardbreaks: metadata.hardbreaks.unwrap_or_default(),
|
||||
..MFile::default()
|
||||
}
|
||||
}
|
||||
MType::Contact => {
|
||||
let mut metadata: FileMetadataContact = deserialize_metadata(text);
|
||||
// Trim descriptions
|
||||
if let Some(desc) = &mut metadata.description {
|
||||
desc.clone_from(&desc.trim().into());
|
||||
}
|
||||
MFile {
|
||||
contact: Some(metadata),
|
||||
..MFile::default()
|
||||
}
|
||||
}
|
||||
MType::Generic => generic,
|
||||
MType::Index => MFile {
|
||||
index: Some(deserialize_metadata(text)),
|
||||
.map(|node| match &node.data.borrow().value {
|
||||
// Extract metadata from frontmatter
|
||||
NodeValue::FrontMatter(text) => match mtype {
|
||||
MType::Blog => {
|
||||
let metadata = deserialize_metadata::<FileMetadataBlog>(text);
|
||||
MFile {
|
||||
blog: Some(metadata.clone()),
|
||||
hardbreaks: metadata.hardbreaks.unwrap_or_default(),
|
||||
..MFile::default()
|
||||
},
|
||||
MType::Portfolio => MFile {
|
||||
portfolio: Some(deserialize_metadata(text)),
|
||||
}
|
||||
}
|
||||
MType::Contact => {
|
||||
let mut metadata = deserialize_metadata::<FileMetadataContact>(text);
|
||||
// Trim descriptions
|
||||
if let Some(desc) = &mut metadata.description {
|
||||
desc.clone_from(&desc.trim().into());
|
||||
}
|
||||
MFile {
|
||||
contact: Some(metadata),
|
||||
..MFile::default()
|
||||
},
|
||||
},
|
||||
_ => generic,
|
||||
}
|
||||
})
|
||||
.next()
|
||||
.map_or_else(
|
||||
|| match mtype {
|
||||
MType::Blog => MFile {
|
||||
blog: Some(FileMetadataBlog::default()),
|
||||
}
|
||||
}
|
||||
MType::Generic => MFile {
|
||||
hardbreaks: deserialize_metadata(text),
|
||||
..MFile::default()
|
||||
},
|
||||
MType::Contact => MFile {
|
||||
contact: Some(FileMetadataContact::default()),
|
||||
..MFile::default()
|
||||
},
|
||||
MType::Generic => MFile::default(),
|
||||
MType::Index => MFile {
|
||||
index: Some(FileMetadataIndex::default()),
|
||||
index: Some(deserialize_metadata(text)),
|
||||
..MFile::default()
|
||||
},
|
||||
MType::Portfolio => MFile {
|
||||
portfolio: Some(FileMetadataPortfolio::default()),
|
||||
portfolio: Some(deserialize_metadata(text)),
|
||||
..MFile::default()
|
||||
},
|
||||
},
|
||||
|data| data,
|
||||
)
|
||||
_ => MFile {
|
||||
hardbreaks: true,
|
||||
..MFile::default()
|
||||
},
|
||||
})
|
||||
.next()
|
||||
.unwrap()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue