Skip to content

Commit 6939f80

Browse files
fix(mobile): keep literal dollars and unwalkable replies out of math conversion
Charge a bare \[ fence line and stop inline reads at a backtick in the parse guard, leave replies whose literal $ md4c would pair with an inserted delimiter as authored, and fall back to the source when the range walker overflows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 53bf5f8 commit 6939f80

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎apps/mobile/src/lib/markdownDocument.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,12 @@ const PARAGRAPH_END = /(?:\r\n|\r(?!\n)|\n)(?: {0,3}>)*[ \t]*(?![^\r\n])/g;
449449
const LINE_END = /[\r\n]/g;
450450
const PAREN_CLOSE = /(?<!\\)(?:\\\\)*\\\)/g;
451451
const BRACKET_CLOSE = /(?<!\\)(?:\\\\)*\\\]/g;
452+
const TEX_FENCE_OPEN_REST = /[ \t]*(?![^\r\n])/y;
452453
const TEX_FENCE_CLOSE = /\\\][ \t]*(?![^\r\n])/g;
453454
const DOLLAR_FENCE_CLOSE = /(?<!\\)\$\$/g;
455+
// An inline `\(` or `\[` fails at the first backtick, so its read stops there.
456+
const BACKTICK = /`/g;
457+
const UNESCAPED_DOLLAR = /(?<!\\)(?:\\\\)*\$/g;
454458

455459
function mathParseFits(markdown: string): boolean {
456460
const stops = new Map<RegExp, number>();
@@ -467,9 +471,11 @@ function mathParseFits(markdown: string): boolean {
467471
let work = markdown.length * CHARACTER_COST;
468472
for (const _ of markdown.matchAll(/(?:\r\n?|\n)(?=[^\r\n])/g)) work += LINE_COST;
469473
let lineStart = 0;
470-
for (const match of markdown.matchAll(/\\[([]([ \t]*(?![^\r\n]))?|\$+/g)) {
471-
const [delimiter, texFence] = match;
474+
for (const match of markdown.matchAll(/\\[([]|\$+/g)) {
475+
const [delimiter] = match;
472476
const index = match.index;
477+
TEX_FENCE_OPEN_REST.lastIndex = index + 2;
478+
const texFence = delimiter === "\\[" && TEX_FENCE_OPEN_REST.test(markdown);
473479
if (index - lineStart > until(LINE_END, lineStart)) {
474480
lineStart = index;
475481
while (lineStart > 0 && !/[\r\n]/.test(markdown[lineStart - 1]!)) lineStart -= 1;
@@ -478,8 +484,11 @@ function mathParseFits(markdown: string): boolean {
478484
const paragraph = until(PARAGRAPH_END, index);
479485
const close = delimiter === "\\(" ? PAREN_CLOSE : delimiter[0] === "\\" ? BRACKET_CLOSE : null;
480486
// A closed formula consumes the openers inside it; only an unclosed one rereads its paragraph.
481-
if (close ? until(close, index + 2) + 2 >= paragraph : delimiter.length > 1) work += paragraph;
482-
if (texFence !== undefined) work += until(TEX_FENCE_CLOSE, index + 2);
487+
if (close) {
488+
const read = Math.min(paragraph, until(BACKTICK, index));
489+
if (until(close, index + 2) + 2 >= read) work += read;
490+
} else if (delimiter.length > 1) work += paragraph;
491+
if (texFence) work += until(TEX_FENCE_CLOSE, index + 2);
483492
else if (delimiter.length > 2 && delimiter[0] === "$") work += markdown.length - index;
484493
else if (delimiter === "$$") work += until(DOLLAR_FENCE_CLOSE, index + 2);
485494
if (work > MATH_PARSE_BUDGET) return false;
@@ -491,7 +500,21 @@ function mathParseFits(markdown: string): boolean {
491500
export function markdownWithDollarMath(markdown: string): string {
492501
if (!markdown.includes("\\(") && !markdown.includes("\\[")) return markdown;
493502
if (!mathParseFits(markdown)) return markdown;
494-
const ranges = markdownMathRanges(markdown);
503+
let ranges: ReturnType<typeof markdownMathRanges>;
504+
try {
505+
ranges = markdownMathRanges(markdown);
506+
} catch {
507+
// Deeply nested Markdown overflows the recursive range walker.
508+
return markdown;
509+
}
510+
// md4c would pair a literal `$` (a price, `$HOME`) with an inserted delimiter.
511+
let covering = 0;
512+
for (const match of markdown.matchAll(UNESCAPED_DOLLAR)) {
513+
const dollar = match.index + match[0].length - 1;
514+
while (covering < ranges.length && ranges[covering]!.end <= dollar) covering += 1;
515+
const range = ranges[covering];
516+
if (!range || range.start > dollar || range.source.startsWith("\\")) return markdown;
517+
}
495518
const edits: MarkdownEdit[] = [];
496519
for (const [index, { start, end, source }] of ranges.entries()) {
497520
const dollars = source.startsWith("\\(") ? "$" : source.startsWith("\\[") ? "$$" : null;

‎apps/mobile/src/lib/markdownDollarMath.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ describe("backslash math delimiters", () => {
6464
);
6565
});
6666

67+
it("leaves replies whose literal dollars md4c would pair with an inserted one", () => {
68+
for (const reply of [
69+
String.raw`It costs $5. The formula is \(\frac{1}{2}\).`,
70+
String.raw`Set $HOME. Then \(\lambda=1\).`,
71+
String.raw`Invalid \(a $b\) here.`,
72+
]) {
73+
expect(markdownWithDollarMath(reply)).toBe(reply);
74+
}
75+
});
76+
77+
it("leaves replies the shared parser would reread or cannot walk", () => {
78+
const formula = String.raw`\(x\)` + "\n\n";
79+
for (const reply of [
80+
formula + (String.raw`\[` + "\n\n").repeat(300),
81+
formula + (String.raw`\(` + "x".repeat(60) + "\n").repeat(80) + "`" + String.raw`\)`,
82+
"- ".repeat(2996) + String.raw`\(x\)`,
83+
]) {
84+
expect(markdownWithDollarMath(reply)).toBe(reply);
85+
}
86+
});
87+
6788
it("keeps container prefixes inside a multi-line formula", () => {
6889
expect(markdownWithDollarMath(String.raw`> \[` + "\n> a+b\n> " + String.raw`\]`)).toBe(
6990
"> $$\n> a+b\n> $$",

0 commit comments

Comments
 (0)