Repository navigation
Quote rendered math as raw TeX in selection copy and Add to chat - #5619
Merged
Merged
Conversation
KaTeX renders each formula twice, a visually hidden MathML copy and the visible HTML, so serializing a selection's text nodes produced output like "E=mc2E = mc^2E=mc2". A selection ending inside a formula also yielded a fragment of its layout. The Markdown selection serializer now expands the range to whole formulas and replaces each one with the TeX KaTeX stores in its annotation: `$$tex$$` inline, a `$$` block for display math. Markdown file previews use the same serializer for Add to chat instead of Selection.toString(), so they also keep emphasis, links, and lists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
Thanks @phyjswang for the PR. This repo approves new contributors before it reviews their PRs, so this PR now closes on its own. That is expected. Join the #contributors channel in Discord (https://discord.gg/kvBU6tJhcJ), post a link to this PR, and explain what it changes and why. A maintainer then adds you to the allow list and can reopen the PR. |
The HTML clipboard flavor kept both KaTeX copies with their classes stripped, so rich paste showed each formula twice. Replace each formula with its MathML, without the TeX annotation, so rich editors render it once. Markdown file previews now serialize Cmd+C with the same Markdown serializer as Add to chat instead of leaving it to the browser. The math helpers move into markdown-clipboard.ts, and the timeline gets a math copy test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
|
/approve @phyjswang |
Contributor
|
Approved @phyjswang for PRs. The name now stays on the allow list. The author can open a PR now. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Human comments
What was wrong
KaTeX renders every formula twice: a visually hidden MathML copy (
.katex-mathml, which also carries the TeX source in<annotation encoding="application/x-tex">) and the visible HTML (.katex-html). The Markdown selection serializer from #5613 walks every text node, so selecting$$E = mc^2$$producedE=mc2E = mc^2E=mc2, and a selection ending inside a formula yielded a fragment of its layout. Markdown file previews were worse off: their Add to chat usedSelection.toString(), which also drops all Markdown.What changed
apps/app/src/lib/markdown-clipboard-math.ts(new) expands a selection range to whole formulas and serializes each formula from its TeX annotation:$$tex$$inline, a$$block for display math, and the raw source for.katex-error. Inline math uses$$because bb disables single-dollar math (Disable single-dollar inline LaTeX so chat prices stay literal #512), so the quote renders back as the same formula.markdown-clipboard.ts:selectionClipboardexpands the range first, andnodeMarkdownemits math before text escaping, so\,_, and*in TeX stay raw.SecondaryPanelSelectionActionsgains an opt-inmarkdownprop that serializes the selection withselectionClipboard;MarkdownFilePreviewsets it. CSV previews keep plain text.No wire, CLI, or plugin API changes.
How you verified
SecondaryPanelSelectionActions.test.tsxdrives Add to chat in a Markdown file preview with real KaTeX: the whole document (round-trips the source exactly), part of an inline formula, and prose into a display formula. All three fail without the fix (Energy E=mc2E = mc^2E=mc2 is big.) and pass with it.pnpm exec turbo run typecheck lint --filter=@bb/apppasses.vitest run --config vitest.config.tsinapps/app: 626 files, 5918 tests pass. (turbo run teststops before Vitest on this macOS machine becausegenerate-pwa-icons.mjs --checkreports the PNGs out of date on an unmodified checkout; unrelated.)🤖 Generated with Claude Code