Match backtick runs in protected inline Markdown math (#4895)

Protected inline math ($`...`$) now matches the opening backtick run to the first closing run of the same length, as Markdown code spans do: ($``x` + y``$) renders as math, while ($`x` `y`$) keeps two code spans and literal dollars.

Co-authored-by: faysou <893862+faysou@users.noreply.github.com>

Co-authored-by: faysou <893862+faysou@users.noreply.github.com>
This commit is contained in:
faysou
2026-11-21 03:56:06 +08:00
committed by GitHub
co-authored by faysou
parent cb24f252c5
commit e7aec17d46
2 changed files with 15 additions and 3 deletions
+11
View File
@@ -37,6 +37,17 @@ describe("markdown LaTeX math", () => {
expect(root.querySelector("p")?.textContent?.startsWith("Prices $5 and $10, with ")).toBe(true);
});
it("leaves separate code spans inside dollar signs as code", () => {
const root = parseSafe("($`x` `y`$)");
expect(root.querySelector("p")?.innerHTML).toBe("($<code>x</code> <code>y</code>$)");
});
it("matches a longer backtick delimiter around math inside emphasis", () => {
const root = parseSafe("**($``x` + y``$)**");
expect(root.querySelector("strong .katex annotation")?.textContent).toBe("x` + y");
expect(root.querySelector("code")).toBeNull();
});
it("keeps an unfinished protected delimiter from swallowing a later formula", () => {
const root = parseSafe("Unfinished $`x` and $y$.");
expect(root.querySelector("p > code")?.textContent).toBe("x");
+4 -3
View File
@@ -39,9 +39,10 @@ function findClosingDelimiter(src: string, from: number, delim: string): number
return -1;
}
const PROTECTED_INLINE_DOLLAR = /^\$`([^`\r\n]+)`\$/;
// The first backtick run of the opening length ends the code span.
const PROTECTED_INLINE_DOLLAR = /^\$(?<ticks>`+)(?!`)(?<tex>(?:[^`\r\n]|(?!\k<ticks>(?!`))`+(?!`))+)\k<ticks>\$/;
// Marked masks code spans as `[aaa]` before calling emStrongMask
const MASKED_PROTECTED_INLINE_DOLLAR = /^\$\[(a+)\]\$/;
const MASKED_PROTECTED_INLINE_DOLLAR = /^\$\[(?<tex>a+)\]\$/;
/**
* Matches inline `$...$` from index 0 using Pandoc-style guards so currency
@@ -87,7 +88,7 @@ function matchInlineMath(
if (src[0] === "$") {
const match = protectedDollar.exec(src);
if (match) {
const text = match[1]!;
const text = match.groups!.tex!;
return text.trim() ? { raw: match[0], text, display: false } : null;
}
if (src[1] === "`") return null;