add ability for the built-in frontmatter parser to also output

This commit is contained in:
zyl 2024-11-08 11:07:31 -08:00
parent 624853f3ba
commit 8b7620b0bd
Signed by: zyl
SSH key fingerprint: SHA256:uxxbSXbdroP/OnKBGnEDk5q7EKB2razvstC/KmzdXXs

View file

@ -1,4 +1,4 @@
use serde::de::DeserializeOwned; use serde::{de::DeserializeOwned, Serialize};
/// Very basic YAML front matter parser. /// Very basic YAML front matter parser.
#[derive(Debug)] #[derive(Debug)]
@ -30,3 +30,27 @@ where
}) })
} }
} }
impl<T> FrontMatter<T>
where
T: Serialize,
{
/// Formats the front matter and content to a string ready for saving.
pub fn format(&self) -> eyre::Result<String> {
let mut output = String::new();
if let Some(data) = &self.data {
output.push_str("---\n");
output.push_str(&serde_yml::to_string(data)?);
output.push_str("---\n\n");
}
output.push_str(&self.content);
if !output.ends_with('\n') {
output.push('\n');
}
Ok(output)
}
}