fix(markdown): avoid parsing currency as LaTeX - #1997
Conversation
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
📝 WalkthroughWalkthroughLaTeX 插件拆分美元公式与括号定界匹配,并增加单美元定界合法性校验。测试覆盖货币文本、转义美元符号、数字开头公式、空格规则、尾随数字规则及双美元公式。 ChangesLaTeX 解析规则
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Bundle ReportChanges will decrease total bundle size by 47 bytes (-0.0%) ⬇️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: antdx-array-pushAssets Changed:
|
|
This comment is automatically generated by the x-markdown performance CI. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1997 +/- ##
=======================================
Coverage 97.06% 97.07%
=======================================
Files 159 159
Lines 5767 5775 +8
Branches 1700 1704 +4
=======================================
+ Hits 5598 5606 +8
Misses 167 167
Partials 2 2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Deploying ant-design-x with
|
| Latest commit: |
d79a244
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://657bcaeb.ant-design-x.pages.dev |
| Branch Preview URL: | https://codex-fix-x-markdown-latex-c.ant-design-x.pages.dev |
size-limit report 📦
|
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
packages/x-markdown/src/plugins/Latex/__tests__/index.test.tsxpackages/x-markdown/src/plugins/Latex/index.ts
…imits Pin down what the Pandoc rules do and do not change: - `$ x $` (padded on both sides) no longer renders as math — this is the behavior change that makes currency detection possible, so assert it rather than leaving it implied by `$ x$` / `$x $`. - `$$ ... $$` is exempt from the spacing rules and still renders padded content, so the change is scoped to single-dollar math. - Currency that happens to satisfy the rules (`价格 $5和$X`) is still parsed as math; record it as a known limitation instead of a silent gap. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
This comment is automatically generated by the x-markdown performance CI. |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/x-markdown/src/plugins/Latex/__tests__/index.test.tsx (1)
25-33: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win确保断言失败时也恢复
console.warn。Line 33 的
warnSpy.mockRestore()位于所有断言之后。如果render或任一断言抛出异常,Line 33 不会执行。后续测试可能继续使用被替换的console.warn,从而隐藏警告并造成测试顺序依赖。请使用try/finally,或在afterEach中统一调用jest.restoreAllMocks()。建议的局部修复
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); - const { container } = render( - <XMarkdown config={{ extensions: latexPlugin() }}>{`**${content}**`}</XMarkdown>, - ); - - expect(container.querySelector('.katex')).not.toBeInTheDocument(); - expect(container.querySelector('strong')).toHaveTextContent(content); - expect(warnSpy.mock.calls.flat().join(' ')).not.toContain('unicodeTextInMathMode'); - warnSpy.mockRestore(); + try { + const { container } = render( + <XMarkdown config={{ extensions: latexPlugin() }}>{`**${content}**`}</XMarkdown>, + ); + + expect(container.querySelector('.katex')).not.toBeInTheDocument(); + expect(container.querySelector('strong')).toHaveTextContent(content); + expect(warnSpy.mock.calls.flat().join(' ')).not.toContain('unicodeTextInMathMode'); + } finally { + warnSpy.mockRestore(); + }🤖 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/__tests__/index.test.tsx` around lines 25 - 33, 在 Latex 测试中围绕 console.warn 的 spy 和相关 render/assert 调用加入可靠的清理机制,确保 render 或任一断言失败时仍会执行 warnSpy.mockRestore();可使用 try/finally,或通过 afterEach 统一调用 jest.restoreAllMocks()。
🧹 Nitpick comments (2)
packages/x-markdown/src/plugins/Latex/__tests__/index.test.tsx (2)
44-58: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win补充不等长美元分隔符的负向用例。
当前参数表覆盖了空格规则和尾随数字规则,但没有验证
$$x$和$x$$被拒绝。请加入这两个输入,并继续断言不生成.katex且保留原始文本。建议增加的用例
'$ x $', '$x$2', + '$$x$', + '$x$$',🤖 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/__tests__/index.test.tsx` around lines 44 - 58, 在单美元分隔符参数化测试中补充 `$$x$` 和 `$x$$` 两个输入,继续通过现有断言验证不生成 `.katex` 且保留原始文本。
60-71: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win补充美元与非标准定界符交错的回归用例。
当前用例只覆盖美元语法。请增加
\[a\] and $b$与$a$ and \[b\]等输入,并断言生成两个.katex节点且顺序正确。这样可以验证美元匹配分支不会跳过或重排\(...\)、\[...\]分支。建议增加的测试
+ it.each([ + '\\[a\\] and $b$', + '$a$ and \\[b\\]', + ])('should preserve mixed delimiter order: %s', (content) => { + const { container } = render( + <XMarkdown config={{ extensions: latexPlugin() }}>{content}</XMarkdown>, + ); + + expect(container.querySelectorAll('.katex')).toHaveLength(2); + });🤖 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/__tests__/index.test.tsx` around lines 60 - 71, 在现有 Latex spacing 回归测试中补充美元语法与非标准定界符交错的输入,至少覆盖 `\[a\] and $b$` 与 `$a$ and \[b\]`;对每个输入断言生成两个按源文本顺序排列的 `.katex` 节点,确保美元匹配分支不跳过或重排其他定界符。
🤖 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.
Outside diff comments:
In `@packages/x-markdown/src/plugins/Latex/__tests__/index.test.tsx`:
- Around line 25-33: 在 Latex 测试中围绕 console.warn 的 spy 和相关 render/assert
调用加入可靠的清理机制,确保 render 或任一断言失败时仍会执行 warnSpy.mockRestore();可使用 try/finally,或通过
afterEach 统一调用 jest.restoreAllMocks()。
---
Nitpick comments:
In `@packages/x-markdown/src/plugins/Latex/__tests__/index.test.tsx`:
- Around line 44-58: 在单美元分隔符参数化测试中补充 `$$x$` 和 `$x$$` 两个输入,继续通过现有断言验证不生成 `.katex`
且保留原始文本。
- Around line 60-71: 在现有 Latex spacing 回归测试中补充美元语法与非标准定界符交错的输入,至少覆盖 `\[a\] and
$b$` 与 `$a$ and \[b\]`;对每个输入断言生成两个按源文本顺序排列的 `.katex` 节点,确保美元匹配分支不跳过或重排其他定界符。
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 058fbec4-2553-4135-830a-f83597612c50
📒 Files selected for processing (1)
packages/x-markdown/src/plugins/Latex/__tests__/index.test.tsx
`[^$]` treated the `$` in `\$` as a closing delimiter, truncating
formulas like `$\text{\$100}$`. Allow backslash-escaped characters in
the inline dollar rule so escaped dollars stay inside the formula.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/x-markdown/src/plugins/Latex/__tests__/index.test.tsx (1)
25-33: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win请确保断言失败时也恢复
console.warnspy。如果 Line 26-28 的
render或 Line 30-32 的任一断言失败,Line 33 不会执行。warnSpy会继续 mock 后续测试的console.warn,导致后续 warning 检查失真。请使用try/finally,或在afterEach中统一恢复 mock。建议修复
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); - const { container } = render( - <XMarkdown config={{ extensions: latexPlugin() }}>{`**${content}**`}</XMarkdown>, - ); - - expect(container.querySelector('.katex')).not.toBeInTheDocument(); - expect(container.querySelector('strong')).toHaveTextContent(content); - expect(warnSpy.mock.calls.flat().join(' ')).not.toContain('unicodeTextInMathMode'); - warnSpy.mockRestore(); + try { + const { container } = render( + <XMarkdown config={{ extensions: latexPlugin() }}>{`**${content}**`}</XMarkdown>, + ); + + expect(container.querySelector('.katex')).not.toBeInTheDocument(); + expect(container.querySelector('strong')).toHaveTextContent(content); + expect(warnSpy.mock.calls.flat().join(' ')).not.toContain('unicodeTextInMathMode'); + } finally { + warnSpy.mockRestore(); + }🤖 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/__tests__/index.test.tsx` around lines 25 - 33, Ensure the console.warn spy created in the test is restored even when render or any assertion fails. Update the test setup around warnSpy and the XMarkdown assertions to use try/finally or an afterEach cleanup, preserving the existing assertions and preventing the mock from leaking into later tests.
🤖 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.
Outside diff comments:
In `@packages/x-markdown/src/plugins/Latex/__tests__/index.test.tsx`:
- Around line 25-33: Ensure the console.warn spy created in the test is restored
even when render or any assertion fails. Update the test setup around warnSpy
and the XMarkdown assertions to use try/finally or an afterEach cleanup,
preserving the existing assertions and preventing the mock from leaking into
later tests.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 3cc2a137-4e83-4c26-9b5e-04801d9cb21b
📒 Files selected for processing (2)
packages/x-markdown/src/plugins/Latex/__tests__/index.test.tsxpackages/x-markdown/src/plugins/Latex/index.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/x-markdown/src/plugins/Latex/index.ts
|
This comment is automatically generated by the x-markdown performance CI. |
|
This comment is automatically generated by the x-markdown performance CI. |
Follow Pandoc's single-dollar delimiter rules so amounts like ($100) are no longer detected as inline math: - split the single-dollar rule from the \\(...\\) / \\[...\\] rules - reject dollar math with leading/trailing whitespace or a digit right after the closing delimiter (currency detection) - allow backslash-escaped characters (\\$) inside inline formulas so escaped dollars stay in the formula - keep \$\$...\$\$ exempt from the spacing rules Sync from ant-design/x#1997 (commit 7470e60).
🤔 This is a ...
🔗 Related Issues
No existing issue or pull request matched this bug.
💡 Background and Solution
The built-in
x-markdownLaTeX tokenizer currently treats any text between two$characters as math. Prose containing multiple currency amounts, such asDowngrade Max ($100) to Pro ($20), is therefore rendered as KaTeX. CJK prose also causes repeatedunicodeTextInMathModewarnings because natural-language text is passed into KaTeX math mode.This change:
\(...\)and\[...\]parsing;$/$$delimiter lengths;$;$2x + 1$and all existing double-dollar behavior;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.biome lintremains blocked by two pre-existingnoDangerouslySetInnerHtmlerrors in benchmark-onlyMarkdownRenderer.tsx; neither file is touched by this PR.Reference: Pandoc's
tex_math_dollarsdelimiter rules: https://pandoc.org/demo/example26.html📝 Change Log
$ x $(padded with spaces) and$x$2no longer render as math;$$...$$is unaffected.$ x $(首尾带空格)与$x$2不再渲染为公式;$$...$$不受影响。Summary by CodeRabbit