From 9d19a1a516a91ccf3033f67478108ac02f4fae4b Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 5 Dec 2026 07:44:32 -0800 Subject: [PATCH] fix(block-ci): preserve original link destination bytes --- .github/block/scripts/doc-links-check-test.py | 21 +++++++++++++++++++ .github/block/scripts/doc-links-check.py | 9 +++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/.github/block/scripts/doc-links-check-test.py b/.github/block/scripts/doc-links-check-test.py index f78d29c8ff..302ad4ba65 100644 --- a/.github/block/scripts/doc-links-check-test.py +++ b/.github/block/scripts/doc-links-check-test.py @@ -104,6 +104,27 @@ class DocLinksTests(unittest.TestCase): text = "Use `code\nmore`[r]: gone.md\n\n[r]: real.md\n" self.assertEqual(list(links.link_targets(text)), [(4, "real.md")]) + def test_code_masking_contract(self): + cases = [ + ("unequal runs", "`unmatched\n[real](real.md) ``", [], [(2, "real.md")]), + ("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")]), + ("existing literal target", "[example](`foo.md`)", ["`foo.md`"], []), + ("missing literal target", "[example](`foo.md`)", [], [(1, "`foo.md`")]), + ] + for name, text, existing, expected in cases: + with self.subTest(name=name), tempfile.TemporaryDirectory() as root: + Path(root, "README.md").write_text(text + "\n", encoding="utf-8") + for target in existing: + Path(root, target).touch() + broken = [(line, target) for _, line, target in links.broken_links(root)] + self.assertEqual(broken, expected) + masked = links.mask_code_spans(text) + self.assertEqual(len(masked), len(text)) + self.assertEqual([i for i, c in enumerate(masked) if c == "\n"], + [i for i, c in enumerate(text) if c == "\n"]) + 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 80534b9036..7c6a7c2d0e 100644 --- a/.github/block/scripts/doc-links-check.py +++ b/.github/block/scripts/doc-links-check.py @@ -41,15 +41,18 @@ def mask_code_spans(text): # Keep columns and non-whitespace line context as well as line numbers. for pos in range(start, runs[closing].end()): if text[pos] != "\n": - masked[pos] = "x" + masked[pos] = "\0" i = closing + 1 return "".join(masked) def paragraph_targets(first, lines): # A link label may wrap, so links are matched per paragraph. - text = mask_code_spans("\n".join(lines)) - found = [(m.start(), m.group(1)) for regex in (INLINE_LINK, REFERENCE) for m in regex.finditer(text)] + 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)] for pos, target in sorted(found): yield first + text.count("\n", 0, pos), target