Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion codex-rs/tui/src/markdown_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@
//! unusably short chunks, expansive cells form tall narrow strips across enough
//! body rows, or even 3-char-wide columns cannot fit, body rows render as
//! key/value records.
//!
//! Inline code and local file paths share the active syntax theme's raw-markup foreground.

use crate::markdown_text_merge::DecodedTextMerge;
use crate::render::highlight::current_syntax_theme;
use crate::render::highlight::foreground_style_for_scopes;
use crate::render::highlight::foreground_style_for_scopes_with_theme;
use crate::render::highlight::highlight_code_to_lines;
use crate::render::line_utils::line_to_static;
use crate::style::table_separator_style;
Expand Down Expand Up @@ -109,14 +112,27 @@ struct MarkdownStyles {

impl Default for MarkdownStyles {
fn default() -> Self {
Self::for_theme(&crate::render::highlight::current_syntax_theme())
}
}

impl MarkdownStyles {
fn for_theme(theme: &syntect::highlighting::Theme) -> Self {
Self {
h1: Style::new().bold().underlined(),
h2: Style::new().bold(),
h3: Style::new().bold().italic(),
h4: Style::new().italic(),
h5: Style::new().italic(),
h6: Style::new().italic(),
code: Style::new().cyan(),
code: foreground_style_for_scopes_with_theme(
theme,
&[
"markup.inline.raw.string.markdown",
"markup.raw.inline.markdown",
],
)
.unwrap_or_else(|| Style::new().cyan()),
emphasis: Style::new().italic(),
strong: Style::new().bold(),
strikethrough: Style::new().crossed_out(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ expression: presentations
Span::from("plain "),
Span::from("plain").cyan().underlined(),
Span::from(" "),
Span::from("code").cyan(),
Span::from("code").fg(Color::Rgb(166, 227, 161)),
Span::from(" "),
Span::from("code").cyan().underlined(),
]),
Expand All @@ -31,7 +31,7 @@ expression: presentations
Span::from("https://example.com").cyan().underlined(),
Span::from(")"),
Span::from(" "),
Span::from("code").cyan(),
Span::from("code").fg(Color::Rgb(166, 227, 161)),
]),
Line::from_iter([
Span::from("code").cyan().underlined(),
Expand Down
135 changes: 103 additions & 32 deletions codex-rs/tui/src/markdown_render_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::MarkdownStyles;
use pretty_assertions::assert_eq;
use ratatui::style::Modifier;
use ratatui::style::Stylize;
Expand Down Expand Up @@ -711,10 +712,60 @@ fn ordered_item_with_indented_continuation_is_tight() {
#[test]
fn inline_code() {
let text = render_markdown_text("Example of `Inline code`");
let expected = Line::from_iter(["Example of ".into(), "Inline code".cyan()]).into();
let expected = Line::from_iter([
"Example of ".into(),
Span::styled("Inline code", MarkdownStyles::default().code),
])
.into();
assert_eq!(text, expected);
}

#[test]
fn inline_code_and_file_paths_follow_syntax_theme() {
let markdown = "Use `src/main.rs` and [src/lib.rs](./src/lib.rs).";
let mut rendered = Vec::new();
for name in [
"catppuccin-mocha",
"catppuccin-latte",
"ansi",
"no-markup-color",
] {
let theme = if name == "no-markup-color" {
syntect::highlighting::Theme::default()
} else {
crate::render::highlight::resolve_theme_by_name(name, /*codex_home*/ None)
.expect("bundled theme")
};
let styles = MarkdownStyles::for_theme(&theme);
let code_style = styles.code;
let mut writer = super::Writer::new(
markdown,
pulldown_cmark::Parser::new(markdown).into_offset_iter(),
/*wrap_width*/ Some(24),
/*cwd*/ None,
&|_| false,
);
writer.styles = styles;
writer.run();
let lines = crate::terminal_hyperlinks::visible_lines(writer.text);
let paths = lines
.iter()
.flat_map(|line| &line.spans)
.filter(|span| matches!(span.content.as_ref(), "src/main.rs" | "src/lib.rs"))
.cloned()
.collect::<Vec<_>>();
assert_eq!(
paths,
vec![
Span::styled("src/main.rs", code_style),
Span::styled("src/lib.rs", code_style),
],
);
rendered.push((name, lines));
}
assert_debug_snapshot!(rendered);
}

#[test]
fn strong() {
assert_eq!(
Expand Down Expand Up @@ -779,7 +830,7 @@ fn web_link_labels_use_link_style_and_preserve_inline_formatting() {
"https://example.com".cyan().underlined(),
")".into(),
" after ".into(),
"code".cyan(),
Span::styled("code", MarkdownStyles::default().code),
]));
assert_eq!(text, expected, "label: {label}");
}
Expand Down Expand Up @@ -828,7 +879,10 @@ fn file_link_hides_destination() {
"[/Users/example/code/codex/codex-rs/tui/src/My%20File.rs](/Users/example/code/codex/codex-rs/tui/src/My%20File.rs)",
Path::new("/Users/example/code/codex"),
);
let expected = Text::from(Line::from_iter(["codex-rs/tui/src/My File.rs".cyan()]));
let expected = Text::from(Line::from_iter([Span::styled(
"codex-rs/tui/src/My File.rs",
MarkdownStyles::default().code,
)]));
assert_eq!(text, expected);
}

Expand All @@ -840,11 +894,14 @@ fn file_link_keeps_descriptive_label_and_target() {
);
let expected = Text::from(Line::from_iter([
"Your ".into(),
"codex".cyan(),
Span::styled("codex", MarkdownStyles::default().code),
" launcher ".into(),
"automatically adds those overrides".into(),
" (".into(),
"project/dotslash-gen/bin/codex:1105".cyan(),
Span::styled(
"project/dotslash-gen/bin/codex:1105",
MarkdownStyles::default().code,
),
")".into(),
", even though you did not specify any.".into(),
]));
Expand Down Expand Up @@ -921,7 +978,10 @@ fn file_link_decodes_percent_encoded_bare_path_destination() {
let expected = Text::from(Line::from_iter([
"open Example Folder/Résumé/report.md".into(),
" (".into(),
"Example Folder/Résumé/report.md".cyan(),
Span::styled(
"Example Folder/Résumé/report.md",
MarkdownStyles::default().code,
),
")".into(),
]));
assert_eq!(text, expected);
Expand Down Expand Up @@ -952,7 +1012,7 @@ fn file_link_keeps_unrelated_relative_label_with_matching_suffix() {
let expected = Text::from(Line::from_iter([
"other/src/lib.rs".into(),
" (".into(),
"src/lib.rs".cyan(),
Span::styled("src/lib.rs", MarkdownStyles::default().code),
")".into(),
]));
assert_eq!(text, expected);
Expand All @@ -965,9 +1025,10 @@ fn file_link_appends_line_number_when_label_lacks_it() {
"[markdown_render.rs](/Users/example/code/codex/codex-rs/tui/src/markdown_render.rs:74)",
Path::new("/Users/example/code/codex"),
);
let expected = Text::from(Line::from_iter([
"codex-rs/tui/src/markdown_render.rs:74".cyan()
]));
let expected = Text::from(Line::from_iter([Span::styled(
"codex-rs/tui/src/markdown_render.rs:74",
MarkdownStyles::default().code,
)]));
assert_eq!(text, expected);
}

Expand All @@ -977,9 +1038,10 @@ fn file_link_keeps_absolute_paths_outside_cwd() {
"[README.md:74](/Users/example/code/codex/README.md:74)",
Path::new("/Users/example/code/codex/codex-rs/tui"),
);
let expected = Text::from(Line::from_iter([
"/Users/example/code/codex/README.md:74".cyan()
]));
let expected = Text::from(Line::from_iter([Span::styled(
"/Users/example/code/codex/README.md:74",
MarkdownStyles::default().code,
)]));
assert_eq!(text, expected);
}

Expand Down Expand Up @@ -1010,9 +1072,10 @@ fn file_link_appends_hash_anchor_when_label_lacks_it() {
"[markdown_render.rs](file:///Users/example/code/codex/codex-rs/tui/src/markdown_render.rs#L74C3)",
Path::new("/Users/example/code/codex"),
);
let expected = Text::from(Line::from_iter([
"codex-rs/tui/src/markdown_render.rs:74:3".cyan(),
]));
let expected = Text::from(Line::from_iter([Span::styled(
"codex-rs/tui/src/markdown_render.rs:74:3",
MarkdownStyles::default().code,
)]));
assert_eq!(text, expected);
}

Expand All @@ -1022,9 +1085,10 @@ fn file_link_uses_target_path_for_hash_anchor() {
"[markdown_render.rs#L74C3](file:///Users/example/code/codex/codex-rs/tui/src/markdown_render.rs#L74C3)",
Path::new("/Users/example/code/codex"),
);
let expected = Text::from(Line::from_iter([
"codex-rs/tui/src/markdown_render.rs:74:3".cyan(),
]));
let expected = Text::from(Line::from_iter([Span::styled(
"codex-rs/tui/src/markdown_render.rs:74:3",
MarkdownStyles::default().code,
)]));
assert_eq!(text, expected);
}

Expand All @@ -1034,9 +1098,10 @@ fn file_link_appends_range_when_label_lacks_it() {
"[markdown_render.rs](/Users/example/code/codex/codex-rs/tui/src/markdown_render.rs:74:3-76:9)",
Path::new("/Users/example/code/codex"),
);
let expected = Text::from(Line::from_iter([
"codex-rs/tui/src/markdown_render.rs:74:3-76:9".cyan(),
]));
let expected = Text::from(Line::from_iter([Span::styled(
"codex-rs/tui/src/markdown_render.rs:74:3-76:9",
MarkdownStyles::default().code,
)]));
assert_eq!(text, expected);
}

Expand All @@ -1046,9 +1111,10 @@ fn file_link_uses_target_path_for_range() {
"[markdown_render.rs:74:3-76:9](/Users/example/code/codex/codex-rs/tui/src/markdown_render.rs:74:3-76:9)",
Path::new("/Users/example/code/codex"),
);
let expected = Text::from(Line::from_iter([
"codex-rs/tui/src/markdown_render.rs:74:3-76:9".cyan(),
]));
let expected = Text::from(Line::from_iter([Span::styled(
"codex-rs/tui/src/markdown_render.rs:74:3-76:9",
MarkdownStyles::default().code,
)]));
assert_eq!(text, expected);
}

Expand All @@ -1058,9 +1124,10 @@ fn file_link_appends_hash_range_when_label_lacks_it() {
"[markdown_render.rs](file:///Users/example/code/codex/codex-rs/tui/src/markdown_render.rs#L74C3-L76C9)",
Path::new("/Users/example/code/codex"),
);
let expected = Text::from(Line::from_iter([
"codex-rs/tui/src/markdown_render.rs:74:3-76:9".cyan(),
]));
let expected = Text::from(Line::from_iter([Span::styled(
"codex-rs/tui/src/markdown_render.rs:74:3-76:9",
MarkdownStyles::default().code,
)]));
assert_eq!(text, expected);
}

Expand All @@ -1077,7 +1144,10 @@ fn multiline_file_link_label_after_styled_prefix_does_not_panic() {
" ".into(),
"bar".into(),
" (".into(),
"codex-rs/tui/src/markdown_render.rs:74:3".cyan(),
Span::styled(
"codex-rs/tui/src/markdown_render.rs:74:3",
MarkdownStyles::default().code,
),
")".into(),
]));
assert_eq!(text, expected);
Expand All @@ -1089,9 +1159,10 @@ fn file_link_uses_target_path_for_hash_range() {
"[markdown_render.rs#L74C3-L76C9](file:///Users/example/code/codex/codex-rs/tui/src/markdown_render.rs#L74C3-L76C9)",
Path::new("/Users/example/code/codex"),
);
let expected = Text::from(Line::from_iter([
"codex-rs/tui/src/markdown_render.rs:74:3-76:9".cyan(),
]));
let expected = Text::from(Line::from_iter([Span::styled(
"codex-rs/tui/src/markdown_render.rs:74:3-76:9",
MarkdownStyles::default().code,
)]));
assert_eq!(text, expected);
}

Expand Down
Loading
Loading