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 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-05 04:09:42 -08:00
1 parent 3fd8862426
commit b3403d5da6
2 files changed
+41 -16

No files matched your search

@@ -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):
+24 -16
View File
@@ -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):