Skip to content

Quote rendered math as raw TeX in selection copy and Add to chat - #5619

Merged
ymichael merged 2 commits into
get-bb:mainfrom
phyjswang:fix/katex-selection-raw-tex
Oct 16, 2026
Merged

ymichael merged 2 commits into
get-bb:mainfrom
phyjswang:fix/katex-selection-raw-tex

Conversation

@phyjswang

Copy link
Copy Markdown
Contributor

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$$ produced E=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 used Selection.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: selectionClipboard expands the range first, and nodeMarkdown emits math before text escaping, so \, _, and * in TeX stay raw.
  • SecondaryPanelSelectionActions gains an opt-in markdown prop that serializes the selection with selectionClipboard; MarkdownFilePreview sets it. CSV previews keep plain text.
  • Timeline Copy and Add to chat get the math fix through the shared serializer.

No wire, CLI, or plugin API changes.

How you verified

  • New SecondaryPanelSelectionActions.test.tsx drives 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/app passes.
  • vitest run --config vitest.config.ts in apps/app: 626 files, 5918 tests pass. (turbo run test stops before Vitest on this macOS machine because generate-pwa-icons.mjs --check reports the PNGs out of date on an unmodified checkout; unrelated.)

🤖 Generated with Claude Code

AGENT GENERATED

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>
@github-actions

Copy link
Copy Markdown
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.

@github-actions github-actions Bot closed this Oct 16, 2026
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>
@ymichael

Copy link
Copy Markdown
Collaborator

/approve @phyjswang

@github-actions

Copy link
Copy Markdown
Contributor

Approved @phyjswang for PRs. The name now stays on the allow list. The author can open a PR now.

@ymichael ymichael reopened this Oct 16, 2026
@ymichael
ymichael merged commit 1e6af3d into get-bb:main Oct 16, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants