--- a/mdit_py_plugins/dollarmath/index.py +++ b/mdit_py_plugins/dollarmath/index.py @@ -348,6 +348,9 @@ haveEndMarker = True label = eqnoMatch.group(1)[::-1] end = end - eqnoMatch.end() + # after the if/elif chain, check for ambiguous case + if not haveEndMarker and "$$" in lineText[2:]: + return False # search for end of block on subsequent line if not haveEndMarker: --- a/tests/test_dollarmath.py +++ b/tests/test_dollarmath.py @@ -104,3 +104,68 @@ text = md.render(input) print(text) assert text.rstrip() == expected.rstrip() + + +def test_same_line_close_with_trailing_text_not_swallowed(): + # today math_block_dollar's same-line search sets neither endswith("$$") + # nor a label, then the subsequent-line scan consumes everything up to + # the next "$$" line, so no paragraph is produced + md = MarkdownIt().use(dollarmath_plugin) + tokens = md.parse("$$a$$ text\n\nb\n\n$$c$$\n") + types = [t.type for t in tokens] + assert "paragraph_open" in types + math_blocks = [t for t in tokens if t.type == "math_block"] + assert len(math_blocks) == 1 + assert math_blocks[0].content == "c" + + +def test_same_line_close_still_block(): + md = MarkdownIt().use(dollarmath_plugin) + tokens = md.parse("$$a$$\n") + math_blocks = [t for t in tokens if t.type == "math_block"] + assert len(math_blocks) == 1 + assert math_blocks[0].content == "a" + + +def test_same_line_close_with_label_still_block_label(): + md = MarkdownIt().use(dollarmath_plugin) + tokens = md.parse("$$a$$ (eq1)\n") + labels = [t for t in tokens if t.type == "math_block_label"] + assert len(labels) == 1 + assert labels[0].info == "eq1" + + +def test_same_line_close_trailing_text_inline_double(): + # today the block rule consumes the line (and onward) instead of + # returning False, so no math_inline_double token is ever produced + md = MarkdownIt().use(dollarmath_plugin, double_inline=True) + tokens = md.parse("$$a$$ text\n") + inlines = [ + child + for t in tokens + if t.type == "inline" and t.children + for child in t.children + if child.type == "math_inline_double" + ] + assert len(inlines) == 1 + assert inlines[0].content == "a" + + +def test_same_line_close_trailing_text_no_labels(): + # with allow_labels=False the label branch is skipped, so today the + # subsequent-line scan runs; the fix must reject the line regardless + md = MarkdownIt().use(dollarmath_plugin, allow_labels=False) + tokens = md.parse("$$a$$ text\n\nb\n\n$$c$$\n") + types = [t.type for t in tokens] + assert "paragraph_open" in types + math_blocks = [t for t in tokens if t.type == "math_block"] + assert len(math_blocks) == 1 + assert math_blocks[0].content == "c" + + +def test_multiline_block_unchanged(): + md = MarkdownIt().use(dollarmath_plugin) + tokens = md.parse("$$\na\n$$\n") + math_blocks = [t for t in tokens if t.type == "math_block"] + assert len(math_blocks) == 1 + assert math_blocks[0].content == "\na\n"