Skip to content

Commit 65cbb52

Browse files
committed
fix(mobile): isolate math markers from authored backticks
1 parent 84fc998 commit 65cbb52

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎apps/mobile/modules/t3-markdown-text/src/nativeMarkdownMath.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ describe("native math parsing", () => {
4545
expect(nodes.map((node) => node.content ?? node.children?.[0]?.content)).toEqual(contents);
4646
});
4747

48+
it.each(["`\\(x\\)", "``\\(x\\)", "\\(x\\)`", "``before \\(x\\) after ````"])(
49+
"restores math beside unmatched authored backticks: %s",
50+
(source) => {
51+
expect(parseNativeMarkdownMath(source, codeSpans).children).toEqual([
52+
{ type: "math_inline", content: "x", children: [] },
53+
]);
54+
},
55+
);
56+
4857
it.each([
4958
"Pay $20 or $30.",
5059
String.raw`Unfinished \[\sqrt{x}`,
@@ -65,7 +74,7 @@ describe("native math parsing", () => {
6574
const parse = vi.fn(codeSpans);
6675
const result = parseNativeMarkdownMath(source, parse);
6776
expect(parse).toHaveBeenCalledWith(
68-
"**Growth `t3-math::0`** and `t3-math:0` then `t3-math::1`.",
77+
"**Growth ``t3-math::0``** and `t3-math:0` then ``t3-math::1``.",
6978
);
7079
expect(result.children?.map((node) => node.type)).toEqual([
7180
"math_inline",

‎apps/mobile/modules/t3-markdown-text/src/nativeMarkdownMath.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export function parseNativeMarkdownMath(
1313

1414
let prefix = "t3-math:";
1515
while (markdown.includes(prefix)) prefix += ":";
16+
// Authored backticks, including unmatched ones, cannot close a placeholder.
17+
let fenceLength = 1;
18+
for (const [run] of markdown.matchAll(/`+/g)) {
19+
fenceLength = Math.max(fenceLength, run.length + 1);
20+
}
21+
const fence = "`".repeat(fenceLength);
1622
const formulas = new Map<string, MarkdownNode>();
1723
let source = "";
1824
let offset = 0;
@@ -36,7 +42,7 @@ export function parseNativeMarkdownMath(
3642
source += markdown.slice(offset, start);
3743
// Adjacent closing/opening backticks merge into one CommonMark delimiter.
3844
if (source.endsWith("`")) source += " ";
39-
source += `\`${marker}\``;
45+
source += `${fence}${marker}${fence}`;
4046
if (markdown[end] === "`") source += " ";
4147
offset = end;
4248
} else if ("children" in node) {

0 commit comments

Comments
 (0)