fix(block-ci): preserve original link destination bytes

This commit is contained in:
pingqiu committed 2026-12-05 07:44:32 -08:00
1 parent 36d91b0cea
commit 9d19a1a516
2 files changed
+27 -3

No files matched your search

@@ -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):
+6 -3
View File
@@ -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