MHamdan's picture
Deploy Research-Link-AI (Docker Space, offline demo)
a753e74 verified
Raw
History Blame Contribute Delete
908 Bytes
"""Internal link validation for generated Markdown files."""
from __future__ import annotations
import re
from pathlib import Path
LINK_RE = re.compile(r"\[([^\]]+)\]\(([^)]+)\)")
def find_internal_links(content: str) -> list[tuple[str, str]]:
"""Extract (text, href) pairs for internal (non-http) links."""
all_links = LINK_RE.findall(content)
return [(text, href) for text, href in all_links if not href.startswith("http")]
def validate_internal_links(file_path: Path, base_dir: Path) -> list[str]:
"""Return list of broken internal link hrefs in a Markdown file."""
broken = []
try:
content = file_path.read_text(encoding="utf-8")
for _, href in find_internal_links(content):
target = (file_path.parent / href).resolve()
if not target.exists():
broken.append(href)
except Exception:
pass
return broken