Files
seaweedfs/.github/block/scripts/doc-links-check.py
T
Chris LuandClaude Opus 5.5 ed7592dc4d ci: run the block documentation link check from root scripts
The Relative documentation links step called .github/scripts/doc-links-check.py
and its test inside seaweed-block/, but neither the imported module nor
upstream seaweed-block has them, so the unit suite would fail once the step
runs. Add the checker and its test under .github/block/scripts/ with the
other port scripts and point the step and its contract test there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 19:22:24 -08:00

82 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Fail when a Markdown file links to a relative path that does not exist.
Usage: doc-links-check.py <root>
"""
import os
import re
import sys
import urllib.parse
SKIP_DIRS = {".git", "node_modules", "vendor"}
FENCE = re.compile(r"^\s*(```|~~~)")
INLINE_CODE = re.compile(r"(`+)(?:(?!\1).)+?\1")
INLINE_LINK = re.compile(r"!?\[(?:[^\[\]]|\[[^\[\]]*\])*\]\(\s*(<[^>]*>|[^\s)]+)")
REFERENCE = re.compile(r"^\s{0,3}\[[^\]]+\]:\s*(<[^>]*>|\S+)")
SCHEME = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*:")
def link_targets(text):
fence = None
for lineno, line in enumerate(text.splitlines(), 1):
m = FENCE.match(line)
if m:
if fence is None:
fence = m.group(1)
elif m.group(1) == fence:
fence = None
continue
if fence is not None:
continue
line = INLINE_CODE.sub("", line)
for m in INLINE_LINK.finditer(line):
yield lineno, m.group(1)
m = REFERENCE.match(line)
if m:
yield lineno, m.group(1)
def local_path(target):
if target.startswith("<") and target.endswith(">"):
target = target[1:-1]
if not target or target.startswith(("#", "//")) or SCHEME.match(target):
return None
path = urllib.parse.unquote(target.split("#", 1)[0].split("?", 1)[0])
return path or None
def broken_links(root):
root = os.path.abspath(root)
for dirpath, dirnames, filenames in os.walk(root):
dirnames[:] = sorted(d for d in dirnames if d not in SKIP_DIRS)
for name in sorted(filenames):
if not name.endswith(".md"):
continue
source = os.path.join(dirpath, name)
with open(source, encoding="utf-8", errors="replace") as f:
text = f.read()
for lineno, target in link_targets(text):
path = local_path(target)
if path is None:
continue
if path.startswith("/"):
resolved = os.path.join(root, path.lstrip("/"))
else:
resolved = os.path.join(dirpath, path)
if not os.path.exists(resolved):
yield os.path.relpath(source, root), lineno, target
def main(argv):
if len(argv) != 2:
print(__doc__.strip(), file=sys.stderr)
return 2
broken = list(broken_links(argv[1]))
for source, lineno, target in broken:
print(f"{source}:{lineno}: broken relative link {target}")
return 1 if broken else 0
if __name__ == "__main__":
sys.exit(main(sys.argv))