Skip to content

Commit 3f86b2c

Browse files
fix(shared): typing after a skill chip no longer reparses the prompt as math
AI-assisted; verified with the tests and tools listed above.
1 parent 62dc589 commit 3f86b2c

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

‎packages/shared/src/markdownMath.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,32 @@ export function markdownMathRanges(source: string, tree?: Root) {
5353
/** Messages without these characters skip math parsing entirely. */
5454
function mayContainMath(text: string): boolean {
5555
return (
56-
text.includes("$") ||
56+
mayContainDollarMath(text) ||
5757
text.includes("\\(") ||
5858
text.includes("\\[") ||
5959
/(?:`{3,}|~{3,})[ \t]*math\b/.test(text)
6060
);
6161
}
6262

63+
/**
64+
* A `$` formula needs a closing `$` (or the second `$` of `$$`) after another `$` on its line and
65+
* not right after a space (micromark reads a tab as virtual spaces, which still close). So `$skill`
66+
* chips and prices skip the parser.
67+
*/
68+
function mayContainDollarMath(text: string): boolean {
69+
let opened = false;
70+
for (let index = 0; index < text.length; index += 1) {
71+
const char = text[index];
72+
if (char === "\n" || char === "\r") {
73+
opened = false;
74+
} else if (char === "$") {
75+
if (opened && text[index - 1] !== " ") return true;
76+
opened = true;
77+
}
78+
}
79+
return false;
80+
}
81+
6382
export function mathKind(className: string | undefined): "inline" | "display" | null {
6483
if (!className?.includes("language-math")) return null;
6584
if (className.includes("math-display")) return "display";

0 commit comments

Comments
 (0)