Skip to content

fix: preserve currency formatting alongside inline math - #976

Merged
agegr merged 2 commits into
agegr:mainfrom
gindinconsulting:codex/currency-formatting
Sep 30, 2026
Merged

agegr merged 2 commits into
agegr:mainfrom
gindinconsulting:codex/currency-formatting

Conversation

@gindinconsulting

Copy link
Copy Markdown
Contributor

Dollar amounts such as $20/mo and $6 currently become the opening and closing delimiters of one inline-math token. That can consume a bold heading's closing markers and render the following prose as math. This change keeps those prices and Markdown formatting readable while retaining single-dollar equations.

Closes #975.
Related: #135, which proposed disabling single-dollar math entirely.

Changes

  • Wrap the existing remark-math tokenizer and reject numeric single-dollar candidates when the closing dollar precedes another digit or the content has mismatched whitespace padding. Rejecting the token lets ordinary Markdown parsing recover emphasis and links.
  • Apply the same rule to chat, file previews, the minimap, and the browser demo's renderer.
  • Add seven rendering regressions covering a price in a bold heading followed by another price, adjacent amounts/ranges/decimals, mixed currency and formulas, supported math, previews, escaped dollars/code/links, and HTML/URL sanitization.

Example

**3. Membership — $20/mo (+tax)**
One free hour monthly. Waives the $6 activation fee.

The heading now stays bold, the prices remain literal, and the description retains its spaces. $x$, $2 + 2 = 4$, $20$, balanced padding, multiline math, and $$...$$ remain supported.

Validation

  • npm run lint — passed.
  • npx tsc --noEmit — passed.
  • npm test — 1,314 tests passed, including the seven added regressions.
  • npm run build — passed.
  • E2E_SERVER_MODE=start npm run test:e2e — passed at desktop and mobile viewport sizes.
  • Demo: npm run lint and npm run build — passed.
  • git diff --check — passed.

Run locally on macOS arm64 / Node 26.10.0 with isolated Pi test data. Browser checks used Chromium; physical iPhone testing was not part of this validation.

Scope and tradeoff

This is a rendering-only change with no dependency, authentication, configuration, or stored-message changes. The existing tokenizer/resolver and HTML/URL sanitization remain in use.

Dollar-delimited prose and math are inherently ambiguous. Numeric formulas with one-sided whitespace padding (such as $2 + 2 $) or a digit immediately after the closing dollar (such as $2$3) are treated as literal text by this heuristic; balanced delimiters or $$...$$ avoid that ambiguity. Non-numeric single-dollar formulas retain the previous behavior. The wrapper depends on the upstream mathText tokenizer shape, which the rendering regressions exercise.

@agegr

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Reviewed. #975 reproduces on main: with the current lib/markdown.ts, five of the new MarkdownBody tests fail, and $20/mo … $6 becomes one inline-math span.

What I checked:

  • No shared state is patched. mathText(options) returns a fresh construct each time remarkMath runs, so wrapping its tokenize stays local to that processor and doesn't stack across renders.
  • Real math is unchanged. I compared 20 inputs between main and this branch. $n$, $O(n^2)$, $1/2$, $0.5$, $-1$, $ 5 $, $1$ and $2$, $3 \times 4 = 12$ and 设 $1 \le k \le n$,则 render the same. Only price-shaped pairs change: $5 and $x$, from $5-$10, $1,000 or $2,000, 价格是 $20,折后 $15。 and Price $3.50 each, $x^2$ here now keep the prices literal and still render the real formula.
  • Tradeoff. $1$2 and one-sided padding like $2 \cdot 3 $ become literal, as the description says. That's an acceptable price for the fix.
  • Demo. The demo/lib/markdown.ts copy is identical, and unified / micromark-util-types are already in the demo lockfile for the type imports.

Merged locally with current main (clean): tsc --noEmit, eslint, and the full unit suite pass (1359/1360; the one failure, worktree.test.mjs force removal, also fails on main in my macOS temp dir). LGTM 👍

Heads-up: #972 also edits both remarkPlugins arrays in lib/markdown.ts, so whichever lands second needs a one-line conflict resolution.


Generated by Claude Code

@agegr

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

#972 landed, so I merged main and resolved the lib/markdown.ts conflict. Both plugins are kept, in this order: remarkGfm → remarkSplitAutolinkLiterals → remarkCurrencySafeMath. They don't interact. A line mixing both, 价格 $20,见 https://a.com/p,然后 $x^2$ 和 $6, keeps the prices literal, stops the link at ,, and renders $x^2$.

tsc --noEmit, eslint, and the full unit suite still pass (same pre-existing macOS-only worktree.test.mjs failure as main).


Generated by Claude Code

@agegr
agegr merged commit 6a97d0b into agegr:main Sep 30, 2026
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
…gr#976 agegr#974 agegr#989 agegr#990 agegr#991)

这 8 个提交与我们的分叉没有文本冲突,直接并入:
- agegr#1011 回合答案出现后折叠过程详情
- agegr#1012 移动端打开模型选择器不弹键盘
- agegr#972/agegr#976 行内链接在 CJK 标点处停止、行内公式与货币格式
- agegr#974 首条消息前即报告 system prompt
- agegr#989/agegr#990/agegr#991 子代理:收集标记按 run 计、重启后的中断报告、续跑背景报告标注
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dollar prices are parsed as inline math and break Markdown formatting

2 participants