From b3403d5da699b886faa70f41fc61710a66aacc88 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 5 Dec 2026 04:09:42 -0800 Subject: [PATCH] block-ci: match doc links per paragraph, with nested parentheses and long fences A link whose label wraps onto the next line was never checked, a target such as guide(v2).md was cut at its first ')', and a three-backtick line inside a four-backtick fence closed it. Close a fence only with the same character at least as long, keep one level of balanced parentheses in a destination, and match links over each paragraph. Co-Authored-By: Claude Opus 5.5 --- .github/block/scripts/doc-links-check-test.py | 17 ++++++++ .github/block/scripts/doc-links-check.py | 40 +++++++++++-------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/.github/block/scripts/doc-links-check-test.py b/.github/block/scripts/doc-links-check-test.py index 911ecc0f90..c6543d650e 100644 --- a/.github/block/scripts/doc-links-check-test.py +++ b/.github/block/scripts/doc-links-check-test.py @@ -56,6 +56,23 @@ class DocLinksTests(unittest.TestCase): }) self.assertEqual(broken, []) + def test_wrapped_labels_parentheses_and_long_fences(self): + with tempfile.TemporaryDirectory() as root: + Path(root, "guide(v2).md").write_text("") + Path(root, "README.md").write_text("\n".join([ + "[guide](guide(v2).md) [old](guide(v1).md)", + "", + "see the [wrapped", + "label](wrapped.md) here", + "````md", + "```", + "[example](missing.md)", + "```", + "````", + ]) + "\n") + broken = [(line, target) for _, line, target in links.broken_links(root)] + self.assertEqual(broken, [(1, "guide(v1).md"), (3, "wrapped.md")]) + def test_main_exit_status(self): out = io.StringIO() with tempfile.TemporaryDirectory() as root, contextlib.redirect_stdout(out), contextlib.redirect_stderr(out): diff --git a/.github/block/scripts/doc-links-check.py b/.github/block/scripts/doc-links-check.py index 068832b1f4..409c4b122d 100644 --- a/.github/block/scripts/doc-links-check.py +++ b/.github/block/scripts/doc-links-check.py @@ -9,31 +9,39 @@ import sys import urllib.parse SKIP_DIRS = {".git", "node_modules", "vendor"} -FENCE = re.compile(r"^\s*(```|~~~)") +FENCE = re.compile(r"^\s*(`{3,}|~{3,})(.*)$") INLINE_CODE = re.compile(r"(`+)(?:(?!\1).)+?\1") -INLINE_LINK = re.compile(r"!?\[(?:[^\[\]]|\[[^\[\]]*\])*\]\(\s*(<[^>]*>|[^\s)]+)") -REFERENCE = re.compile(r"^\s{0,3}\[[^\]]+\]:\s*(<[^>]*>|\S+)") +INLINE_LINK = re.compile(r"!?\[(?:[^\[\]]|\[[^\[\]]*\])*\]\(\s*(<[^>\n]*>|(?:[^\s()]|\([^\s()]*\))+)") +REFERENCE = re.compile(r"^[ \t]{0,3}\[[^\]]+\]:\s*(<[^>]*>|\S+)", re.M) SCHEME = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*:") +def paragraph_targets(first, lines): + # A link label may wrap, so links are matched per paragraph. + text = INLINE_CODE.sub("", "\n".join(lines)) + found = [(m.start(), m.group(1)) for regex in (INLINE_LINK, REFERENCE) for m in regex.finditer(text)] + for pos, target in sorted(found): + yield first + text.count("\n", 0, pos), target + + def link_targets(text): - fence = None - for lineno, line in enumerate(text.splitlines(), 1): + fence, block, first = None, [], 1 + 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: + if fence is not None: + # Only a fence of the same character, at least as long, closes it. + if m and m.group(1)[0] == fence[0] and len(m.group(1)) >= len(fence) and not m.group(2).strip(): fence = None continue - if fence is not None: + if m or not line.strip(): + yield from paragraph_targets(first, block) + block = [] + if m: + fence = m.group(1) 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) + if not block: + first = lineno + block.append(line) def local_path(target):