Skip to content

Commit 2fc4d5b

Browse files
committed
fix(markdown): formulas ending in a comma render
The dollar tokenizer counted a comma before the closing $ as spacing, so $x,$ and $f(x),$ stayed literal while $x.$ and $x;$ rendered, and the unclosed dollars could pair with later ones into a wrong formula. Native mobile already renders these. A comma now only counts as spacing to match an opening space, so $$ x,$$ keeps rendering.
1 parent 448b8a9 commit 2fc4d5b

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎packages/shared/src/markdownMath.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ describe("chat math parsing", () => {
143143
}
144144
});
145145

146+
it("reads formulas that end in a comma, as with other punctuation", () => {
147+
expect(formulas("Where $x = 1,$ $f(x),$ and $y = 2.$ hold, with $$a,$$ and $$ b,$$.")).toEqual([
148+
["inline", "x = 1,"],
149+
["inline", "f(x),"],
150+
["inline", "y = 2."],
151+
["display", "a,"],
152+
["display", " b,"],
153+
]);
154+
expect(markdownMathRanges("| Value |\n| --- |\n| $x,$ |")).toEqual([
155+
expect.objectContaining({ source: "$x,$", tex: "x,", inTable: true }),
156+
]);
157+
for (const prose of ["Pay $5,$10 now.", "Prices $1,000,$2,000 rose."]) {
158+
expect(formulas(prose), prose).toEqual([]);
159+
}
160+
});
161+
146162
it("leaves code, escapes, links, and citations alone", () => {
147163
for (const [markdown, rendered] of [
148164
["Use `$x$` in code.", "Use in code."],

‎patches/[email protected]‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ diff --git a/dev/lib/math-text.js b/dev/lib/math-text.js
299299
if (
300300
code === codes.eof ||
301301
code === codes.space ||
302-
@@ -179,9 +207,26 @@
302+
@@ -179,9 +207,28 @@
303303

304304
// Done!
305305
if (size === sizeOpen) {
@@ -318,7 +318,9 @@ diff --git a/dev/lib/math-text.js b/dev/lib/math-text.js
318318
+ const closeWhitespace =
319319
+ charBeforeClose === codes.space ||
320320
+ charBeforeClose === codes.horizontalTab ||
321-
+ charBeforeClose === codes.comma
321+
+ // A formula can end in a comma, as it can in `.` or `;`. The comma
322+
+ // still matches an opening space, as in `$$ x,$$`.
323+
+ (charBeforeClose === codes.comma && openWhitespace)
322324
+ if (openWhitespace === closeWhitespace) {
323325
+ effects.exit('mathTextSequence')
324326
+ effects.exit('mathText')

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)