pub fn parse_frontmatter(content: &str) -> (Option<Vec<(String, String)>>, &str, usize) {
let trimmed = content.trim_start();
if !trimmed.starts_with("---") {
return (None, content, 0);
}
let after_opening = &trimmed[3..];
if let Some(end_pos) = after_opening.find("\n---") {
let frontmatter_text = &after_opening[..end_pos];
let remaining = &after_opening[end_pos + 4..];
let frontmatter_lines = frontmatter_text.lines().count();
let total_lines = 1 + frontmatter_lines + 1;
let mut fields = Vec::new();
for line in frontmatter_text.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
if let Some(colon_pos) = line.find(':') {
let key = line[..colon_pos].trim().to_string();
let value = line[colon_pos + 1..].trim().to_string();
let value = if (value.starts_with('"') && value.ends_with('"'))
|| (value.starts_with('\'') && value.ends_with('\''))
{
value[1..value.len() - 1].to_string()
} else {
value
};
fields.push((key, value));
}
}
if !fields.is_empty() {
return (Some(fields), remaining, total_lines);
}
}
(None, content, 0)
}