Skip to content

Commit 7f3cc3d

Browse files
StiensWoutclaude
andcommitted
perf(mobile): skip the math grammar when dollars sit only in code
Agent replies often carry `$` only in shell prompts or template literals. Parse natively first and run the JS math grammar only when native text holds a `$` or the source has a backslash opener, so streaming code-heavy messages no longer pay a full remark parse per update. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent f6e1ba7 commit 7f3cc3d

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@ import { parseComposerContextHref } from "@t3tools/shared/composerContextReferen
33
import type { NativeMarkdownTextRun } from "./nativeMarkdownText";
44
import type { MarkdownNode } from "react-native-nitro-markdown/headless";
55

6+
function hasDollarText(node: MarkdownNode): boolean {
7+
return node.type === "text"
8+
? (node.content?.includes("$") ?? false)
9+
: (node.children?.some(hasDollarText) ?? false);
10+
}
11+
612
/** Shield math from the native parser, then restore typed nodes using the shared grammar. */
713
export function parseNativeMarkdownMath(
814
source: string,
915
parse: (markdown: string) => MarkdownNode,
1016
): MarkdownNode {
17+
const document = parse(source);
18+
// Agent output often has `$` only in shell or template code. Math restores only
19+
// into native text, so skip the JS grammar pass unless native text could hold it.
20+
// Backslash openers are checked in the source because escapes drop the backslash.
21+
if (!source.includes("\\(") && !source.includes("\\[") && !hasDollarText(document))
22+
return document;
1123
const ranges = markdownMathRanges(source);
12-
if (ranges.length === 0) return parse(source);
24+
if (ranges.length === 0) return document;
1325
let prefix = ":t3-math-";
1426
while (source.includes(prefix)) prefix += "-";
1527
const mathByMarker = new Map(ranges.map((math, index) => [`${prefix}${index}:`, math]));

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ describe("native math", () => {
7373
]);
7474
});
7575

76+
it("typesets dollar math in native text but not dollars inside native code", () => {
77+
const asText = (markdown: string) => ({
78+
type: "paragraph" as const,
79+
children: [{ type: "text" as const, content: markdown }],
80+
});
81+
expect(
82+
nativeMarkdownDocumentRuns(parseNativeMarkdownMath("Energy $E$ here", asText)),
83+
).toContainEqual({ text: "$E$", mathSource: "$E$", role: "body" });
84+
const code = "```sh\necho $HOME/$E$\n```";
85+
const nativeCode = { type: "code_block" as const, content: "echo $HOME/$E$\n" };
86+
expect(parseNativeMarkdownMath(code, () => nativeCode)).toBe(nativeCode);
87+
});
88+
7689
it.each([
7790
String.raw`$x_i^2+\alpha$`,
7891
String.raw`\(\frac{a}{b}\)`,

0 commit comments

Comments
 (0)