fix: invalid code span detection in reference extractor

Signed-off-by: squidfunk <martin.donath@squidfunk.com>
This commit is contained in:
squidfunk
2026-08-14 18:36:52 +02:00
parent cfdf18f19b
commit 6774d6159c
2 changed files with 34 additions and 0 deletions
+21
View File
@@ -1819,6 +1819,27 @@ class TestInlineCode:
assert text(md, links[0].text) == b"text"
assert text(md, links[0].href) == b"href"
@pytest.mark.parametrize(
"md",
[
pytest.param(
b"a`\n\n`[b]`",
id="blank-line",
),
pytest.param(
b"a`\n \n`[b]`",
id="whitespace-only-line",
),
pytest.param(
b"a`\r\n\r\n`[b]`",
id="blank-line-crlf",
),
],
)
def test_inline_code_does_not_cross_blank_line(self, md: bytes) -> None:
refs = collect(md)
assert len(refs) == 0
# --- negative cases ---
@pytest.mark.parametrize(
@@ -1083,6 +1083,19 @@ def _scan_inline_code(cursor: Cursor) -> int | None:
# Search for matching closing backticks
while pos < cursor.end:
# Inline code spans may contain line endings, but cannot cross a
# blank line because that starts a new block in Python-Markdown.
if cursor.data[pos] in (_CR, _NL):
line = pos + 1
if (
cursor.data[pos] == _CR
and line < cursor.end
and cursor.data[line] == _NL
):
line += 1
if _is_blank_line(cursor, line):
return None
if cursor.data[pos] == _BACKTICK:
end = pos