Skip to content

Copy message selections without inherited block Markdown - #5776

Merged
ymichael merged 1 commit into
mainfrom
copy-selection-without-block-markdown
Oct 19, 2026
Merged

ymichael merged 1 commit into
mainfrom
copy-selection-without-block-markdown

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

selectionClipboard (apps/app/src/lib/markdown-clipboard.ts) cloned the selected range and then rebuilt every ancestor up to the message root before serializing. Any partial selection therefore inherited the Markdown of its enclosing blocks: selecting a word in a heading, list item, quote or table cell copied ## word, - word, > word or | word |\n| --- |, in both text/plain and text/html. Links were always serialized as [text](href) with Markdown escaping, so copying a bare URL produced [https://example.com/a\_b](https://example.com/a_b), which is useless when pasted into a browser address bar. A URL alone in a list item copied as 2. [url](url).

What changed

  • markdown-clipboard.ts:
    • Inline ancestors (emphasis, links, code, pre) around a selection are still kept.
    • Single block: a selection inside one block, even a fully selected one, copies only its inline content.
    • Across blocks: block structure is kept only when the selection spans blocks. That means the common container's markers (e.g. a quote spanning two paragraphs), plus the list or table that selected items or cells need.
    • Triple-click boundary: the range is first trimmed to its selected content. A triple-click in Chromium ends at offset 0 of the next block, which used to copy an empty trailing item (## Heading\n\n3.).
    • Task checkboxes: partially selected task items keep their [x] / [ ] marker.
    • Bare URLs: links whose visible text is their own URL (GFM bare, www. and email autolinks) copy as the selected text, unescaped. Named links keep [text](href).
    • Ordered list start: the start-number fix-up now maps copied lists to their originals correctly when ancestors are not rebuilt.
  • Applies to every caller: the timeline copy handler, Add to chat (readSelectedMessageMarkdown) and secondary-panel selections.
  • ThreadTimelineRows.actions.test.tsx: the partial-link Add to chat case now expects [oc](https://example.com) instead of - [oc](https://example.com).
  • Behaviour change: fully selecting a single list item now copies Four, not 4. Four. The ordered-number test now covers a two-item selection (4. Four\n5. Five).

How you verified

  • New table-driven cases in SelectableMessageProse.clipboard.test.tsx:
    • substrings and whole single blocks (heading, list item, nested task item, quote paragraph, table cell), checking text and HTML;
    • multi-block selections (heading into a list, list items, nested task items, quote paragraphs, table cells);
    • the triple-click boundary shape;
    • bare, partial, www., email and list-item URLs, plus a named link.
  • The new cases fail on main and pass with this change.
  • pnpm exec turbo run typecheck lint test --filter=@bb/app passes (6062 tests).
  • Real Chromium against a pnpm start:worktree build, using real mouse drags and triple-clicks on an agent reply, then reading the copy event's clipboard data:
Selection Copied text
drag over a bare URL https://github.com/get-bb/bb/pull/1234?tab=files_changed#diff_a_b
drag over a named link [the docs](https://docs.example.com/guide)
triple-click heading Hello **bold** words
triple-click a list item Four
triple-click a nested task item Nested item
drag across two list items 3. Three\n4. Four
drag across a whole quote > Quoted *emphasis* words\n> \n> Second
drag across two table cells | some cell text | other |\n| --- | --- |

🤖 Generated with Claude Code

AGENT GENERATED

selectionClipboard rebuilt every ancestor of the selection up to the
message root, so copying a word from a heading, list item, quote or table
cell produced "## word", "- word", "> word" or a one-cell table, and a URL
alone in a list item copied as "2. [url](url)". A selection inside one
block now copies its inline content only. Block structure is kept when the
selection spans blocks: the common container's markers, plus the list or
table its selected items and cells need. The range is first trimmed to
its selected content, so triple-clicks that end at the start of the next
block no longer copy an empty item.

Links whose visible text is their own URL (bare, www and email autolinks)
copy as the selected text, unescaped, so they paste into a browser
address bar. Named links keep their Markdown.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit a8baa7c into main Oct 19, 2026
28 checks passed
@ymichael
ymichael deleted the copy-selection-without-block-markdown branch October 19, 2026 21:02
ymichael added a commit that referenced this pull request Oct 28, 2026
#6487)

🚨 **SLOP COP** 🚨 · `new-issue-autopilot`

## Human comments

## What was wrong

Selection copy writes Markdown to `text/plain`, so Markdown syntax
leaked into pastes where people expect the visible text (the same class
as #6376 and #5776):

- **Inline code (#6484):** copying a code-only selection such as `valet
restart nginx` pasted `` `valet restart nginx` ``. Only `pre` bypassed
the serializer.
- **Formatted spans:** copying a word inside bold, italic, strikethrough
or a link pasted `**important**` or
`[deployment](https://example.com/deploy)`.
- **Prose escapes:** `escapeText` escaped every `` \ ` * _ [ ] < > ``,
so copying `config_loader.py`, `C:\Users\me\notes.txt` or `x < y,
arr[0]` pasted `config\_loader.py`, `C:\\Users\\me\\notes.txt` and `x \<
y, arr\[0\]`. It also turned `<https://example.com>` into
`<https://example.com\>`, which re-renders as the wrong URL.

## What changed

- `selectionClipboard` copies the visible text when the whole selection
sits inside one `pre`, inline `code`, link, `strong`/`b`, `em`/`i` or
`del`/`s` span. Wider selections, cross-message selections and Add to
chat keep Markdown. Math inside such a span still copies as TeX, because
KaTeX's hidden duplicate layout would otherwise be pasted twice.
- `escapeText` escapes a character only where Markdown would reinterpret
it: `_` unless between letters or digits, `\` before punctuation or at
the end, `[` and `>` at line start, `]` before `(`, `[` or `:`, `<`
before a tag or autolink start, and always `` ` `` and `*`.
- Rich HTML is unchanged. No public API, wire, schema or storage
changes.

## How you verified

- Real app (`pnpm start:worktree`, headless Chromium, real mouse
triple-click, double-click and drag on an agent reply, reading the copy
event's `text/plain`), main compared with this branch:

  | Selection | main | this PR |
  | --- | --- | --- |
| triple-click / drag `valet restart nginx` | `` `valet restart nginx`
`` | `valet restart nginx` |
  | double-click `my_old_var` in prose | `my\_old\_var` | `my_old_var` |
| `Open C:\Users\me\notes.txt` paragraph | `C:\\Users\\me\\notes.txt` |
`C:\Users\me\notes.txt` |
| `x < y and y > z, arr[0]` paragraph | `x \< y and y \> z, arr\[0\]` |
unchanged text |
  | double-click a bold word | `**important**` | `important` |
| word inside a named link | `[deployment](https://example.com/deploy)`
| `deployment` |
| paragraph mixing prose, bold and italic, lists, quotes, tables, whole
message | Markdown | unchanged Markdown |

- New tests: visible-text copy for every span type across five selection
shapes; a round trip that copies tricky prose, re-renders the copied
Markdown and requires the same text (fails on main for the over-escaped
cases and the autolink); math inside bold (fails without the math
guard). Four existing tests that pinned the old Markdown-around-a-span
output were updated.
- `pnpm exec turbo run test --filter=@bb/app --
src/components/thread/timeline src/components/secondary-panel src/lib
src/components/ui/markdown-preview`: 215 files, 2173 passed. `pnpm exec
turbo run lint typecheck --filter=@bb/app` passes.
- Not verified: native Cmd+C in bb Desktop or Android (same DOM copy
path).

Fixes #6484

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- slopcop:rule=new-issue-autopilot run=run_6fij7oq0mvsmw015
sha=issue-6484 kind=summary -->

---------

Co-authored-by: SawyerHood <[email protected]>
Co-authored-by: Michael Yong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant