diff --git a/.github/block/scripts/doc-links-check-test.py b/.github/block/scripts/doc-links-check-test.py index 302ad4ba65..07f67e2d23 100644 --- a/.github/block/scripts/doc-links-check-test.py +++ b/.github/block/scripts/doc-links-check-test.py @@ -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`")]), ] diff --git a/.github/block/scripts/doc-links-check.py b/.github/block/scripts/doc-links-check.py index 7c6a7c2d0e..926aa2f4e1 100644 --- a/.github/block/scripts/doc-links-check.py +++ b/.github/block/scripts/doc-links-check.py @@ -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