|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Check external URLs in English docs for dead links.""" |
| 3 | + |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
| 9 | + |
| 10 | +DOCS_DIR = Path(__file__).parent.parent / "docs" |
| 11 | +DOC_EXTENSIONS = (".md", ".mdx") |
| 12 | + |
| 13 | +# URLs to skip (templates with placeholders, localhost, etc.) |
| 14 | +SKIP_PATTERNS = [ |
| 15 | + "<", # URLs with placeholder variables like <DOMAIN_NAME> |
| 16 | + "localhost", |
| 17 | + "127.0.0.1", |
| 18 | + "1.1.1.1", |
| 19 | + "example.com", |
| 20 | +] |
| 21 | + |
| 22 | + |
| 23 | +def extract_urls(text: str) -> list[str]: |
| 24 | + """Extract all http/https URLs from markdown text.""" |
| 25 | + urls = set() |
| 26 | + # Markdown links [text](url) |
| 27 | + for m in re.finditer(r'\[(?:[^\]\\]|\\.)*\]\((https?://[^)]+)\)', text): |
| 28 | + urls.add(m.group(1)) |
| 29 | + # Auto-links <url> |
| 30 | + for m in re.finditer(r'<(https?://[^>]+)>', text): |
| 31 | + urls.add(m.group(1)) |
| 32 | + return sorted(urls) |
| 33 | + |
| 34 | + |
| 35 | +def should_skip(url: str) -> bool: |
| 36 | + for pattern in SKIP_PATTERNS: |
| 37 | + if pattern in url: |
| 38 | + return True |
| 39 | + return False |
| 40 | + |
| 41 | + |
| 42 | +def find_urls_in_docs() -> dict[str, list[str]]: |
| 43 | + """Return {url: [files]} mapping for all external URLs in docs.""" |
| 44 | + url_files: dict[str, list[str]] = {} |
| 45 | + for ext in DOC_EXTENSIONS: |
| 46 | + for f in DOCS_DIR.rglob(f"*{ext}"): |
| 47 | + text = f.read_text(encoding="utf-8") |
| 48 | + rel = str(f.relative_to(DOCS_DIR.parent)) |
| 49 | + for url in extract_urls(text): |
| 50 | + if not should_skip(url): |
| 51 | + url_files.setdefault(url, []).append(rel) |
| 52 | + return url_files |
| 53 | + |
| 54 | + |
| 55 | +def check_url(url: str) -> tuple[str, int | str]: |
| 56 | + """Check a URL with curl and return (url, status_code_or_error).""" |
| 57 | + try: |
| 58 | + result = subprocess.run( |
| 59 | + [ |
| 60 | + "curl", "-sS", "-o", "/dev/null", "-w", "%{http_code}", |
| 61 | + "-L", # follow redirects |
| 62 | + "--max-time", "15", |
| 63 | + "-A", "Mozilla/5.0 (link checker)", |
| 64 | + url, |
| 65 | + ], |
| 66 | + capture_output=True, |
| 67 | + text=True, |
| 68 | + timeout=20, |
| 69 | + ) |
| 70 | + code = int(result.stdout.strip()) |
| 71 | + return url, code |
| 72 | + except Exception as e: |
| 73 | + return url, str(e) |
| 74 | + |
| 75 | + |
| 76 | +def main(): |
| 77 | + url_files = find_urls_in_docs() |
| 78 | + urls = sorted(url_files.keys()) |
| 79 | + print(f"Checking {len(urls)} unique external URLs...\n") |
| 80 | + |
| 81 | + dead = [] |
| 82 | + ok = 0 |
| 83 | + with ThreadPoolExecutor(max_workers=8) as pool: |
| 84 | + futures = {pool.submit(check_url, url): url for url in urls} |
| 85 | + for future in as_completed(futures): |
| 86 | + url, result = future.result() |
| 87 | + if isinstance(result, int) and 200 <= result < 400: |
| 88 | + ok += 1 |
| 89 | + else: |
| 90 | + files = url_files[url] |
| 91 | + dead.append((url, result, files)) |
| 92 | + print(f" DEAD [{result}] {url}") |
| 93 | + for f in files: |
| 94 | + print(f" in {f}") |
| 95 | + |
| 96 | + print(f"\n{ok} OK, {len(dead)} dead") |
| 97 | + if dead: |
| 98 | + sys.exit(1) |
| 99 | + else: |
| 100 | + print("PASSED: All external links are alive") |
| 101 | + sys.exit(0) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + main() |
0 commit comments