File size: 908 Bytes
a753e74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""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