TypeScript
fix(markdown): formulas ending in a comma render (#22883)
Nikita Maximilian Grimm1d631342b01d
diff --git a/packages/shared/src/markdownMath.test.ts b/packages/shared/src/markdownMath.test.tsindex cb920c07379..0bc6941e494 100644--- a/packages/shared/src/markdownMath.test.ts+++ b/packages/shared/src/markdownMath.test.ts@@ -124,8 +124,18 @@ describe("chat math parsing", () => {"It costs $5 and $10.","Budget $5-$10 per seat, or $20,000-$30,000 a year.","Plans are $5/$10 monthly.",+ "Pay $5,$10 now.","Run $browser then $deploy on it.","Set $HOME before running.",+ "Set $HOME,$PATH before running.",+ "Set $HOME, $PATH before running.",+ "Set $HOME,$_PATH before running.",+ "Set $home,$path before running.",+ "Set $HOME,${PATH} before running.",+ "Set $HOME,$? before running.",+ "Set $HOME,$@ before running.",+ "Set $HOME,$(pwd) before running.",+ "Pay $1,000,$2,000 now.",]) {expect(formulas(prose), prose).toEqual([]);expect(text(prose), prose).toBe(prose);@@ -143,6 +153,17 @@ describe("chat math parsing", () => {}});+ it("reads formulas that end in a comma", () => {+ for (const boundary of ["", " ", "\n", ".", ",", ";", ":", ")", "]", "}"]) {+ expect(formulas(`$x,$${boundary}`)).toEqual([["inline", "x,"]]);+ }+ expect(formulas("Where $x = 1,$ with $$a,$$ and $$ b,$$.")).toEqual([+ ["inline", "x = 1,"],+ ["display", "a,"],+ ["display", " b,"],+ ]);+ });+it("leaves code, escapes, links, and citations alone", () => {for (const [markdown, rendered] of [["Use `$x$` in code.", "Use in code."],diff --git a/patches/[email protected] b/patches/[email protected]index 335ce4c11d5..25da4f5f477 100644--- a/patches/[email protected]+++ b/patches/[email protected]@@ -235,6 +235,9 @@ diff --git a/dev/lib/math-tex-text.js b/dev/lib/math-tex-text.jsdiff --git a/dev/lib/math-text.js b/dev/lib/math-text.js--- a/dev/lib/math-text.js+++ b/dev/lib/math-text.js+@@ -10 +10 @@+-import {markdownLineEnding} from 'micromark-util-character'++import {markdownLineEnding, markdownLineEndingOrSpace} from 'micromark-util-character'@@ -42,6 +42,13 @@let size/** @type {Token} */@@ -299,7 +302,7 @@ diff --git a/dev/lib/math-text.js b/dev/lib/math-text.jsif (code === codes.eof ||code === codes.space ||-@@ -179,9 +207,26 @@+@@ -179,9 +207,29 @@// Done!if (size === sizeOpen) {@@ -314,11 +317,14 @@ diff --git a/dev/lib/math-text.js b/dev/lib/math-text.js+ if (code !== null && code >= codes.digit0 && code <= codes.digit9) {+ return nok(code)+ }++ if (charBeforeClose === codes.comma && !openWhitespace && code !== codes.eof && !markdownLineEndingOrSpace(code) && !/[.,;:)\]}]/.test(String.fromCharCode(code))) {++ return nok(code)++ }+ // Check if closing sequence has matching whitespace pattern+ const closeWhitespace =+ charBeforeClose === codes.space ||+ charBeforeClose === codes.horizontalTab ||-+ charBeforeClose === codes.comma++ (charBeforeClose === codes.comma && openWhitespace)+ if (openWhitespace === closeWhitespace) {+ effects.exit('mathTextSequence')+ effects.exit('mathText')diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 4bff27f291a..196781c388b 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -380,7 +380,7 @@ patchedDependencies:[email protected]: 8bf49067f93742855e15f29559ac75c8b50230b47069060993c9ea01dac11171[email protected]: afe6b2c89e1eaddfd3c90cf2723f12c9a17d513e65beff51653781486013251c[email protected]: 9ad23a530e45d9f83cba1da8654fe16d5af8b59d5c73d5c7a7c91fd25b9be1d8- [email protected]: 49ab4815c18b003f6af998dcc6c258c17a21b50b2cc87745f12d4bd8b6d591f9+ [email protected]: 7beb6ac4ba52166286593a494531aace81547e55fdddecf892824c25bd84d15a[email protected]: f1e680afec85f650699ea756f91f469844ec4c976f4f168492a45c6b3f67d651[email protected]: abe20e1d1c23549297cbbc818e14fe479b41455930d9663135d6b40fe359b626[email protected]: 419f35ee91eac88ced9438058281db5eb09d9c799d8974c1172926068c9448cf@@ -20056,7 +20056,7 @@ snapshots:micromark-util-combine-extensions: 2.0.1micromark-util-types: 2.0.2- [email protected](patch_hash=49ab4815c18b003f6af998dcc6c258c17a21b50b2cc87745f12d4bd8b6d591f9):+ [email protected](patch_hash=7beb6ac4ba52166286593a494531aace81547e55fdddecf892824c25bd84d15a):dependencies:'@types/katex': 0.16.8devlop: 1.1.0@@ -21348,7 +21348,7 @@ snapshots:dependencies:'@types/mdast': 4.0.4mdast-util-math: 3.0.0(patch_hash=afe6b2c89e1eaddfd3c90cf2723f12c9a17d513e65beff51653781486013251c)([email protected])- micromark-extension-math: [email protected](patch_hash=49ab4815c18b003f6af998dcc6c258c17a21b50b2cc87745f12d4bd8b6d591f9)+ micromark-extension-math: [email protected](patch_hash=7beb6ac4ba52166286593a494531aace81547e55fdddecf892824c25bd84d15a)unified: 11.0.5transitivePeerDependencies:- supports-color