fix: two backticks with no closing run trip up link parser (#663) (#665)

Signed-off-by: squidfunk <martin.donath@squidfunk.com>
This commit is contained in:
Martin Donath
2026-05-12 09:43:32 +02:00
committed by GitHub
parent 5ef9ee4ee3
commit c338b270b5
2 changed files with 19 additions and 2 deletions
@@ -1554,6 +1554,10 @@ class TestInlineCode:
b"``text`[text](href)``",
id="inline-code, inner backticks",
),
pytest.param(
b"`` `[text](href)`",
id="inline-code, empty code span",
),
],
)
def test_inline_code(self, md: bytes) -> None:
@@ -198,12 +198,25 @@ def _scan(cursor: Cursor) -> Iterator[Reference]:
# Code block: ```...``` or `...`
if char == _BACKTICK:
if cursor.at_line_start():
# Consume the full run so a failed match does not leave trailing
# backticks to be rescanned as new delimiters.
count = 1
while cursor.peek(count) == _BACKTICK:
count += 1
if (
cursor.at_line_start()
and count >= 3 # noqa: PLR2004
and cursor.peek(count) in (_SPACE, _TAB, _NL, _CR, -1)
):
end = _scan_fenced_code(cursor, _BACKTICK)
if end is not None:
cursor.advance(end - cursor.pos)
continue
# Literal
cursor.advance(count)
continue
# Inline code: `...`
end = _scan_inline_code(cursor)
if end is not None:
@@ -211,7 +224,7 @@ def _scan(cursor: Cursor) -> Iterator[Reference]:
continue
# Literal
cursor.advance(1)
cursor.advance(count)
continue
# Code block: ~~~...~~~