Skip to content

The agent that grows with you

fix(desktop): ship the markdown-preprocess change the currency test asserts

teknium13c7943fbcc62
diff --git a/apps/desktop/src/lib/markdown-preprocess.ts b/apps/desktop/src/lib/markdown-preprocess.ts
index 71da4def762..c54987da1b3 100644
--- a/apps/desktop/src/lib/markdown-preprocess.ts
+++ b/apps/desktop/src/lib/markdown-preprocess.ts
@@ -306,6 +306,33 @@ function isEscapedAt(text: string, index: number): boolean {
return slashCount % 2 === 1
}
+/**
+ * True when the `$` at `index` opens a currency amount rather than math.
+ *
+ * Two shapes, and the second is why this helper exists. The US shape `$5`
+ * hugs its digits, so "followed by a digit" identifies it. Most of the rest
+ * of the world writes a currency PREFIX plus a space — `R$ 12.345` (BRL),
+ * `US$ 1,200`, `AU$ 40`. Treating only the hugging shape as currency left
+ * `R$ 12.345 … R$ 98.765` with two bare dollars on one line, so remark-math
+ * (`singleDollarTextMath: true`) paired them and painted the whole sentence
+ * between two prices as an equation.
+ *
+ * The spaced shape additionally requires a letter immediately before the `$`
+ * — that prefix is what makes it a currency symbol. A bare `$ 5` keeps its
+ * old behavior, so spaced inline math like `$ x^2 $` is untouched.
+ */
+function isCurrencyOpenerAt(text: string, index: number): boolean {
+ if (text[index] !== '$' || isEscapedAt(text, index) || text[index - 1] === '$' || text[index + 1] === '$') {
+ return false
+ }
+
+ if (/\d/u.test(text[index + 1] || '')) {
+ return true
+ }
+
+ return /^[ \u00a0]\d/u.test(text.slice(index + 1, index + 3)) && /^[A-Za-z]$/u.test(text[index - 1] || '')
+}
+
function findClosingSingleDollar(text: string, openingIndex: number): number {
for (let cursor = openingIndex + 1; cursor < text.length && text[cursor] !== '\n'; cursor += 1) {
if (text[cursor] !== '$' || isEscapedAt(text, cursor)) {
@@ -380,12 +407,7 @@ function escapeCurrencyDollarsPreservingMath(text: string): string {
let copiedThrough = 0
for (let cursor = 0; cursor < text.length; cursor += 1) {
- if (
- text[cursor] !== '$' ||
- !/\d/u.test(text[cursor + 1] || '') ||
- text[cursor - 1] === '$' ||
- isEscapedAt(text, cursor)
- ) {
+ if (!isCurrencyOpenerAt(text, cursor)) {
continue
}
@@ -393,6 +415,10 @@ function escapeCurrencyDollarsPreservingMath(text: string): string {
if (
closingIndex !== -1 &&
+ // A second amount on the same line is the NEXT opener, never this
+ // span's closer: `R$ 12.345 … R$ 98.765` is two prices, not one
+ // equation wrapping the prose between them.
+ !isCurrencyOpenerAt(text, closingIndex) &&
!opensCompleteInlineMath(text, closingIndex) &&
isLikelyNumericInlineMath(text.slice(cursor + 1, closingIndex), text[closingIndex + 1] || '')
) {