mirror of
https://github.com/openai/codex.git
synced 2026-11-26 07:26:11 +00:00
Preserve Markdown formatting when copying TUI responses (#42847)
## What changed - Add rendered HTML alongside the original Markdown when copying a whole response through the native clipboard, so rich-text destinations preserve headings, lists, tables, code, and other formatting. - Keep code-block, blockquote, transcript, terminal, and WSL clipboard copies as plain text. - Escape raw HTML, omit remote image sources, and render unsupported link destinations as visible text when producing clipboard HTML. ## Testing - Add coverage for rich formatting, local and unsafe links, inert images, fenced tables, copy selection formats, and clipboard fallbacks. GitOrigin-RevId: c6f5edd88b65f9e9b1ddc5618442868cf47ee560
This commit is contained in:
1 parent
9c4253ffc1
commit
7a8092a447
17 files changed
+329
-57
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
use super::*;
|
||||
use crate::bottom_pane::BottomPaneView;
|
||||
use crate::clipboard_copy::CopyFormat;
|
||||
|
||||
impl ChatWidget {
|
||||
pub(crate) fn set_agents_navigation_enabled(&mut self, enabled: bool) {
|
||||
@@ -283,9 +284,11 @@ impl ChatWidget {
|
||||
false
|
||||
}
|
||||
|
||||
/// Copy the last agent response (raw markdown) to the system clipboard.
|
||||
/// Copy the last response as Markdown with HTML for rich-text destinations.
|
||||
pub(crate) fn copy_last_agent_markdown(&mut self) {
|
||||
self.copy_last_agent_markdown_with(crate::clipboard_copy::copy_to_clipboard);
|
||||
self.copy_last_agent_markdown_with(|text| {
|
||||
crate::clipboard_copy::copy_to_clipboard(text, CopyFormat::Markdown)
|
||||
});
|
||||
}
|
||||
|
||||
/// Inner implementation with an injectable clipboard backend for testing.
|
||||
@@ -327,6 +330,7 @@ impl ChatWidget {
|
||||
let mut choices = vec![(
|
||||
"Whole response".to_string(),
|
||||
Arc::<str>::from(markdown.as_str()),
|
||||
CopyFormat::Markdown,
|
||||
)];
|
||||
let source = self
|
||||
.transcript
|
||||
@@ -343,21 +347,27 @@ impl ChatWidget {
|
||||
|language| format!("{language} code"),
|
||||
),
|
||||
content,
|
||||
CopyFormat::PlainText,
|
||||
)),
|
||||
crate::markdown::CopyTarget::Quote(content) => {
|
||||
let content: String = content
|
||||
.split_inclusive('\n')
|
||||
.map(|line| crate::git_action_directives::strip_line_directives(line).0)
|
||||
.collect();
|
||||
(!content.trim().is_empty())
|
||||
.then(|| ("Blockquote".to_string(), Arc::from(content)))
|
||||
(!content.trim().is_empty()).then(|| {
|
||||
(
|
||||
"Blockquote".to_string(),
|
||||
Arc::from(content),
|
||||
CopyFormat::PlainText,
|
||||
)
|
||||
})
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let items = choices
|
||||
.into_iter()
|
||||
.map(|(label, text)| {
|
||||
.map(|(label, text, format)| {
|
||||
let description = text
|
||||
.lines()
|
||||
.find(|line| !line.trim().is_empty())
|
||||
@@ -369,6 +379,7 @@ impl ChatWidget {
|
||||
tx.send(AppEvent::CopySelection {
|
||||
text: Arc::clone(&text),
|
||||
label: label.clone(),
|
||||
format,
|
||||
});
|
||||
})],
|
||||
dismiss_on_select: true,
|
||||
@@ -386,8 +397,10 @@ impl ChatWidget {
|
||||
self.defer_input_until_settings_applied();
|
||||
}
|
||||
|
||||
pub(crate) fn copy_selection(&mut self, text: Arc<str>, label: String) {
|
||||
self.copy_selection_with(&text, &label, crate::clipboard_copy::copy_to_clipboard);
|
||||
pub(crate) fn copy_selection(&mut self, text: Arc<str>, label: String, format: CopyFormat) {
|
||||
self.copy_selection_with(&text, &label, |text| {
|
||||
crate::clipboard_copy::copy_to_clipboard(text, format)
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn copy_selection_with(
|
||||
|
||||
@@ -113,7 +113,7 @@ fn next_copy_selection(
|
||||
rx: &mut tokio::sync::mpsc::UnboundedReceiver<AppEvent>,
|
||||
) -> (String, String) {
|
||||
let selection = match rx.try_recv() {
|
||||
Ok(AppEvent::CopySelection { text, label }) => (text.to_string(), label),
|
||||
Ok(AppEvent::CopySelection { text, label, .. }) => (text.to_string(), label),
|
||||
other => panic!("expected selected clipboard content, got {other:?}"),
|
||||
};
|
||||
assert_matches!(rx.try_recv(), Ok(AppEvent::SettingsSelectionClosed));
|
||||
@@ -2089,6 +2089,37 @@ async fn slash_copy_picker_numeric_shortcuts_copy_whole_response_and_exact_code(
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn slash_copy_picker_renders_only_whole_responses_as_markdown() {
|
||||
use crate::clipboard_copy::CopyFormat;
|
||||
|
||||
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
let markdown = "**Intro**\n\n```text\n# literal *code*\n```\n\n> **Quote**\n";
|
||||
chat.transcript.last_agent_markdown = Some(markdown.to_string());
|
||||
for (key, expected) in [
|
||||
('1', (markdown, "Whole response", CopyFormat::Markdown)),
|
||||
(
|
||||
'2',
|
||||
("# literal *code*\n", "text code", CopyFormat::PlainText),
|
||||
),
|
||||
('3', ("**Quote**\n", "Blockquote", CopyFormat::PlainText)),
|
||||
] {
|
||||
chat.dispatch_command(SlashCommand::Copy);
|
||||
chat.handle_key_event(KeyEvent::new(KeyCode::Char(key), KeyModifiers::NONE));
|
||||
let event = rx.try_recv().expect("selected clipboard content");
|
||||
let AppEvent::CopySelection {
|
||||
text,
|
||||
label,
|
||||
format,
|
||||
} = event
|
||||
else {
|
||||
panic!("expected selected clipboard content, got {event:?}");
|
||||
};
|
||||
assert_eq!((text.as_ref(), label.as_str(), format), expected);
|
||||
assert_matches!(rx.try_recv(), Ok(AppEvent::SettingsSelectionClosed));
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn slash_copy_picker_preserves_nested_blockquote_markdown_and_code() {
|
||||
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
|
||||
@@ -5,7 +5,10 @@ use crate::app_event::TranscriptExportDestination;
|
||||
|
||||
impl ChatWidget {
|
||||
pub(crate) fn copy_transcript_to_clipboard(&mut self, markdown: &str) {
|
||||
match crate::clipboard_copy::copy_to_clipboard(markdown) {
|
||||
match crate::clipboard_copy::copy_to_clipboard(
|
||||
markdown,
|
||||
crate::clipboard_copy::CopyFormat::PlainText,
|
||||
) {
|
||||
Ok(lease) => {
|
||||
self.clipboard_lease = lease;
|
||||
self.add_info_message(
|
||||
|
||||
Reference in new issue
Block a user