File size: 3,181 Bytes
845996e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
--- 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"