Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion mdit_py_plugins/dollarmath/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,22 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool:
DOLLAR_EQNO_REV = re.compile(r"^\s*\)([^)$\r\n]+?)\(\s*\${2}")


def _math_block_content(
state: StateBlock, startLine: int, startPos: int, endLine: int, end: int
) -> str:
"""Extract math body, omitting parent container prefixes on each line."""
content_start = startPos + 2
if startLine == endLine:
return state.src[content_start:end]

parts: list[str] = [state.src[content_start : state.eMarks[startLine]], "\n"]
for line in range(startLine + 1, endLine):
parts.append(state.src[state.bMarks[line] : state.eMarks[line]])
parts.append("\n")
parts.append(state.src[state.bMarks[endLine] : end])
return "".join(parts)


def math_block_dollar(
allow_labels: bool = True,
label_normalizer: Callable[[str], str] | None = None,
Expand Down Expand Up @@ -392,7 +408,7 @@ def _math_block_dollar(

token = state.push("math_block_label" if label else "math_block", "math", 0)
token.block = True
token.content = state.src[startPos + 2 : end]
token.content = _math_block_content(state, startLine, startPos, nextLine, end)
token.markup = "$$"
token.map = [startLine, state.line]
if label:
Expand Down
63 changes: 63 additions & 0 deletions tests/fixtures/dollar_math.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,69 @@ a+b=c
</blockquote>
.

display equation in blockquote, multi-line. (valid=True)
.
> Given
>
> $$
> x^2 + y^2 = 9
> $$
>
> What is the radius of the circle?
.
<blockquote>
<p>Given</p>
<div class="math block">
x^2 + y^2 = 9
</div>
<p>What is the radius of the circle?</p>
</blockquote>
.

labelled display equation in blockquote, multi-line. (valid=True)
.
> $$
> a+b=c
> $$ (2)
.
<blockquote>
<div id="2" class="math block">
<a href="#2" class="mathlabel" title="Permalink to this equation">¶</a>
a+b=c
</div>
</blockquote>
.

display equation in nested blockquote, multi-line. (valid=True)
.
> > $$
> > a
> > b
> > $$
.
<blockquote>
<blockquote>
<div class="math block">
a
b
</div>
</blockquote>
</blockquote>
.

display equation in blockquote, content on the delimiter lines. (valid=True)
.
> $$ a
> b $$
.
<blockquote>
<div class="math block">
a
b
</div>
</blockquote>
.

mixed syntax:
.
$$
Expand Down
12 changes: 12 additions & 0 deletions tests/test_dollarmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ def test_custom_renderer(data_regression):
assert md.render("$x$").strip() == '<p><span class="math inline">x</span></p>'


def test_block_in_blockquote_allow_blank_lines():
"""A blank ``>`` line inside a blockquoted ``$$`` block keeps no ``>`` prefix.

The fixture tests run with ``allow_blank_lines=False``, so this case needs its own test.
"""
md = MarkdownIt("commonmark").use(dollarmath_plugin, allow_blank_lines=True)
tokens = md.parse("> $$\n> a\n>\n> b\n> $$\n")
math_tokens = [t for t in tokens if t.type.startswith("math")]
assert [t.type for t in math_tokens] == ["math_block"]
assert math_tokens[0].content == "\na\n\nb\n"


@pytest.mark.parametrize(
"line,title,input,expected",
read_fixture_file(FIXTURE_PATH.joinpath("dollar_math.md")),
Expand Down
Loading