Files
2nd/scratch/find_broken_links.py

37 lines
1.3 KiB
Python

import os
import re
wiki_root = r"E:\Wiki\2nd\10_Wiki"
all_files = set()
for root, dirs, files in os.walk(wiki_root):
for file in files:
if file.endswith(".md"):
name = os.path.splitext(file)[0]
all_files.add(name)
broken_links = []
link_pattern = re.compile(r"\[\[([^\]|]+)(?:\|[^\]]+)?\]\]")
for root, dirs, files in os.walk(wiki_root):
for file in files:
if file.endswith(".md"):
abs_path = os.path.join(root, file)
try:
with open(abs_path, "r", encoding="utf-8") as f:
content = f.read()
links = link_pattern.findall(content)
for link in links:
link_target = link.strip().replace("\\", "/").split("/")[-1]
if link_target and link_target not in all_files and link_target != "Index":
broken_links.append((os.path.relpath(abs_path, wiki_root), link))
except:
pass
if broken_links:
with open(r"E:\Wiki\2nd\scratch\broken_links.txt", "w", encoding="utf-8") as out:
for source, target in broken_links:
out.write(f"{source} -> {target}\n")
print(f"Found {len(broken_links)} broken links. Saved to orphans.txt")
else:
print("No broken links found!")