Skip to content

Commit 7711e93

Browse files
Add internal links ci check
1 parent cc68796 commit 7711e93

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
import re
3+
import sys
4+
5+
CARDS_DIR = 'cards'
6+
7+
# Build a set of all valid card names (without .md extension)
8+
valid_cards = set()
9+
for file in os.listdir(CARDS_DIR):
10+
if file.endswith('.md'):
11+
valid_cards.add(file[:-3]) # Remove .md
12+
13+
# Regex to match Obsidian-style internal links: [[Link Name]] or [[Link Name|Alias]]
14+
link_pattern = re.compile(r'\[\[([^\]|\n\]]+)(?:\|[^\]]*)?\]\]')
15+
16+
broken_links = []
17+
18+
for filename in os.listdir(CARDS_DIR):
19+
if not filename.endswith('.md'):
20+
continue
21+
path = os.path.join(CARDS_DIR, filename)
22+
with open(path, 'r', encoding='utf-8') as f:
23+
for i, line in enumerate(f, 1):
24+
for match in link_pattern.finditer(line):
25+
link_target = match.group(1).strip()
26+
# Obsidian links are case-sensitive and must match the filename (without .md)
27+
if link_target not in valid_cards:
28+
broken_links.append((filename, i, link_target))
29+
30+
if broken_links:
31+
for card, line_num, link in broken_links:
32+
print(f"Broken link in {card} (line {line_num}): [[{link}]]")
33+
print(f"\nERROR: {len(broken_links)} broken internal link(s) found.")
34+
sys.exit(1)
35+
else:
36+
print("All internal links are valid.")

.github/workflows/check-cards.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ jobs:
2020
python -m pip install --upgrade pip
2121
2222
- name: Run card check script
23-
run: python .github/scripts/check-cards.py
23+
run: python .github/scripts/check-cards.py
24+
25+
- name: Run internal link check script
26+
run: python .github/scripts/check-internal-links.py

0 commit comments

Comments
 (0)