23 lines
860 B
Python
23 lines
860 B
Python
"""Check repository-relative links in Markdown without network access."""
|
|
|
|
import re
|
|
from pathlib import Path
|
|
from urllib.parse import unquote, urlsplit
|
|
|
|
root = Path(__file__).resolve().parents[1]
|
|
errors = []
|
|
checked = 0
|
|
for source in root.rglob("*.md"):
|
|
if ".git" in source.relative_to(root).parts:
|
|
continue
|
|
for target in re.findall(r"!?\[[^\]]*\]\(([^\s)]+)\)", source.read_text()):
|
|
parsed = urlsplit(target)
|
|
if parsed.scheme or parsed.netloc or not parsed.path:
|
|
continue
|
|
path = (source.parent / unquote(parsed.path)).resolve()
|
|
checked += 1
|
|
if not path.is_relative_to(root) or not path.exists():
|
|
errors.append(f"{source.relative_to(root)}: {target}")
|
|
if errors:
|
|
raise SystemExit("Broken local links:\n" + "\n".join(errors))
|
|
print(f"Local Markdown links: {checked} checked")
|