fix(block-ci): validate original destination syntax

This commit is contained in:
pingqiu committed 2026-12-05 07:55:05 -08:00
1 parent 9d19a1a516
commit 286cbf4ecb
2 files changed
+11 -4

No files matched your search

@@ -110,6 +110,7 @@ class DocLinksTests(unittest.TestCase):
("escaped delimiters", "\\`\n[real](real.md)\n\\`", [], [(2, "real.md")]),
("reference context", "Use `code\nmore`[r]: gone.md\n\n[r]: real.md", [], [(4, "real.md")]),
("wrapped span", "`[code\nhere](hidden.md)`\n[after](after.md)", [], [(3, "after.md")]),
("hidden-space invalid target", "[example](`foo bar`)", [], []),
("existing literal target", "[example](`foo.md`)", ["`foo.md`"], []),
("missing literal target", "[example](`foo.md`)", [], [(1, "`foo.md`")]),
]
+10 -4
View File
@@ -11,8 +11,10 @@ import urllib.parse
SKIP_DIRS = {".git", "node_modules", "vendor"}
FENCE = re.compile(r"^\s*(`{3,}|~{3,})(.*)$")
BACKTICKS = re.compile(r"`+")
INLINE_LINK = re.compile(r"!?\[(?:[^\[\]]|\[[^\[\]]*\])*\]\(\s*(<[^>\n]*>|(?:[^\s()]|\([^\s()]*\))+)")
REFERENCE = re.compile(r"^[ \t]{0,3}\[[^\]]+\]:\s*(<[^>]*>|\S+)", re.M)
INLINE_DESTINATION = r"<[^>\n]*>|(?:[^\s()]|\([^\s()]*\))+"
REFERENCE_DESTINATION = r"<[^>]*>|\S+"
INLINE_LINK = re.compile(r"!?\[(?:[^\[\]]|\[[^\[\]]*\])*\]\(\s*(" + INLINE_DESTINATION + r")")
REFERENCE = re.compile(r"^[ \t]{0,3}\[[^\]]+\]:\s*(" + REFERENCE_DESTINATION + r")", re.M)
SCHEME = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*:")
@@ -51,8 +53,12 @@ def paragraph_targets(first, lines):
original = "\n".join(lines)
text = mask_code_spans(original)
# Masked bytes are context only, never a synthetic filesystem destination.
found = [(m.start(), original[m.start(1):m.end(1)])
for regex in (INLINE_LINK, REFERENCE) for m in regex.finditer(text)]
found = []
for regex, syntax in ((INLINE_LINK, INLINE_DESTINATION), (REFERENCE, REFERENCE_DESTINATION)):
for match in regex.finditer(text):
target = original[match.start(1):match.end(1)]
if re.fullmatch(syntax, target):
found.append((match.start(), target))
for pos, target in sorted(found):
yield first + text.count("\n", 0, pos), target