Minimal frontmatter splitter for a full markdown document: `---\n \n---\n `. Enough for templates and the CLI; values may be quoted strings or `[a, b]` arrays. Not a full YAML parser.
(doc: &str)
| 365 | /// `---\n<key: value lines>\n---\n<body>`. Enough for templates and the CLI; |
| 366 | /// values may be quoted strings or `[a, b]` arrays. Not a full YAML parser. |
| 367 | pub fn parse_markdown(doc: &str) -> Result<(Map<String, Value>, String)> { |
| 368 | // Normalize CRLF so a Windows-authored file parses identically to LF — |
| 369 | // otherwise the "---\n" prefix match fails and the whole doc is silently |
| 370 | // treated as body with empty frontmatter. |
| 371 | let normalized = doc |
| 372 | .strip_prefix('\u{feff}') |
| 373 | .unwrap_or(doc) |
| 374 | .replace("\r\n", "\n"); |
| 375 | let doc = normalized.as_str(); |
| 376 | let rest = match doc.strip_prefix("---\n") { |
| 377 | Some(r) => r, |
| 378 | None => return Ok((Map::new(), doc.to_string())), |
| 379 | }; |
| 380 | let end = rest |
| 381 | .find("\n---") |
| 382 | .ok_or_else(|| CanonicalError::Lift("unterminated frontmatter block".into()))?; |
| 383 | let fm_src = &rest[..end]; |
| 384 | // body starts after the closing --- line |
| 385 | let after = &rest[end + 1..]; // at "---..." |
| 386 | let body = after |
| 387 | .strip_prefix("---") |
| 388 | .map(|b| b.trim_start_matches(['\n', '\r']).to_string()) |
| 389 | .unwrap_or_default(); |
| 390 | |
| 391 | let mut fm = Map::new(); |
| 392 | for line in fm_src.lines() { |
| 393 | let line = line.trim(); |
| 394 | if line.is_empty() { |
| 395 | continue; |
| 396 | } |
| 397 | if let Some(colon) = line.find(':') { |
| 398 | let key = line[..colon].trim().to_string(); |
| 399 | let raw = line[colon + 1..].trim(); |
| 400 | fm.insert(key, parse_scalar(raw)); |
| 401 | } |
| 402 | } |
| 403 | Ok((fm, body)) |
| 404 | } |
| 405 | |
| 406 | fn parse_scalar(raw: &str) -> Value { |
| 407 | if raw.starts_with('[') && raw.ends_with(']') { |