mirror of
https://github.com/zyllian/webdog.git
synced 2025-05-11 02:56:38 -07:00
custom front matter parser for showing errors + make all extras configurable from the page side
This commit is contained in:
parent
159c8fa5b3
commit
76c75a40d9
9 changed files with 119 additions and 97 deletions
32
src/frontmatter.rs
Normal file
32
src/frontmatter.rs
Normal 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,
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue