From 10035593e3973d57355a827c55bd6bb5553bab37 Mon Sep 17 00:00:00 2001 From: Yuzhong Zhang Date: Sat, 29 Aug 2026 19:35:07 +0000 Subject: [PATCH] fix: strip nested prefixes from multi-line dollarmath blocks Fixes #125 --- mdit_py_plugins/dollarmath/index.py | 18 +++++++++++++++- tests/fixtures/dollar_math.md | 33 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/mdit_py_plugins/dollarmath/index.py b/mdit_py_plugins/dollarmath/index.py index 57ba590..e24b050 100644 --- a/mdit_py_plugins/dollarmath/index.py +++ b/mdit_py_plugins/dollarmath/index.py @@ -287,6 +287,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, @@ -365,7 +381,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: diff --git a/tests/fixtures/dollar_math.md b/tests/fixtures/dollar_math.md index 08a2e90..d4421d8 100644 --- a/tests/fixtures/dollar_math.md +++ b/tests/fixtures/dollar_math.md @@ -418,6 +418,39 @@ a+b=c . +display equation in blockquote, multi-line. (valid=True) +. +> Given +> +> $$ +> x^2 + y^2 = 9 +> $$ +> +> What is the radius of the circle? +. +
+

Given

+
+x^2 + y^2 = 9 +
+

What is the radius of the circle?

+
+. + +labelled display equation in blockquote, multi-line. (valid=True) +. +> $$ +> a+b=c +> $$ (2) +. +
+
+ΒΆ +a+b=c +
+
+. + mixed syntax: . $$