md_formatter/
parser.rs

1use pulldown_cmark::{Event, Parser};
2
3/// Extract YAML frontmatter from markdown input if present
4/// Returns (frontmatter, remaining_input)
5pub fn extract_frontmatter(input: &str) -> (Option<String>, &str) {
6    if !input.starts_with("---\n") {
7        return (None, input);
8    }
9
10    // Find the closing ---
11    let after_opening = &input[4..]; // Skip first "---\n"
12    if let Some(end_pos) = after_opening.find("\n---\n") {
13        let frontmatter = after_opening[..end_pos].to_string();
14        let remaining = &after_opening[end_pos + 5..]; // Skip "\n---\n"
15                                                       // Include the frontmatter with opening and closing markers, plus blank line
16        (Some(format!("---\n{}\n---\n\n", frontmatter)), remaining)
17    } else {
18        (None, input)
19    }
20}
21
22/// Parse markdown into events (GFM tables not needed for basic support)
23pub fn parse_markdown(input: &str) -> Vec<Event<'_>> {
24    Parser::new(input).collect()
25}