from pathlib import Path import re def normalize_markdown(text: str) -> str: text = text.replace("\r\n", "\n").replace("\r", "\n") lines = [line.rstrip() for line in text.split("\n")] text = "\n".join(lines).strip() return re.sub(r"\n{3,}", "\n\n", text) def load_markdown(path: str | Path) -> str: path = Path(path) if not path.exists(): raise FileNotFoundError(f"Markdown file not found: {path}") if path.suffix.lower() != ".md": raise ValueError(f"Input must be a Markdown .md file: {path}") return normalize_markdown(path.read_text(encoding="utf-8"))