-
Notifications
You must be signed in to change notification settings - Fork 11
63 lines (56 loc) · 1.85 KB
/
Copy pathdocs.yml
File metadata and controls
63 lines (56 loc) · 1.85 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
name: Docs
on:
pull_request:
push:
branches:
- main
- gh-pages
jobs:
links:
name: Check local documentation links
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Check local links
shell: bash
run: |
python3 - <<'PY'
from pathlib import Path
import re
import sys
from urllib.parse import unquote, urlparse
root = Path(".").resolve()
docs = [
*root.glob("*.md"),
*root.glob("docs/**/*.md"),
]
pattern = re.compile(r"\[[^\]]+\]\(([^)]+)\)")
ignored_prefixes = ("http://", "https://", "mailto:", "#")
missing = []
for doc in docs:
text = doc.read_text(encoding="utf-8")
for raw in pattern.findall(text):
target = raw.strip()
if not target or target.startswith(ignored_prefixes):
continue
if "://" in target:
continue
parsed = urlparse(target)
path = unquote(parsed.path)
if not path or path.startswith("#"):
continue
candidate = (doc.parent / path).resolve()
try:
candidate.relative_to(root)
except ValueError:
missing.append((doc, target, "outside repository"))
continue
if not candidate.exists():
missing.append((doc, target, "missing"))
if missing:
for doc, target, reason in missing:
print(f"{doc.relative_to(root)} -> {target}: {reason}")
sys.exit(1)
print(f"Checked {len(docs)} Markdown files.")
PY