-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix(cli): align inline math recognition #7701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wenshao
merged 3 commits into
QwenLM:main
from
CubeLander:fix/cli-inline-math-recognition
Jul 25, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Qwen Team | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| findInlineMathExpressions, | ||
| INLINE_MATH_MAX_CHARS, | ||
| readInlineMathSpanAt, | ||
| } from './inline-math.js'; | ||
|
|
||
| describe('inline math recognition', () => { | ||
| it('recognizes single-character and CJK-adjacent formulas', () => { | ||
| expect(findInlineMathExpressions('Values $x$、$α$。')).toEqual(['x', 'α']); | ||
| }); | ||
|
|
||
| it('preserves escaped dollars, prices, variables, and adjacent spans', () => { | ||
| expect( | ||
| findInlineMathExpressions(String.raw`Literal \$xy$ and \$\alpha$`), | ||
| ).toEqual([]); | ||
| expect(findInlineMathExpressions('Price $20 and $30')).toEqual([]); | ||
| expect(findInlineMathExpressions('Use $HOME and ${PATH}')).toEqual([]); | ||
| expect(findInlineMathExpressions('$a$$b$')).toEqual([]); | ||
| }); | ||
|
|
||
| it('rejects formulas whose closing dollar is escaped', () => { | ||
| expect(findInlineMathExpressions(String.raw`A $x\$ B`)).toEqual([]); | ||
| expect(findInlineMathExpressions(String.raw`Total $a b\$ end`)).toEqual([]); | ||
| }); | ||
|
|
||
| it('ignores inline code spans and unclosed formulas', () => { | ||
| expect(findInlineMathExpressions('Use `$xy$` then $z$ and $open')).toEqual([ | ||
| 'z', | ||
| ]); | ||
| expect(findInlineMathExpressions('Use ``a `$x$` b`` then $y$')).toEqual([ | ||
| 'y', | ||
| ]); | ||
| expect(findInlineMathExpressions('Use `a `` $x$ `` b` then $y$')).toEqual([ | ||
| 'y', | ||
| ]); | ||
| }); | ||
|
|
||
| it('bounds formula length', () => { | ||
| const maximum = 'x'.repeat(INLINE_MATH_MAX_CHARS); | ||
| const tooLong = 'x'.repeat(INLINE_MATH_MAX_CHARS + 1); | ||
|
|
||
| expect(findInlineMathExpressions(`$${maximum}$`)).toHaveLength(1); | ||
| expect(findInlineMathExpressions(`$${tooLong}$`)).toEqual([]); | ||
| }); | ||
|
|
||
| it('reads a span only at the requested offset', () => { | ||
| expect(readInlineMathSpanAt('A $x$ B', 2)).toBe('$x$'); | ||
| expect(readInlineMathSpanAt(String.raw`A \$x$ B`, 3)).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Qwen Team | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| export const INLINE_MATH_MAX_CHARS = 1024; | ||
|
|
||
| export const INLINE_MATH_PATTERN_SOURCE = String.raw`(?<![\\\w$])\$(?![\s\d$])[^$\n]{0,${INLINE_MATH_MAX_CHARS - 1}}[^$\s](?<!\\)\$(?![\w$])`; | ||
|
|
||
| export const INLINE_CODE_SPAN_PATTERN_SOURCE = String.raw`(?<!\`)(?<inlineCodeFence>\`+)(?!\`).+?(?<!\`)\k<inlineCodeFence>(?!\`)`; | ||
|
|
||
| const INLINE_CODE_SPAN_RE = new RegExp(INLINE_CODE_SPAN_PATTERN_SOURCE, 'g'); | ||
|
|
||
| export function findInlineMathExpressions(text: string): string[] { | ||
| const codeRanges = [...text.matchAll(INLINE_CODE_SPAN_RE)].map((match) => ({ | ||
| start: match.index, | ||
| end: match.index + match[0].length, | ||
| })); | ||
| const mathRegex = new RegExp(INLINE_MATH_PATTERN_SOURCE, 'g'); | ||
| const expressions: string[] = []; | ||
|
|
||
| for (const match of text.matchAll(mathRegex)) { | ||
| const start = match.index; | ||
| const raw = match[0]; | ||
| const end = start + raw.length; | ||
| if (codeRanges.some((range) => start >= range.start && end <= range.end)) { | ||
| continue; | ||
| } | ||
| expressions.push(raw.slice(1, -1)); | ||
| } | ||
|
|
||
| return expressions; | ||
| } | ||
|
|
||
| const INLINE_MATH_AT_RE = new RegExp(INLINE_MATH_PATTERN_SOURCE, 'y'); | ||
|
|
||
| export function readInlineMathSpanAt( | ||
| text: string, | ||
| index: number, | ||
| ): string | null { | ||
| INLINE_MATH_AT_RE.lastIndex = index; | ||
| return INLINE_MATH_AT_RE.exec(text)?.[0] ?? null; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Suggestion] This scenario adds valuable inline-math content (items 4–5, 7), but no CI workflow or
npm testcommand collects theterminal-capturescenarios — the only entry point is the manualcapture:markdown-renderingscript. If the rendering or/copy latexintegration for math regresses, this scenario silently produces stale screenshots without any automated gate catching it.Concrete cost: the three bugs this PR fixes (single-char
$x$, escaped\$xy$, code-span`$xy$`) now have new scenario assertions that will never run in CI.Consider either adding a CI step that runs
capture:markdown-rendering(or the whole terminal-capture suite), or adding an assertion-based integration test for the math rendering +/copy latexflow in a vitest-collected file.— qwen3.7-max via Qwen Code /review