rag_project_backend / chunking /markdown_loader.py
anhkhoiphan's picture
deploy: 2026-06-05 11:40:44
4d85858
Raw
History Blame Contribute Delete
607 Bytes
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"))