2026-07-12 11:28:39 +07:00
|
|
|
//! Markdown-to-styled-spans rendering for the chat transcript.
|
|
|
|
|
//!
|
|
|
|
|
//! Flow: `render_markdown` walks a `pulldown_cmark` event stream and
|
2026-07-13 05:42:17 +07:00
|
|
|
//! translates each markdown construct into styled `ratatui::text::Span`s,
|
|
|
|
|
//! then re-wraps the flat span list to a target column width.
|
2026-07-12 11:28:39 +07:00
|
|
|
//!
|
2026-07-13 05:42:17 +07:00
|
|
|
//! Design: code blocks get a dark background with a labeled top bar,
|
|
|
|
|
//! headings are bold with distinct colors, blockquotes get a vertical
|
|
|
|
|
//! accent bar prefix, and inline code is highlighted with a background.
|
2026-07-12 11:28:39 +07:00
|
|
|
|
2026-07-13 05:42:17 +07:00
|
|
|
use ratatui::style::{Modifier, Style};
|
2026-07-11 13:16:10 +07:00
|
|
|
use ratatui::text::Span;
|
2026-07-13 05:42:17 +07:00
|
|
|
use super::theme::Theme;
|
2026-07-11 13:16:10 +07:00
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Render a markdown string into styled terminal spans, word-wrapped to `width`.
|
|
|
|
|
///
|
|
|
|
|
/// Flow: pulldown_cmark parses `text` into an event stream → each
|
2026-07-13 05:42:17 +07:00
|
|
|
/// Start/End/Text/Code/Break event is translated into styled `Span`s →
|
|
|
|
|
/// if `width > 0`, a second pass wraps long lines.
|
2026-07-12 11:28:39 +07:00
|
|
|
///
|
|
|
|
|
/// Return: a flat vec of styled spans; `chat::split_spans_into_lines`
|
|
|
|
|
/// turns it back into `Line`s for the Paragraph widget.
|
2026-07-12 01:25:52 +07:00
|
|
|
pub fn render_markdown(text: &str, width: u16) -> Vec<Span<'static>> {
|
2026-07-11 13:16:10 +07:00
|
|
|
let mut spans = Vec::new();
|
|
|
|
|
let parser = pulldown_cmark::Parser::new(text);
|
|
|
|
|
let mut in_code_block = false;
|
2026-07-13 05:42:17 +07:00
|
|
|
let mut in_heading = false;
|
|
|
|
|
let mut heading_level = 0;
|
|
|
|
|
let mut first_in_paragraph = true;
|
2026-07-11 13:16:10 +07:00
|
|
|
|
|
|
|
|
for event in parser {
|
|
|
|
|
match event {
|
|
|
|
|
pulldown_cmark::Event::Start(tag) => {
|
|
|
|
|
match tag {
|
|
|
|
|
pulldown_cmark::Tag::CodeBlock(_) => {
|
|
|
|
|
in_code_block = true;
|
2026-07-13 05:42:17 +07:00
|
|
|
// Code block top bar
|
2026-07-11 13:16:10 +07:00
|
|
|
spans.push(Span::styled(
|
2026-07-13 05:42:17 +07:00
|
|
|
"\n",
|
|
|
|
|
Style::default(),
|
|
|
|
|
));
|
|
|
|
|
spans.push(Span::styled(
|
|
|
|
|
" ┌─ code ",
|
|
|
|
|
Style::default().fg(Theme::TEXT_MUTED).bg(Theme::CODE_BG),
|
|
|
|
|
));
|
|
|
|
|
spans.push(Span::styled(
|
|
|
|
|
"\n",
|
|
|
|
|
Style::default(),
|
2026-07-11 13:16:10 +07:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::Tag::Heading { level, .. } => {
|
2026-07-13 05:42:17 +07:00
|
|
|
in_heading = true;
|
|
|
|
|
heading_level = match level {
|
|
|
|
|
pulldown_cmark::HeadingLevel::H1 => 1,
|
|
|
|
|
pulldown_cmark::HeadingLevel::H2 => 2,
|
|
|
|
|
pulldown_cmark::HeadingLevel::H3 => 3,
|
|
|
|
|
_ => 4,
|
2026-07-11 13:16:10 +07:00
|
|
|
};
|
2026-07-13 05:42:17 +07:00
|
|
|
// No prefix, we'll handle in the text events
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::Tag::Paragraph => {
|
|
|
|
|
first_in_paragraph = true;
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
pulldown_cmark::Tag::Emphasis => {}
|
|
|
|
|
pulldown_cmark::Tag::Strong => {}
|
|
|
|
|
pulldown_cmark::Tag::List(_) => {}
|
|
|
|
|
pulldown_cmark::Tag::Item => {
|
2026-07-13 05:42:17 +07:00
|
|
|
// List item bullet
|
2026-07-11 13:16:10 +07:00
|
|
|
spans.push(Span::styled(
|
2026-07-13 05:42:17 +07:00
|
|
|
" • ",
|
|
|
|
|
Style::default().fg(Theme::PRIMARY),
|
2026-07-11 13:16:10 +07:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::Tag::Link { dest_url, .. } => {
|
|
|
|
|
spans.push(Span::styled(
|
|
|
|
|
"[",
|
2026-07-13 05:42:17 +07:00
|
|
|
Style::default().fg(Theme::INFO),
|
2026-07-11 13:16:10 +07:00
|
|
|
));
|
2026-07-13 05:42:17 +07:00
|
|
|
// We push the URL as a tooltip-like suffix
|
|
|
|
|
// After the link text ends, we'll add the URL
|
2026-07-11 13:16:10 +07:00
|
|
|
spans.push(Span::styled(
|
|
|
|
|
format!("]({})", dest_url),
|
2026-07-13 05:42:17 +07:00
|
|
|
Style::default().fg(Theme::TEXT_MUTED).add_modifier(Modifier::ITALIC),
|
2026-07-11 13:16:10 +07:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::Tag::BlockQuote(_) => {
|
|
|
|
|
spans.push(Span::styled(
|
2026-07-13 05:42:17 +07:00
|
|
|
"▎",
|
|
|
|
|
Style::default().fg(Theme::BLOCKQUOTE_BAR),
|
2026-07-11 13:16:10 +07:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::Event::End(tag) => {
|
|
|
|
|
match tag {
|
|
|
|
|
pulldown_cmark::TagEnd::CodeBlock => {
|
|
|
|
|
in_code_block = false;
|
2026-07-13 05:42:17 +07:00
|
|
|
// Code block bottom bar
|
2026-07-11 13:16:10 +07:00
|
|
|
spans.push(Span::styled(
|
2026-07-13 05:42:17 +07:00
|
|
|
"\n └─\n",
|
|
|
|
|
Style::default().fg(Theme::TEXT_MUTED).bg(Theme::CODE_BG),
|
2026-07-11 13:16:10 +07:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::TagEnd::Heading(_) => {
|
2026-07-13 05:42:17 +07:00
|
|
|
in_heading = false;
|
|
|
|
|
heading_level = 0;
|
2026-07-11 13:16:10 +07:00
|
|
|
spans.push(Span::raw("\n"));
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::TagEnd::Paragraph => {
|
2026-07-13 05:42:17 +07:00
|
|
|
first_in_paragraph = true;
|
2026-07-11 13:16:10 +07:00
|
|
|
spans.push(Span::raw("\n\n"));
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::TagEnd::Emphasis => {}
|
|
|
|
|
pulldown_cmark::TagEnd::Strong => {}
|
|
|
|
|
pulldown_cmark::TagEnd::List(_) => {}
|
|
|
|
|
pulldown_cmark::TagEnd::Item => {
|
|
|
|
|
spans.push(Span::raw("\n"));
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::TagEnd::Link => {}
|
|
|
|
|
pulldown_cmark::TagEnd::BlockQuote(_) => {
|
|
|
|
|
spans.push(Span::raw("\n"));
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::Event::Text(text) => {
|
|
|
|
|
let s = text.to_string();
|
|
|
|
|
if in_code_block {
|
|
|
|
|
spans.push(Span::styled(
|
2026-07-13 05:42:17 +07:00
|
|
|
format!(" {}", s),
|
|
|
|
|
Style::default().fg(Theme::ACCENT_TEAL).bg(Theme::CODE_BG),
|
|
|
|
|
));
|
|
|
|
|
} else if in_heading {
|
|
|
|
|
let color = match heading_level {
|
|
|
|
|
1 => Theme::PRIMARY,
|
|
|
|
|
2 => Theme::INFO,
|
|
|
|
|
3 => Theme::ACCENT_PURPLE,
|
|
|
|
|
_ => Theme::TEXT,
|
|
|
|
|
};
|
|
|
|
|
let prefix = match heading_level {
|
|
|
|
|
1 => " ",
|
|
|
|
|
2 => " ",
|
|
|
|
|
3 => " ",
|
|
|
|
|
_ => " ",
|
|
|
|
|
};
|
|
|
|
|
spans.push(Span::styled(
|
|
|
|
|
format!("{}{}", prefix, s),
|
|
|
|
|
Style::default().fg(color).add_modifier(Modifier::BOLD),
|
2026-07-11 13:16:10 +07:00
|
|
|
));
|
|
|
|
|
} else {
|
2026-07-13 05:42:17 +07:00
|
|
|
// Handle first word detection for paragraph indentation
|
|
|
|
|
if first_in_paragraph {
|
|
|
|
|
spans.push(Span::raw(" "));
|
|
|
|
|
first_in_paragraph = false;
|
|
|
|
|
}
|
2026-07-11 13:16:10 +07:00
|
|
|
spans.push(Span::raw(s));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::Event::Code(text) => {
|
2026-07-13 05:42:17 +07:00
|
|
|
// Inline code with background
|
2026-07-11 13:16:10 +07:00
|
|
|
spans.push(Span::styled(
|
2026-07-13 05:42:17 +07:00
|
|
|
format!(" {} ", text),
|
|
|
|
|
Style::default()
|
|
|
|
|
.fg(Theme::ACCENT_TEAL)
|
|
|
|
|
.bg(Theme::CODE_BAR)
|
|
|
|
|
.add_modifier(Modifier::BOLD),
|
2026-07-11 13:16:10 +07:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
pulldown_cmark::Event::SoftBreak => {
|
2026-07-13 05:42:17 +07:00
|
|
|
spans.push(Span::raw(" "));
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
pulldown_cmark::Event::HardBreak => {
|
|
|
|
|
spans.push(Span::raw("\n"));
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if width > 0 {
|
|
|
|
|
let mut spans_out = Vec::new();
|
|
|
|
|
let mut line_len = 0;
|
2026-07-13 05:42:17 +07:00
|
|
|
let effective_width = (width as usize).saturating_sub(2); // leave margin
|
|
|
|
|
|
2026-07-11 13:16:10 +07:00
|
|
|
for span in &spans {
|
2026-07-12 01:25:52 +07:00
|
|
|
let style = span.style;
|
2026-07-11 13:16:10 +07:00
|
|
|
let s = span.content.clone();
|
2026-07-13 05:42:17 +07:00
|
|
|
let text_str = s.as_ref();
|
|
|
|
|
let remaining = text_str.len();
|
|
|
|
|
|
|
|
|
|
if line_len + remaining > effective_width && line_len > 0 {
|
2026-07-11 13:16:10 +07:00
|
|
|
spans_out.push(Span::raw("\n"));
|
|
|
|
|
line_len = 0;
|
|
|
|
|
}
|
2026-07-13 05:42:17 +07:00
|
|
|
|
|
|
|
|
spans_out.push(Span::styled(text_str.to_string(), style));
|
|
|
|
|
|
|
|
|
|
if !text_str.contains('\n') {
|
2026-07-11 13:16:10 +07:00
|
|
|
line_len += remaining;
|
|
|
|
|
} else {
|
2026-07-13 05:42:17 +07:00
|
|
|
line_len = text_str.split('\n').next_back().unwrap_or("").len();
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
spans = spans_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spans
|
|
|
|
|
}
|