mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
Render web URLs in TUI file citations as hyperlinks (#65052)
## Why The TUI treated URLs in file citations as local paths. This could prepend the working directory and escape parts of the URL needed to reach the cited resource. ## What changed Render HTTP and HTTPS `codex-file-citation` paths as web links, preserving percent escapes, query strings, and fragments in the displayed URL and hyperlink destination. ## Testing Regression tests compare file citations with equivalent Markdown links with and without a working directory. A snapshot covers the rendered text and hyperlink metadata for a spreadsheet URL. GitOrigin-RevId: 2f3213192fced0e92ae87062d086a9015113b862
This commit is contained in:
@@ -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<_>>(),
|
||||
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 [
|
||||
|
||||
@@ -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([
|
||||
(
|
||||
|
||||
+20
@@ -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,
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
Reference in New Issue
Block a user