Skip to content

Commit 1141eec

Browse files
committed
fix(math): support display and backtick math
Refs #330, #371, #369.
1 parent bf95c32 commit 1141eec

2 files changed

Lines changed: 57 additions & 6 deletions

File tree

src/mistune/plugins/math.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,36 @@
99

1010
__all__ = ["math", "math_in_quote", "math_in_list"]
1111

12-
BLOCK_MATH_PATTERN = r"^ {0,3}\$\$[ \t]*\n(?P<math_text>[\s\S]+?)\n\$\$[ \t]*$"
13-
INLINE_MATH_PATTERN = r"\$(?!\$)(?!\s)(?P<math_text>(?:[^$\\\n]|\\.)+?)\$(?!\d)"
12+
BLOCK_MATH_PATTERN = (
13+
r"^ {0,3}\$\$(?P<math_text_single>[^\n]*?)\$\$[ \t]*(?:\n|$)|"
14+
r"^ {0,3}\$\$[ \t]*\n(?P<math_text_multi>[\s\S]*?)\n\$\$[ \t]*(?:\n|$)"
15+
)
16+
INLINE_MATH_PATTERN = (
17+
r"\$\$(?P<display_math_text>(?:[^$\\]|\\.)*?)\$\$|"
18+
r"\$(?P<backtick_math_marker>`+)(?P<backtick_math_text>[\s\S]*?)(?P=backtick_math_marker)\$|"
19+
r"\$(?!\$)(?!\s)(?P<math_text>(?:[^$\\\n]|\\.)+?)\$(?!\d)"
20+
)
1421

1522

1623
def parse_block_math(block: "BlockParser", m: Match[str], state: "BlockState") -> int:
17-
text = m.group("math_text")
24+
text = m.group("math_text_single")
25+
if text is None:
26+
text = m.group("math_text_multi")
27+
assert text is not None
1828
state.append_token({"type": "block_math", "raw": text})
19-
return m.end() + 1
29+
return m.end()
2030

2131

2232
def parse_inline_math(inline: "InlineParser", m: Match[str], state: "InlineState") -> int:
23-
text = m.group("math_text")
33+
display_text = m.group("display_math_text")
34+
if display_text is not None:
35+
state.append_token({"type": "block_math", "raw": display_text})
36+
return m.end()
37+
38+
text = m.group("backtick_math_text")
39+
if text is None:
40+
text = m.group("math_text")
41+
assert text is not None
2442
state.append_token({"type": "inline_math", "raw": text})
2543
return m.end()
2644

@@ -50,7 +68,7 @@ def math(md: "Markdown") -> None:
5068
:param md: Markdown instance
5169
"""
5270
md.block.register("block_math", BLOCK_MATH_PATTERN, parse_block_math, before="list")
53-
md.inline.register("inline_math", INLINE_MATH_PATTERN, parse_inline_math, before="link")
71+
md.inline.register("inline_math", INLINE_MATH_PATTERN, parse_inline_math, before="codespan")
5472
if md.renderer and md.renderer.NAME == "html":
5573
md.renderer.register("block_math", render_block_math)
5674
md.renderer.register("inline_math", render_inline_math)

tests/fixtures/math.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,39 @@ Result is $x^2$.
7474
<p>Result is <span class="math">\(x^2\)</span>.</p>
7575
````````````````````````````````
7676

77+
```````````````````````````````` example
78+
$$x$$
79+
.
80+
<div class="math">$$
81+
x
82+
$$</div>
83+
````````````````````````````````
84+
85+
```````````````````````````````` example
86+
$$$$
87+
.
88+
<div class="math">$$
89+
90+
$$</div>
91+
````````````````````````````````
92+
93+
```````````````````````````````` example
94+
No newline $$x$$
95+
.
96+
<p>No newline <div class="math">$$
97+
x
98+
$$</div>
99+
</p>
100+
````````````````````````````````
101+
102+
```````````````````````````````` example
103+
$`a+%
104+
b`$
105+
.
106+
<p><span class="math">\(a+%
107+
b\)</span></p>
108+
````````````````````````````````
109+
77110
## Currency rejection
78111

79112
```````````````````````````````` example

0 commit comments

Comments
 (0)