diff --git a/CHANGELOG.md b/CHANGELOG.md index c411d126..6d3464de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## Unreleased + +### 🐛 Fixed + +* 🐛 Require whitespace between a link reference destination and its multiline title, preserving malformed definitions as paragraph text. + ## 4.3.0 - 2026-11-10 This release fixes two quadratic-time parsing paths that let crafted input stall rendering for minutes (security), diff --git a/markdown_it/rules_block/reference.py b/markdown_it/rules_block/reference.py index 175308f9..26b9f166 100644 --- a/markdown_it/rules_block/reference.py +++ b/markdown_it/rules_block/reference.py @@ -91,7 +91,6 @@ def reference(state: StateBlock, startLine: int, _endLine: int, silent: bool) -> # [label]: destination 'title' # ^^^ skipping those spaces - start = pos while pos < maximum: ch = charCodeAt(string, pos) if ch == 0x0A: @@ -107,6 +106,7 @@ def reference(state: StateBlock, startLine: int, _endLine: int, silent: bool) -> # [label]: destination 'title' # ^^^^^^^ parse this + titleStart = pos titleRes = state.md.helpers.parseLinkTitle(string, pos, maximum, None) while titleRes.can_continue: if (lineContent := getNextLine(state, nextLine)) is None: @@ -117,7 +117,7 @@ def reference(state: StateBlock, startLine: int, _endLine: int, silent: bool) -> nextLine += 1 titleRes = state.md.helpers.parseLinkTitle(string, pos, maximum, titleRes) - if pos < maximum and start != pos and titleRes.ok: + if pos < maximum and titleStart != destEndPos and titleRes.ok: title = titleRes.str pos = titleRes.pos else: diff --git a/tests/test_port/test_references.py b/tests/test_port/test_references.py index 97f8a65a..fc8b5d5f 100644 --- a/tests/test_port/test_references.py +++ b/tests/test_port/test_references.py @@ -50,3 +50,16 @@ def test_inline_definitions(data_regression): src = '[a]: url "title"\n- [a]: url "title"' tokens = md.parse(src) data_regression.check([token.as_dict() for token in tokens]) + + +def test_multiline_reference_title_requires_whitespace() -> None: + """A title glued to its destination leaves both lines in the paragraph.""" + md = MarkdownIt() + src = '[guide]\n\n[guide]: "First line\nsecond line"\n' + env: dict[str, object] = {} + tokens = md.parse(src, env) + assert [token.map for token in tokens if token.type == "paragraph_open"] == [ + [0, 1], + [2, 4], + ] + assert "references" not in env