feat: add tests, CI and P0 reliability features
This commit is contained in:
1 parent
0754065dae
commit
626650e44a
22 files changed
+1576
-49
No files matched your search
@@ -0,0 +1,79 @@
|
||||
"""Check relative links and image paths in Markdown files.
|
||||
|
||||
Usage:
|
||||
python tools/check_links.py
|
||||
python tools/check_links.py docs README.md
|
||||
|
||||
External links are not fetched; only local targets are verified. Returns a
|
||||
non-zero exit code when a local link points to a missing file or directory.
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import urllib.parse
|
||||
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
SKIP_DIRS = {".git", ".gitea", "cache", "data", "__pycache__", "node_modules"}
|
||||
EXTERNAL_PREFIXES = ("http://", "https://", "mailto:", "tel:", "data:")
|
||||
LINK_RE = re.compile(r"!?\[[^\]]*\]\(([^)]+)\)")
|
||||
|
||||
|
||||
def iter_markdown(paths):
|
||||
for path in paths:
|
||||
if os.path.isdir(path):
|
||||
for dirpath, dirs, files in os.walk(path):
|
||||
dirs[:] = sorted(d for d in dirs if d not in SKIP_DIRS)
|
||||
for name in sorted(files):
|
||||
if name.endswith(".md"):
|
||||
yield os.path.join(dirpath, name)
|
||||
elif path.endswith(".md"):
|
||||
yield path
|
||||
|
||||
|
||||
def link_targets(text):
|
||||
for lineno, line in enumerate(text.splitlines(), 1):
|
||||
for raw in LINK_RE.findall(line):
|
||||
target = raw.strip()
|
||||
if target.startswith("<") and target.endswith(">"):
|
||||
target = target[1:-1].strip()
|
||||
if not target or target.startswith(EXTERNAL_PREFIXES):
|
||||
continue
|
||||
target = target.split("#", 1)[0].strip()
|
||||
if not target:
|
||||
continue
|
||||
yield lineno, target
|
||||
|
||||
|
||||
def check_file(path):
|
||||
errors = []
|
||||
with open(path, encoding="utf-8", errors="replace") as fh:
|
||||
text = fh.read()
|
||||
for lineno, target in link_targets(text):
|
||||
if target.startswith("/"):
|
||||
resolved = os.path.join(BASE_DIR, target.lstrip("/"))
|
||||
else:
|
||||
resolved = os.path.join(os.path.dirname(path), target)
|
||||
resolved = os.path.normpath(urllib.parse.unquote(resolved.split(" ", 1)[0]))
|
||||
if not os.path.exists(resolved):
|
||||
errors.append(f"{path}:{lineno}: missing local link target: {target}")
|
||||
return errors
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
argv = argv if argv is not None else sys.argv[1:]
|
||||
targets = [os.path.join(BASE_DIR, p) for p in argv] or [BASE_DIR]
|
||||
files = list(iter_markdown(targets))
|
||||
errors = []
|
||||
for path in files:
|
||||
errors.extend(check_file(path))
|
||||
if errors:
|
||||
print("\n".join(errors), file=sys.stderr)
|
||||
print(f"Broken links: {len(errors)}", file=sys.stderr)
|
||||
return 1
|
||||
print(f"Checked {len(files)} Markdown files: no broken local links")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user