from typeguard import typechecked from typing import Tuple import re @typechecked def parse_frontmatter(raw: str) -> Tuple[dict, str]: if not raw.startswith("---"): return {}, raw end = raw.find("---", 3) if end == -1: return {}, raw fm_block = raw[3:end].strip() body = raw[end + 3:].strip() meta: dict = {} for line in fm_block.splitlines(): m = re.match(r"^(\w[\w_-]*)\s*:\s*(.+)$", line) if m: meta[m.group(1).strip()] = m.group(2).strip().strip('"').strip("'") return meta, body