Preserve Markdown formatting when copying transcript selections (#47896)

## Why

Transcript selections were copied as plain text, losing the formatting of rendered Markdown when pasted.

## What changed

- Retain semantic Markdown annotations through rendering, wrapping, and streaming, then serialize selected text with balanced markup.
- Preserve inline formatting, web links, headings, lists, blockquotes, and hard breaks in Markdown clipboard payloads.
- Keep code-only and other literal-only selections as plain text. In mixed selections, escape user prose and fence code, tables, and tool output to preserve their literal content.

## Testing

Add unit tests and Markdown/HTML snapshots covering partial selections, nested containers, escaping, links, resize stability, and streaming boundaries.

GitOrigin-RevId: f0476334a6bf8ad0fb57626627bd258443540852
This commit is contained in:
Felipe Coury
2026-09-24 17:55:29 +00:00
committed by copyberry
parent f6fd7f17ed
commit a33fb9751c
38 changed files with 1790 additions and 41 deletions
+33 -9
View File
@@ -159,7 +159,10 @@ impl TranscriptView {
})
}
pub(crate) fn selected_text(&mut self, cells: &[Arc<dyn HistoryCell>]) -> Option<String> {
fn selected_ranges(
&mut self,
cells: &[Arc<dyn HistoryCell>],
) -> Option<Vec<(Arc<TextLayout>, std::ops::Range<usize>)>> {
if !self.has_selection_range() {
return None;
}
@@ -176,16 +179,12 @@ impl TranscriptView {
if start == end {
return None;
}
let mut text = String::new();
let mut previous: Option<Arc<TextLayout>> = None;
let mut ranges = Vec::new();
for index in start.index..=end.index {
let layout = self.layout(cells, index)?;
if layout.row_count() == 0 {
continue;
}
if let Some(previous) = &previous {
text.push_str(previous.separator_after(&layout));
}
let begin = if index == start.index {
start.offset
} else {
@@ -196,7 +195,19 @@ impl TranscriptView {
} else {
layout.text().len()
};
text.push_str(layout.text().get(begin..finish)?);
ranges.push((layout, begin..finish));
}
Some(ranges)
}
pub(crate) fn selected_text(&mut self, cells: &[Arc<dyn HistoryCell>]) -> Option<String> {
let mut text = String::new();
let mut previous: Option<Arc<TextLayout>> = None;
for (layout, range) in self.selected_ranges(cells)? {
if let Some(previous) = &previous {
text.push_str(previous.separator_after(&layout));
}
text.push_str(layout.text().get(range)?);
previous = Some(layout);
}
text.retain(|ch| !ch.is_control() || matches!(ch, '\n' | '\t'));
@@ -210,9 +221,22 @@ impl TranscriptView {
cells: &[Arc<dyn HistoryCell>],
text: &str,
clear_selection: bool,
copy: impl FnOnce(&str) -> Result<crate::clipboard_copy::CopyStatus, String>,
copy: impl FnOnce(
&str,
crate::clipboard_copy::CopyFormat,
) -> Result<crate::clipboard_copy::CopyStatus, String>,
) -> Result<crate::clipboard_copy::CopyStatus, String> {
let result = copy(text);
let mut lines = Vec::new();
let mut previous: Option<Arc<TextLayout>> = None;
for (layout, range) in self.selected_ranges(cells).unwrap_or_default() {
let separator = previous
.as_ref()
.map_or("", |previous| previous.separator_after(&layout));
layout.copy_lines(range, separator, &mut lines);
previous = Some(layout);
}
let (text, format) = crate::markdown_copy::selection(&lines, text);
let result = copy(&text, format);
match result {
Ok(crate::clipboard_copy::CopyStatus::Confirmed) => {
if clear_selection {