MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / parse_markdown

Function parse_markdown

atomic-canonical/src/lift.rs:432–469  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

430/// `---\n<key: value lines>\n---\n<body>`. Enough for templates and the CLI;
431/// values may be quoted strings or `[a, b]` arrays. Not a full YAML parser.
432pub fn parse_markdown(doc: &str) -> Result<(Map<String, Value>, String)> {
433 // Normalize CRLF so a Windows-authored file parses identically to LF —
434 // otherwise the "---\n" prefix match fails and the whole doc is silently
435 // treated as body with empty frontmatter.
436 let normalized = doc
437 .strip_prefix('\u{feff}')
438 .unwrap_or(doc)
439 .replace("\r\n", "\n");
440 let doc = normalized.as_str();
441 let rest = match doc.strip_prefix("---\n") {
442 Some(r) => r,
443 None => return Ok((Map::new(), doc.to_string())),
444 };
445 let end = rest
446 .find("\n---")
447 .ok_or_else(|| CanonicalError::Lift("unterminated frontmatter block".into()))?;
448 let fm_src = &rest[..end];
449 // body starts after the closing --- line
450 let after = &rest[end + 1..]; // at "---..."
451 let body = after
452 .strip_prefix("---")
453 .map(|b| b.trim_start_matches(['\n', '\r']).to_string())
454 .unwrap_or_default();
455
456 let mut fm = Map::new();
457 for line in fm_src.lines() {
458 let line = line.trim();
459 if line.is_empty() {
460 continue;
461 }
462 if let Some(colon) = line.find(':') {
463 let key = line[..colon].trim().to_string();
464 let raw = line[colon + 1..].trim();
465 fm.insert(key, parse_scalar(raw));
466 }
467 }
468 Ok((fm, body))
469}
470
471fn parse_scalar(raw: &str) -> Value {
472 if raw.starts_with('[') && raw.ends_with(']') {

Callers 3

runMethod · 0.85
runMethod · 0.85
parse_markdown_then_liftFunction · 0.85

Calls 6

parse_scalarFunction · 0.85
replaceMethod · 0.45
as_strMethod · 0.45
linesMethod · 0.45
is_emptyMethod · 0.45
insertMethod · 0.45

Tested by 1

parse_markdown_then_liftFunction · 0.68