custom front matter parser for showing errors + make all extras configurable from the page side

This commit is contained in:
zyl 2024-11-04 16:20:03 -08:00
parent 159c8fa5b3
commit 76c75a40d9
Signed by: zyl
SSH key fingerprint: SHA256:uxxbSXbdroP/OnKBGnEDk5q7EKB2razvstC/KmzdXXs
9 changed files with 119 additions and 97 deletions

32
src/frontmatter.rs Normal file
View file

@ -0,0 +1,32 @@
use serde::de::DeserializeOwned;
/// Very basic YAML front matter parser.
#[derive(Debug)]
pub struct FrontMatter<T> {
/// The content past the front matter.
pub content: String,
/// The front matter found, if any.
pub data: Option<T>,
}
impl<T> FrontMatter<T>
where
T: DeserializeOwned,
{
/// Parses the given input for front matter.
pub fn parse(input: String) -> eyre::Result<Self> {
if input.starts_with("---\n") {
if let Some((frontmatter, content)) = input[3..].split_once("---\n") {
let data = serde_yml::from_str(frontmatter)?;
return Ok(Self {
content: content.to_string(),
data,
});
}
}
Ok(Self {
content: input,
data: None,
})
}
}