Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

slides: an escaped \$ never opens or closes a formula (#647)

nyblnete0993393af4c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 091791e8..236e4462 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,12 @@ pre-1.0.
## [Unreleased]
+- **Dollar amounts next to escaped dollars stay text.** In a line like
+ "$10, and \$x\$", the first dollar could pair with an escaped one and the
+ words between were treated as a formula: the editor flagged them as a
+ formula that failed to render. An escaped `\$` now never opens or closes a
+ formula, inline or display, while an escaped dollar inside a formula still
+ belongs to it.
- **Turn text into code without leaving the text box.** Type a fenced block
— a line of three backticks (optionally with a language, like ```` ```js ````
or ```` ```python ````), your code, and a closing line of three backticks —
diff --git a/scripts/test-slides-math-delims.ts b/scripts/test-slides-math-delims.ts
index 7db69973..17e4b2d8 100644
--- a/scripts/test-slides-math-delims.ts
+++ b/scripts/test-slides-math-delims.ts
@@ -118,5 +118,26 @@ ok(markdownToHtml('see \\(\\frac{a}{b}\\) and $a\\_1$') === 'see \\(\\frac{a}{b}
ok(markdownToHtml('$a < b$ **bold**') === '$a &lt; b$ <b>bold</b>', 'a parked formula is still HTML-escaped, and markdown outside it still works')
ok(resolve(markdownToHtml('\\[\\sum_i x_i\\]')) === display('\\sum_i x_i'), 'pasted, then rendered: the whole round trip')
+// --- an escaped \$ never opens or closes a formula ---------------------------
+// Prices beside escaped dollars: `$10, and \$x\$` used to pair the first `$`
+// with the ESCAPED one and treat `10, and \` as a formula (convert hit it in a
+// pptx report). The real engine happened to refuse that source, so the prose
+// survived by luck; a stand-in renderer that accepts ANY source shows the
+// pairing decision itself, which is what must be right.
+console.log('\nescaped dollars and prices\n')
+{
+ const stub = (src: string) => `<math>${src}</math>`
+ const any = (html: string) => resolveMathHtml(html, stub)
+ ok(any('$10, and \\$x\\$') === '$10, and $x$', 'a price, then escaped dollars: no formula (the escaped $ cannot close one)')
+ ok(any('pay $3 or \\$4 today') === 'pay $3 or $4 today', 'a price and an escaped price: both dollars')
+ ok(any('\\$5 to \\$10') === '$5 to $10', 'two escaped prices: dollars, no formula between them')
+ ok(any('it costs $5 and $10') === 'it costs $5 and $10', 'two plain prices: still no formula (digit after the closer)')
+ ok(any('$x$ costs \\$5') === '<math>x</math> costs $5', 'a formula, then an escaped price: the formula renders, the price stays')
+ ok(any('$a \\$ b$') === '<math>a \\$ b</math>', 'an escaped $ INSIDE a formula is part of its body')
+ ok(any('$$a\\$$ then $$b$$') === '$$a$$ then <math>b</math>', 'display: an escaped $ cannot close $$…$$ either')
+ ok(any('$$\\$5 + x$$') === '<math>\\$5 + x</math>', 'display: an escaped $ inside the body is kept')
+ ok(hinted('$10, and \\$x\\$') === '$10, and $x$', 'the editor\'s hint marks nothing: there is no formula to have failed')
+}
+
console.log(`\n${checks - failures}/${checks} checks passed`)
process.exit(failures ? 1 : 0)
diff --git a/scripts/test-slides-math-href.ts b/scripts/test-slides-math-href.ts
index 2659ed70..c7838f52 100644
--- a/scripts/test-slides-math-href.ts
+++ b/scripts/test-slides-math-href.ts
@@ -42,7 +42,10 @@ console.log('the source\n')
ok(/export function resolveMath\(html: string, hint\?[^)]*\)[^{]*\{\s*return resolveMathHtml\(html, renderMath,/.test(render), 'render.ts resolveMath hands the html to resolveMathHtml')
ok(/const parts = html\.split\(\/\(<\[\^>\]\*>\)\/\)/.test(delims) && /parts\.map\(\(p, i\) => \(i % 2 \? p : resolveRun\(/.test(delims), 'resolveMathHtml splits on tags and hands only the text runs to resolveRun')
ok(!/html\.replace\(/.test(delims), 'no rule runs over the whole HTML')
-ok(delims.includes("/(^|[^\\\\$])\\$(\\S(?:[^$\\n]*?\\S)?)\\$(?!\\d)/g"), 'the fussy inline rule is unchanged (no whitespace inside, no digit after)')
+// the body is read as tokens since an escaped \$ stopped closing a formula
+// (test-slides-math-delims.ts runs that); the fussiness is the same
+ok(delims.includes("/(^|[^\\\\$])\\$((?:[^$\\\\\\s]|\\\\[^\\n])(?:(?:[^$\\\\\\n]|\\\\[^\\n])*?(?:[^$\\\\\\s]|\\\\[^\\n]))?)\\$(?!\\d)/g"),
+ 'the fussy inline rule is unchanged (no whitespace at the edges, no digit after; an escaped \\$ never closes)')
console.log('\nthe behaviour, with the real split and a stand-in renderer\n')
// the stand-in: any source becomes a <math> stub — what matters is WHERE the
diff --git a/slides/src/maths/delimiters.ts b/slides/src/maths/delimiters.ts
index e8958588..9d20812e 100644
--- a/slides/src/maths/delimiters.ts
+++ b/slides/src/maths/delimiters.ts
@@ -144,15 +144,20 @@ function resolveRun(text: string, render: RenderFn, hold: (s: string) => string,
const raw = m.slice(pre.length)
return pre + (hint && (sure || MATHY.test(src)) ? mark(raw, hint(src, display)) : raw)
}
- let out = text.replace(/(^|[^\\])\$\$([^$]+?)\$\$/g, rule(true, true))
+ // An escaped \$ is a dollar sign, never a delimiter — not even a CLOSING one:
+ // formula bodies are read as tokens (any character but `$` and `\`, or a
+ // backslash pair), so `\$` is part of a body and only an unescaped `$` can
+ // close. Reading the body as "anything but `$`" let `$10, and \$x\$` close on
+ // the escaped dollar and treat `10, and \` as a formula.
+ let out = text.replace(/(^|[^\\])\$\$((?:[^$\\]|\\[\s\S])+?)\$\$/g, rule(true, true))
// lookbehind, not a consumed prefix, so \(a\)\(b\) back to back both match
out = out.replace(/(?<!\\)()\\\[([\s\S]+?)\\\]/g, rule(true, true))
out = out.replace(/(?<!\\)()\\\(([\s\S]+?)\\\)/g, rule(false, true))
- out = out.replace(/(^|[^\\$])\$(\S(?:[^$\n]*?\S)?)\$(?!\d)/g, rule(false, false))
+ out = out.replace(/(^|[^\\$])\$((?:[^$\\\s]|\\[^\n])(?:(?:[^$\\\n]|\\[^\n])*?(?:[^$\\\s]|\\[^\n]))?)\$(?!\d)/g, rule(false, false))
// `$ x^2 $`: prose by design. What is left paired after the rules above
// did not render; the maths-shaped ones with a space inside get a hint
if (hint) {
- out = out.replace(/(^|[^\\$])\$([^$\n]+?)\$(?!\d)/g, (m, pre: string, src: string) =>
+ out = out.replace(/(^|[^\\$])\$((?:[^$\\\n]|\\[^\n])+?)\$(?!\d)/g, (m, pre: string, src: string) =>
/^\s|\s$/.test(src) && MATHY.test(src) ? pre + mark(m.slice(pre.length), hint(src, false, true)) : m)
}
return unescapeDelims(out)