Skip to content
Open
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
10 changes: 6 additions & 4 deletions packages/core/src/markdownit/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,15 @@ function math_block(
if (silent) {
return true;
}
if (firstLine.trim().slice(-2) === "$$") {
// Single line expression
firstLine = firstLine.trim().slice(0, -2);
let attrStr = undefined;
const singleLineMatch = firstLine.trim().match(/^(.*)\$\$\s*(\{.*\})?\s*$/);
if (singleLineMatch) {
// Single line expression, optionally followed by attributes (e.g. {#eq-label})
firstLine = singleLineMatch[1];
attrStr = singleLineMatch[2];
found = true;
}

let attrStr = undefined;
for (next = start; !found; ) {
next++;

Expand Down
42 changes: 42 additions & 0 deletions packages/core/test/markdownit-math.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import assert from "node:assert/strict";
import test from "node:test";

import MarkdownIt from "markdown-it";
import { mathjaxPlugin } from "../src/markdownit/math";

const parse = (src: string) => {
const md = new MarkdownIt({ html: true });
md.use(mathjaxPlugin);
return md.parse(src, {});
};

test("single-line math with attributes does not swallow the rest of the document", () => {
const tokens = parse(
"$$1+1$$ {#eq-spec0}\n\n```{python}\n2+2\n```\n"
);
const math = tokens.filter((t) => t.type === "math_block");
const fences = tokens.filter((t) => t.type === "fence");
assert.equal(math.length, 1);
assert.equal(math[0].info, "{#eq-spec0}");
assert.equal(math[0].content.trim(), "1+1");
assert.equal(fences.length, 1);
});

test("single-line math without attributes still parses", () => {
const tokens = parse("$$1+1$$\n\n```{python}\n2+2\n```\n");
const math = tokens.filter((t) => t.type === "math_block");
const fences = tokens.filter((t) => t.type === "fence");
assert.equal(math.length, 1);
assert.equal(math[0].content.trim(), "1+1");
assert.equal(fences.length, 1);
});

test("multi-line math with attributes on the closing line still parses", () => {
const tokens = parse("$$\n1+1\n$$ {#eq-spec0}\n\n```{python}\n2+2\n```\n");
const math = tokens.filter((t) => t.type === "math_block");
const fences = tokens.filter((t) => t.type === "fence");
assert.equal(math.length, 1);
assert.equal(math[0].info, "{#eq-spec0}");
assert.equal(math[0].content.trim(), "1+1");
assert.equal(fences.length, 1);
});