Skip to content

fix(markdown): avoid parsing currency as LaTeX - #1997

Open
Div627 wants to merge 1 commit into
mainfrom
codex/fix-x-markdown-latex-currency
Open

fix(markdown): avoid parsing currency as LaTeX#1997
Div627 wants to merge 1 commit into
mainfrom
codex/fix-x-markdown-latex-currency

Conversation

@Div627

@Div627 Div627 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

🤔 This is a ...

  • 🐞 Bug fix
  • ✅ Test Case

🔗 Related Issues

No existing issue or pull request matched this bug.

💡 Background and Solution

The built-in x-markdown LaTeX tokenizer currently treats any text between two $ characters as math. Prose containing multiple currency amounts, such as Downgrade Max ($100) to Pro ($20), is therefore rendered as KaTeX. CJK prose also causes repeated unicodeTextInMathMode warnings because natural-language text is passed into KaTeX math mode.

This change:

  • separates dollar-delimited math from \(...\) and \[...\] parsing;
  • requires matching $/$$ delimiter lengths;
  • applies Pandoc-compatible rules to single-dollar math: no whitespace immediately inside the delimiters, and no digit immediately after the closing $;
  • preserves valid numeric-leading formulas such as $2x + 1$ and all existing double-dollar behavior;
  • adds English, CJK, multiple-price, delimiter-boundary, and warning regression coverage.

Validation:

  • npm exec --workspace packages/x-markdown -- jest --config .jest.js --runInBand --no-cache — 7 suites, 338 tests, 28 snapshots passed.
  • npm run tsc --workspace packages/x-markdown — passed.
  • npm exec --workspace packages/x-markdown -- biome check src/plugins/Latex/index.ts src/plugins/Latex/__tests__/index.test.tsx — passed.
  • Full package biome lint remains blocked by two pre-existing noDangerouslySetInnerHtml errors in benchmark-only MarkdownRenderer.tsx; neither file is touched by this PR.

Reference: Pandoc's tex_math_dollars delimiter rules: https://pandoc.org/demo/example26.html

📝 Change Log

Language Changelog
🇺🇸 English Prevent currency amounts in prose from being interpreted as LaTeX formulas.
🇨🇳 Chinese 修复正文中的美元金额被误识别为 LaTeX 公式的问题。

Summary by CodeRabbit

  • Bug 修复
    • 优化行内 LaTeX 公式识别规则,避免将货币金额误渲染为数学公式。
    • 支持正确识别以数字开头的公式,例如 $2x + 1$
    • 过滤空格位置不符合规范或缺少有效边界的单美元分隔符,确保原始文本正常显示。
    • 保留对括号定界公式及块级公式的正常解析。

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

LaTeX 插件将美元公式匹配与括号定界匹配拆分,并增加单美元定界合法性校验;测试覆盖货币文本、数字开头公式及空白或相邻字符导致的无效定界场景。

Changes

LaTeX 解析规则

Layer / File(s) Summary
美元定界匹配与校验
packages/x-markdown/src/plugins/Latex/index.ts
新增独立的美元定界规则和合法性校验,保留 \(...\)\[...\] 的非标准定界处理。
Tokenizer 分支与测试覆盖
packages/x-markdown/src/plugins/Latex/index.ts, packages/x-markdown/src/plugins/Latex/__tests__/index.test.tsx
Tokenizer 分别读取美元和括号匹配结果;测试验证货币金额不渲染、数字开头公式正常渲染,以及无效单美元定界保持原文。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • ant-design/x#1859:同样涉及 LaTeX 插件对 \\[...\\] 及块级/行内 KaTeX 输出的处理。

Poem

兔耳蹦跳看公式,
金额不再误入笼。
$2x+1$ 绽光芒,
空格边界守得牢。
KaTeX 安稳落地上。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了本次修改的核心:避免将货币金额误解析为 LaTeX。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-x-markdown-latex-currency

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Bundle Report

Changes will increase total bundle size by 8 bytes (0.0%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
antdx-array-push 2.11MB 8 bytes (0.0%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: antdx-array-push

Assets Changed:

Asset Name Size Change Total Size Change (%)
antdx.min.js 8 bytes 2.11MB 0.0%

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Performance benchmark report not found.


This comment is automatically generated by the x-markdown performance CI.

@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.07%. Comparing base (2c3146a) to head (46ae60f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1997   +/-   ##
=======================================
  Coverage   97.06%   97.07%           
=======================================
  Files         159      159           
  Lines        5766     5774    +8     
  Branches     1713     1715    +2     
=======================================
+ Hits         5597     5605    +8     
  Misses        167      167           
  Partials        2        2           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying ant-design-x with  Cloudflare Pages  Cloudflare Pages

Latest commit: 46ae60f
Status: ✅  Deploy successful!
Preview URL: https://fdce1869.ant-design-x.pages.dev
Branch Preview URL: https://codex-fix-x-markdown-latex-c.ant-design-x.pages.dev

View logs

@github-actions

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
packages/x/dist/antdx.min.js 510.79 KB
packages/x-sdk/dist/x-sdk.min.js 55.54 KB
packages/x-markdown/dist/x-markdown.min.js 34.4 KB
packages/x-markdown/dist/plugins/latex.min.js 61.96 KB (+50 B 🔺)

@Div627
Div627 marked this pull request as ready for review July 29, 2026 08:32
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@dosubot dosubot Bot added bug Something isn't working javascript Pull requests that update Javascript code labels Jul 29, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/x-markdown/src/plugins/Latex/index.ts`:
- Line 6: Update inlineDollarRule to recognize escaped dollar signs within LaTeX
content, so sequences such as \${} are not treated as closing delimiters;
preserve the existing single- and double-dollar delimiter handling and length
limit, and add a regression test covering $\text{\$100}$.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 06c91106-05ec-4050-af4a-0fc267bfb5b9

📥 Commits

Reviewing files that changed from the base of the PR and between 2c3146a and 46ae60f.

📒 Files selected for processing (2)
  • packages/x-markdown/src/plugins/Latex/__tests__/index.test.tsx
  • packages/x-markdown/src/plugins/Latex/index.ts


const inlineRuleNonStandard =
/^(?:\${1,2}([^$]{1,10000}?)\${1,2}|\\\(([\s\S]{1,10000}?)\\\)|\\\[((?:\\.|[^\\]){1,10000}?)\\\])/;
const inlineDollarRule = /^(\${1,2})([^$]{1,10000}?)\1/;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

支持公式内容中的转义美元符。

[^$] 会把 $\text{\$100}$ 中的 \$ 误当作关闭定界符,导致公式被截断并产生 KaTeX 解析错误。允许反斜杠转义的字符,并补充该回归用例。

建议修改
-const inlineDollarRule = /^(\${1,2})([^$]{1,10000}?)\1/;
+const inlineDollarRule = /^(\${1,2})((?:\\[\s\S]|[^$]){1,10000}?)\1/;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const inlineDollarRule = /^(\${1,2})([^$]{1,10000}?)\1/;
const inlineDollarRule = /^(\${1,2})((?:\\[\s\S]|[^$]){1,10000}?)\1/;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/x-markdown/src/plugins/Latex/index.ts` at line 6, Update
inlineDollarRule to recognize escaped dollar signs within LaTeX content, so
sequences such as \${} are not treated as closing delimiters; preserve the
existing single- and double-dollar delimiter handling and length limit, and add
a regression test covering $\text{\$100}$.

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working javascript Pull requests that update Javascript code lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants