From 33fc00e16547a1c4c2327206b40f9db2d74a276d Mon Sep 17 00:00:00 2001 From: Kyle King Date: Mon, 20 Jul 2026 22:35:57 -0600 Subject: [PATCH 1/3] fix(#147): prevent greedily mismatching $$ --- mdit_py_plugins/dollarmath/index.py | 7 ++++++ tests/fixtures/dollar_math.md | 35 +++++++++++++++++++++++++++++ tests/test_dollarmath.py | 15 +++++++++++++ 3 files changed, 57 insertions(+) diff --git a/mdit_py_plugins/dollarmath/index.py b/mdit_py_plugins/dollarmath/index.py index 57ba590..c8f33e8 100644 --- a/mdit_py_plugins/dollarmath/index.py +++ b/mdit_py_plugins/dollarmath/index.py @@ -329,6 +329,13 @@ def _math_block_dollar( haveEndMarker = True label = eqnoMatch.group(1)[::-1] end = end - eqnoMatch.end() + elif "$$" in lineText[2:]: + # a closing marker is present but followed by trailing + # content that isn't a label suffix: ambiguous, so + # don't greedily scan subsequent lines for it + return False + elif "$$" in lineText[2:]: + return False # search for end of block on subsequent line if not haveEndMarker: diff --git a/tests/fixtures/dollar_math.md b/tests/fixtures/dollar_math.md index 08a2e90..95ec2a8 100644 --- a/tests/fixtures/dollar_math.md +++ b/tests/fixtures/dollar_math.md @@ -580,3 +580,38 @@ Indented by 4 spaces, DISABLE-CODEBLOCKS a . + +same-line-closed block with trailing text is not absorbed into +a later math block on a following line. (valid=True) +. +$$a$$ trailing +$$c$$ +. +

a
trailing +
c

+. + +same-line-closed block with trailing text inside a list is not +absorbed into a later list item. (valid=True) +. +1. $$a$$ +1. $$b$$ trailing + 1. $$c$$ +. +
    +
  1. +
    +a +
    +
  2. +
  3. b
    trailing +
      +
    1. +
      +c +
      +
    2. +
    +
  4. +
+. diff --git a/tests/test_dollarmath.py b/tests/test_dollarmath.py index a43dc10..789805e 100644 --- a/tests/test_dollarmath.py +++ b/tests/test_dollarmath.py @@ -64,6 +64,21 @@ def test_block_func(): } +@pytest.mark.parametrize("allow_labels", [True, False]) +def test_block_func_ambiguous_trailing_content(allow_labels): + """A same-line close followed by non-label trailing content is + ambiguous, so the block rule should not match and greedily scan + subsequent lines for the next ``$$``. + """ + block_func = main.math_block_dollar(allow_labels=allow_labels) + md = MarkdownIt() + src = "$$b$$ trailing\n$$c$$\n" + tokens = [] + state = StateBlock(src, md, {}, tokens) + assert block_func(state, 0, 2, False) is False + assert tokens == [] + + def test_plugin_parse(data_regression): md = MarkdownIt().use(dollarmath_plugin) tokens = md.parse( From c74b5be9462f629a1638ee520f4173a6bb7ceb9c Mon Sep 17 00:00:00 2001 From: Kyle King Date: Mon, 20 Jul 2026 22:36:20 -0600 Subject: [PATCH 2/3] docs: update changelog --- CHANGELOG.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1b78b3..170c2fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Change Log +## Unreleased + +- 🐛 FIX: `dollarmath` block rule no longer absorbs following content when a same-line-closed `$$...$$` has non-label trailing text (#147) + + A same-line close followed by trailing content that wasn't a label suffix (e.g. `` $$b$$ trailing ``) wasn't recognized as closed, so the rule fell through to its multi-line scan and silently swallowed every following line, including subsequent list items, up to the next `$$` anywhere later in the document: + + ```markdown + 1. $$a$$ + 1. $$b$$ trailing + 1. $$c$$ + ``` + + This ambiguous case (closing marker present, but not at end of line and not a label) is now rejected outright, letting the line fall through to normal inline parsing instead. + ## 0.7.0 - 2026-07-19 - ✨ NEW: Add section reference plugin (`section_ref`) (#144) From b12cd1d2564be9bd9fb7b065e4af9d54d06d7934 Mon Sep 17 00:00:00 2001 From: Kyle King Date: Tue, 21 Jul 2026 08:13:50 -0600 Subject: [PATCH 3/3] refactor: de-dupe the trailing_close check from review --- mdit_py_plugins/dollarmath/index.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/mdit_py_plugins/dollarmath/index.py b/mdit_py_plugins/dollarmath/index.py index c8f33e8..5c36b24 100644 --- a/mdit_py_plugins/dollarmath/index.py +++ b/mdit_py_plugins/dollarmath/index.py @@ -322,20 +322,21 @@ def _math_block_dollar( if lineText.strip().endswith("$$"): haveEndMarker = True end = end - 2 - (len(lineText) - len(lineText.strip())) - elif allow_labels: - # reverse the line and match - eqnoMatch = DOLLAR_EQNO_REV.match(lineText[::-1]) - if eqnoMatch: - haveEndMarker = True - label = eqnoMatch.group(1)[::-1] - end = end - eqnoMatch.end() - elif "$$" in lineText[2:]: - # a closing marker is present but followed by trailing - # content that isn't a label suffix: ambiguous, so - # don't greedily scan subsequent lines for it + else: + # a closing marker present but not at end of line and not a label + # suffix is ambiguous, so don't greedily scan subsequent lines for it + trailing_close = "$$" in lineText[2:] + if allow_labels: + # reverse the line and match + eqnoMatch = DOLLAR_EQNO_REV.match(lineText[::-1]) + if eqnoMatch: + haveEndMarker = True + label = eqnoMatch.group(1)[::-1] + end = end - eqnoMatch.end() + elif trailing_close: + return False + elif trailing_close: return False - elif "$$" in lineText[2:]: - return False # search for end of block on subsequent line if not haveEndMarker: