Skip to content

Commit c9a7db7

Browse files
committed
ci: add relative link checker
1 parent 4e5973d commit c9a7db7

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

scripts/check_links.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from pathlib import Path
2+
import re
3+
4+
ROOT = Path(__file__).resolve().parents[1]
5+
LINK_RE = re.compile(r'\[[^\]]+\]\(([^)]+)\)')
6+
SKIP_PREFIXES = ('http://', 'https://', 'mailto:', '#')
7+
8+
errors = []
9+
for md in ROOT.rglob('*.md'):
10+
text = md.read_text(encoding='utf-8')
11+
for match in LINK_RE.finditer(text):
12+
target = match.group(1).split('#', 1)[0].strip()
13+
if not target or target.startswith(SKIP_PREFIXES):
14+
continue
15+
if target.startswith('../') or target.startswith('./'):
16+
resolved = (md.parent / target).resolve()
17+
else:
18+
resolved = (md.parent / target).resolve()
19+
try:
20+
resolved.relative_to(ROOT.resolve())
21+
except ValueError:
22+
continue
23+
if not resolved.exists():
24+
errors.append(f'{md.relative_to(ROOT)} -> {target}')
25+
26+
if errors:
27+
print('Broken relative links:')
28+
for error in errors:
29+
print('-', error)
30+
raise SystemExit(1)
31+
32+
print('Relative links OK')

0 commit comments

Comments
 (0)