use std::borrow::Cow;
use extract_frontmatter::{config::{Modifier, Splitter},
Extractor};
pub const FRONTMATTER_DELIMITER_PATTERN: &str = "---";
pub fn create_frontmatter_extractor<'caller>() -> Extractor<'caller> {
let splitter = Splitter::EnclosingLines(FRONTMATTER_DELIMITER_PATTERN);
let modifier = Modifier::TrimWhitespace;
let mut extractor = Extractor::new(splitter);
extractor.with_modifier(modifier);
extractor
}
pub fn try_extract_front_matter(markdown_input: &str) -> FrontmatterExtractionResponse {
let extractor = create_frontmatter_extractor();
let (frontmatter, content) = extractor.extract(markdown_input);
if content.is_empty() {
FrontmatterExtractionResponse::NoFrontmatter
} else {
FrontmatterExtractionResponse::ValidFrontmatter(frontmatter, Cow::Borrowed(content))
}
}
#[derive(Debug, Clone)]
pub enum FrontmatterExtractionResponse<'caller> {
NoFrontmatter,
ValidFrontmatter(
Cow<'caller, str>,
Cow<'caller, str>,
),
}
impl<'caller> From<&'caller str> for FrontmatterExtractionResponse<'caller> {
fn from(markdown_input: &'caller str) -> Self { try_extract_front_matter(markdown_input) }
}