-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathcheck_docs_links.py
More file actions
82 lines (57 loc) · 2.18 KB
/
check_docs_links.py
File metadata and controls
82 lines (57 loc) · 2.18 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""Validate local markdown links under docs/."""
from __future__ import annotations
import re
from pathlib import Path
LINK_PATTERN = re.compile(r"\[[^\]]+\]\(([^)]+)\)")
def is_external(target: str) -> bool:
return target.startswith(
("http://", "https://", "mailto:", "#", "<", "javascript:"))
def resolve_candidates(file_path: Path, docs_root: Path, target: str) -> list[Path]:
clean_target = target.split("#", 1)[0].split("?", 1)[0].strip()
if not clean_target:
return []
if clean_target.endswith("/"):
clean_target = f"{clean_target}index.md"
candidates: list[Path] = []
if clean_target.startswith("/"):
candidates.append(docs_root / clean_target.lstrip("/"))
return candidates
base = file_path.parent
path_target = Path(clean_target)
candidates.append(base / path_target)
if not path_target.suffix:
candidates.append(base / f"{clean_target}.md")
return candidates
def check_links(docs_root: Path) -> list[str]:
errors: list[str] = []
for md_file in sorted(docs_root.rglob("*.md")):
content = md_file.read_text(encoding="utf-8")
for line_no, line in enumerate(content.splitlines(), start=1):
for target in LINK_PATTERN.findall(line):
stripped = target.strip()
if is_external(stripped):
continue
candidates = resolve_candidates(md_file, docs_root, stripped)
if not candidates:
continue
if any(path.is_file() for path in candidates):
continue
errors.append(
f"{md_file.relative_to(docs_root.parent)}:{line_no} -> {stripped}"
)
return errors
def main() -> int:
repo_root = Path(__file__).resolve().parents[1]
docs_root = repo_root / "docs"
errors = check_links(docs_root)
if not errors:
print("Docs link check passed.")
return 0
print("Docs link check failed:")
for err in errors:
print(f" - {err}")
return 1
if __name__ == "__main__":
raise SystemExit(main())