-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcheck_links.py
More file actions
76 lines (60 loc) · 2.54 KB
/
check_links.py
File metadata and controls
76 lines (60 loc) · 2.54 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
#!/usr/bin/env python3
"""
Link validation script for GitHub Actions workflow.
Checks for broken links in learning room markdown files.
"""
import re
import sys
from pathlib import Path
def check_links(directory):
"""Check for broken links in markdown files."""
errors = []
warnings = []
md_files = Path(directory).glob('**/*.md')
for file in md_files:
content = file.read_text()
# Find all markdown links: [text](url)
links = re.findall(r'\[([^\]]+)\]\(([^)]+)\)', content)
for text, url in links:
# Skip external URLs (start with http)
if url.startswith('http'):
continue
# Check local files
target = (file.parent / url).resolve()
# Check for common typos
if url.startswith('htp'):
errors.append({
'file': str(file),
'message': f'Typo in URL: "{url}" should be "https://..."',
'help_url': 'https://github.com/markdownlint/markdownlint/rules/MD001'
})
# Check if file exists
elif not url.startswith('#') and not target.exists():
warnings.append({
'file': str(file),
'message': f'Link to "{url}" - file may not exist'
})
# Check link text (WCAG 2.4.4)
if text.lower() in ['click here', 'read more', 'learn more', 'here', 'link', 'more']:
warnings.append({
'file': str(file),
'message': f'Link text "{text}" is not descriptive. Use descriptive text that explains where the link goes.',
'help_url': 'https://www.w3.org/WAI/WCAG21/Understanding/link-purpose-in-context.html'
})
return {'errors': errors, 'warnings': warnings}
if __name__ == '__main__':
directory = sys.argv[1] if len(sys.argv) > 1 else '.'
results = check_links(directory)
import json
with open('validation-results.json', 'w') as f:
json.dump(results, f, indent=2)
# Print summary
print(f"\n🔗 Link Check Results:")
print(f" Errors: {len(results['errors'])}")
print(f" Warnings: {len(results['warnings'])}")
for err in results['errors']:
print(f"\n❌ {err['file']}")
print(f" {err['message']}")
for warn in results['warnings']:
print(f"\n⚠️ {warn['file']}")
print(f" {warn['message']}")