修复: https://github.com/Nagi-ovo/gemini-voyager/issues/421
- User
Assistant
The issue: Quote Reply strips LaTeX
$...$syntax from equations, showing raw text instead of preserving the math formatting.Let me find the quote reply implementation.
Assistant
Now I need to understand how Gemini renders math in the DOM to properly extract LaTeX source.
Assistant
Now I understand the issue.
Range.toString()returns rendered text (e.g.,U∈[0,1)) instead of LaTeX source (e.g.,$U \in [0, 1)$). Gemini renders math as DOM elements withdata-mathattributes inside.math-inline/.math-blockcontainers.The fix: replace
currentSelectionRange.toString()with a function that walks the cloned DOM and substitutes math elements with their LaTeX source.Assistant
Now add tests for the LaTeX preservation:
Assistant
Now let's run the tests:
Assistant
The issue is that jsdom doesn't fully support
innerText. Let me add atextContentfallback:Assistant
All 13 tests pass. Now let's run the full verification suite:
Assistant
Typecheck and lint pass (only pre-existing warnings, no errors). Let's run the full test suite and build:
Assistant
All 593 tests pass, build succeeds, typecheck and lint clean.
Here's a summary of the fix:
Problem: Quote Reply used
Range.toString()which returns visually rendered text, stripping LaTeX delimiters. Math like$U \in [0, 1)$was quoted asU∈[0,1).Fix: Added
extractTextWithLatex()insrc/pages/content/quoteReply/index.tsthat:- Clones the selection range contents
- Checks for Gemini math elements (
.math-inline,.math-block,[data-math]) - If none found, falls back to
Range.toString()(zero overhead for non-math selections) - If found, replaces math containers with
$latex$(inline) or$$latex$$(block) text nodes using thedata-mathattribute - Renders the modified DOM in a temporary hidden element to preserve newline handling
Tests added: 3 new tests covering inline math, block math, and standalone
data-mathelements.- User
你修改了啥?
Assistant
改了
src/pages/content/quoteReply/index.ts一个文件:问题根因: Quote Reply 用
Range.toString()取选中文本,这个 API 只返回渲染后的纯文本,会丢掉 LaTeX 语法。比如$U \in [0, 1)$变成U∈[0,1)。改动:
- 新增两个函数
replaceMathWithLatex()和extractTextWithLatex()(约 50 行) handleQuoteClick里把currentSelectionRange.toString()换成extractTextWithLatex(currentSelectionRange)
逻辑:
- clone 选区 DOM,用
querySelector查是否有 Gemini 的数学元素(.math-inline、.math-block、[data-math]) - 没有 → 直接走原来的
Range.toString(),零开销 - 有 → 把数学元素替换成
$data-math$(行内)或$$data-math$$(块级)的文本节点,再取innerText
测试: 加了 3 个用例(行内公式、块级公式、独立
data-math元素),全部通过。原有 10 个测试不受影响。- 新增两个函数
- User
push Fixes 那个 issue
Assistant
Done.
7f79ec2pushed, Fixes #421。