diff --git a/codex-rs/tui/src/markdown_render/file_citations_tests.rs b/codex-rs/tui/src/markdown_render/file_citations_tests.rs index c5c00c64b0..a067fcbc6d 100644 --- a/codex-rs/tui/src/markdown_render/file_citations_tests.rs +++ b/codex-rs/tui/src/markdown_render/file_citations_tests.rs @@ -2,6 +2,7 @@ use crate::markdown::render_markdown_agent_with_links_and_cwd; use crate::markdown_render::render_markdown_lines_with_width_and_cwd; +use crate::terminal_hyperlinks::TerminalHyperlink; use itertools::Itertools; use pretty_assertions::assert_eq; use std::path::Path; @@ -13,6 +14,55 @@ fn rendered_text(markdown: &str, cwd: Option<&Path>) -> String { .join("\n") } +#[test] +fn web_file_citations_preserve_rendered_labels_and_destinations() { + let cwd = std::env::temp_dir(); + for cwd in [None, Some(cwd.as_path())] { + for destination in [ + "https://docs.google.com/spreadsheets/d/example/edit?tab=Q1%20Plan#gid=123", + "http://example.com/report%2Ffinal?download=1&name=a%20b#section", + "https://example.com/report:10:2-20:4", + "https://example.com/report#L10-L20", + "https://example.com/report?range=A1:B10", + ] { + let citation = + format!(":codex-file-citation{{path=\"{destination}\" purpose=\"output\"}}"); + let lines = + render_markdown_agent_with_links_and_cwd(&citation, /*width*/ None, cwd); + assert_eq!( + lines.iter().map(|line| line.line.to_string()).join("\n"), + format!(" ({destination})"), + ); + assert_eq!( + lines + .iter() + .flat_map(|line| line.hyperlinks.clone()) + .collect::>(), + vec![TerminalHyperlink::web( + 2..destination.len() + 2, + destination.to_owned() + )], + ); + assert_eq!( + lines, + render_markdown_agent_with_links_and_cwd( + &format!("[]({destination})"), + /*width*/ None, + cwd, + ), + ); + } + } +} + +#[test] +fn web_file_citation_snapshot() { + let markdown = r#":codex-file-citation{path="https://docs.google.com/spreadsheets/d/example/edit?tab=Q1%20Plan#gid=123" purpose="output"}"#; + let rendered = + render_markdown_agent_with_links_and_cwd(markdown, /*width*/ None, /*cwd*/ None); + insta::assert_debug_snapshot!(rendered); +} + #[test] fn file_citation_paths_preserve_markdown_significant_characters() { for path in [ diff --git a/codex-rs/tui/src/markdown_render/inline_directives.rs b/codex-rs/tui/src/markdown_render/inline_directives.rs index 17e65637ea..df2d582e86 100644 --- a/codex-rs/tui/src/markdown_render/inline_directives.rs +++ b/codex-rs/tui/src/markdown_render/inline_directives.rs @@ -7,6 +7,7 @@ use super::local_links::is_local_path_like_link; use crate::assistant_directives::AssistantDirective; use crate::assistant_directives::QuoteEscaping; use crate::assistant_directives::parse_assistant_directive_with_budget; +use crate::terminal_hyperlinks::web_destination; use itertools::Either; use pulldown_cmark::Event; use pulldown_cmark::LinkType; @@ -216,23 +217,28 @@ impl<'a> InlineDirectives<'a> { continue; } let path = directive.attributes["path"].as_ref(); - let destination = if is_local_path_like_link(path) { - path.to_string() + let destination = if let Some(destination) = web_destination(path) { + destination } else { - cwd.map_or_else( - || format!("./{path}"), - |cwd| cwd.join(path).to_string_lossy().into_owned(), - ) + let destination = if is_local_path_like_link(path) { + path.to_string() + } else { + cwd.map_or_else( + || format!("./{path}"), + |cwd| cwd.join(path).to_string_lossy().into_owned(), + ) + }; + // Local citation paths are literal; the link renderer decodes destinations. + let mut destination = destination + .replace('%', "%25") + .replace('#', "%23") + .replace('?', "%3F"); + if let Some(suffix) = extract_colon_location_suffix(&destination) { + let suffix_start = destination.len() - suffix.len(); + destination.replace_range(suffix_start.., &suffix.replace(':', "%3A")); + } + destination }; - // Citation paths are literal; the existing link renderer decodes destinations. - let mut destination = destination - .replace('%', "%25") - .replace('#', "%23") - .replace('?', "%3F"); - if let Some(suffix) = extract_colon_location_suffix(&destination) { - let suffix_start = destination.len() - suffix.len(); - destination.replace_range(suffix_start.., &suffix.replace(':', "%3A")); - } // Citations have no author-provided label; render the formatted destination alone. events.extend([ ( diff --git a/codex-rs/tui/src/markdown_render/snapshots/codex_tui__markdown_render__inline_directives__tests__web_file_citation_snapshot.snap b/codex-rs/tui/src/markdown_render/snapshots/codex_tui__markdown_render__inline_directives__tests__web_file_citation_snapshot.snap new file mode 100644 index 0000000000..831d1e827d --- /dev/null +++ b/codex-rs/tui/src/markdown_render/snapshots/codex_tui__markdown_render__inline_directives__tests__web_file_citation_snapshot.snap @@ -0,0 +1,20 @@ +--- +source: tui/src/markdown_render/file_citations_tests.rs +expression: rendered +--- +[ + HyperlinkLine { + line: Line::from_iter([ + Span::from(" ("), + Span::from("https://docs.google.com/spreadsheets/d/example/edit?tab=Q1%20Plan#gid=123").fg(Color::Reset).underlined(), + Span::from(")"), + ]), + hyperlinks: [ + TerminalHyperlink { + columns: 2..75, + destination: "https://docs.google.com/spreadsheets/d/example/edit?tab=Q1%20Plan#gid=123", + destination_kind: Web, + }, + ], + }, +]