fix(web): render display math whose $$ hug a multi-line formula (#1623)

processMarkdownContent only moved $$ onto their own lines when the whole
block fit on one line. $$a \\\\ ... $$ spread over several lines kept its
fences glued to the formula and remark-math left it as text. Now a line
that opens with a lone $$ is matched to the next line ending in $$ and both
fences are put on their own lines; blank lines stop the search.
This commit is contained in:
Maxi
2026-09-28 17:28:22 +02:00
parent ef2d9e5c3c
commit a2eb9acd36
2 changed files with 71 additions and 0 deletions

View File

@@ -65,6 +65,26 @@ function looksLikeLatexBlock(lines: string[]): boolean {
return block.length > 0 && LIKELY_LATEX_BLOCK_RE.test(block);
}
function countFences(line: string): number {
return line.match(/\$\$/g)?.length ?? 0;
}
/**
* The line that closes a display block opened above `start`: the next line
* with a single `$$`, at its end. A blank line or a line that opens another
* block first means there is nothing to close.
*/
function findDisplayClose(lines: string[], start: number): number {
for (let j = start; j < lines.length; j++) {
const trimmed = lines[j].trim();
if (!trimmed) return -1;
const fences = countFences(trimmed);
if (fences === 0) continue;
return fences === 1 && trimmed.endsWith("$$") ? j : -1;
}
return -1;
}
function normalizeEditorMdInlineMath(content: string): string {
return mapMarkdownProse(content, normalizeEditorMdInlineMathInProse);
}
@@ -97,6 +117,26 @@ function normalizeEditorMdInlineMathInProse(content: string): string {
}
}
// `$$` hugging a formula that spans several lines, e.g.
// `$$a = b\\` / `c = d$$` (#1623). remark-math only reads a multi-line
// display block with each fence on its own line, so move them there.
if (trimmed.startsWith("$$") && countFences(trimmed) === 1) {
const endIdx = findDisplayClose(lines, i + 1);
const first = trimmed.slice(2).trim();
const last = endIdx === -1 ? "" : lines[endIdx].trim().slice(0, -2).trim();
if (endIdx !== -1 && (first || last)) {
result.push("$$");
if (first) result.push(first);
for (let j = i + 1; j < endIdx; j++) {
result.push(lines[j]);
}
if (last) result.push(last);
result.push("$$");
i = endIdx;
continue;
}
}
if (
/^\$\$[\s\S]+\$\$$/.test(trimmed) &&
(trimmed.match(/\$\$/g)?.length ?? 0) === 2

View File

@@ -156,6 +156,37 @@ test("inline math: standalone one-line $$...$$ → split to block", () => {
assert.equal(lines[2], "$$");
});
test("display math: $$ hugging a multi-line formula → fences on their own lines (#1623)", () => {
const input = "$$Testing\\ Latex \\rightarrow\n\\leftarrow ENTER\\ HERE$$";
const lines = processMarkdownContent(input).trim().split("\n");
assert.deepEqual(lines, [
"$$",
"Testing\\ Latex \\rightarrow",
"\\leftarrow ENTER\\ HERE",
"$$",
]);
});
test("display math: aligned block with the opening fence hugging, closing alone", () => {
const input = "$$\\begin{aligned}\na &= b \\\\\nc &= d\n\\end{aligned}\n$$";
const lines = processMarkdownContent(input).trim().split("\n");
assert.equal(lines[0], "$$");
assert.equal(lines[1], "\\begin{aligned}");
assert.equal(lines[lines.length - 2], "\\end{aligned}");
assert.equal(lines[lines.length - 1], "$$");
});
test("display math: fences already on their own lines are left alone", () => {
const input = "$$\na = b\nc = d\n$$";
assert.equal(processMarkdownContent(input).trim(), input);
});
test("display math: an unclosed $$ across a blank line is not rewritten", () => {
const input = "$$x = 1\n\nprice is 5$ tomorrow";
const result = processMarkdownContent(input);
assert.ok(result.includes("$$x = 1"));
});
// ---------------------------------------------------------------------------
// processMarkdownContent — loose block math ($...\n...\n$) promotion
// ---------------------------------------------------------------------------