A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
markdown: restore TS v0.9.8 rendering parity for pa-tui (#3591)
Sebastian Müller5f218f780dab
diff --git a/crates/pa-tui/src/chat.rs b/crates/pa-tui/src/chat.rsindex f8f0dfdbba..b80af8653e 100644--- a/crates/pa-tui/src/chat.rs+++ b/crates/pa-tui/src/chat.rs@@ -310,14 +310,24 @@ pub fn render_user_block(theme: &Theme,code_block_indent: &str,width: usize,+) -> Vec<Line> {+ let mut md = crate::markdown::MarkdownStyle::from_theme(theme);+ md.code_block_indent = code_block_indent.to_string();+ render_user_block_with_style(text, theme, &md, width)+}++/// The view supplies its session cwd through Markdown style, keeping href resolution local.+pub(crate) fn render_user_block_with_style(+ text: &str,+ theme: &Theme,+ md: &crate::markdown::MarkdownStyle,+ width: usize,) -> Vec<Line> {let bg = theme.bg_style(ThemeBg::UserMessageBg);let content_width = width.saturating_sub(4).max(1);let body = theme.fg_style(ThemeColor::UserMessageText);- let mut md = crate::markdown::MarkdownStyle::from_theme(theme);- md.code_block_indent = code_block_indent.to_string();let mask = geometry::user_mask(text);- let rendered = crate::markdown::render_markdown(&mask.text, content_width, &md);+ let rendered = crate::markdown::render_markdown(&mask.text, content_width, md);let mut rows: Vec<Line> = Vec::new();let blank = vec![Span::styled(" ".repeat(width), bg)];rows.push(blank.clone());@@ -371,6 +381,29 @@ pub fn render_assistant(width: usize,preceded_by_tool_activity: bool,cache: &mut crate::markdown::MarkdownBlockCache,+) -> Vec<Line> {+ let mut md = crate::markdown::MarkdownStyle::from_theme(theme);+ md.code_block_indent = code_block_indent.to_string();+ render_assistant_with_style(+ message,+ detail,+ theme,+ &md,+ width,+ preceded_by_tool_activity,+ cache,+ )+}++/// Render with the owning view's session-local Markdown context.+pub(crate) fn render_assistant_with_style(+ message: &AssistantMessage,+ detail: Detail,+ theme: &Theme,+ md: &crate::markdown::MarkdownStyle,+ width: usize,+ preceded_by_tool_activity: bool,+ cache: &mut crate::markdown::MarkdownBlockCache,) -> Vec<Line> {let visible_blocks = geometry::visible_blocks(message, detail);let has_visible_content = !visible_blocks.is_empty();@@ -378,15 +411,13 @@ pub fn render_assistant(if has_visible_content {out.push(spacer());}- let mut md = crate::markdown::MarkdownStyle::from_theme(theme);- md.code_block_indent = code_block_indent.to_string();for (index, block) in visible_blocks.iter().enumerate() {match block {MessageBlock::Text(text) => {- out.extend(render_markdown_block(text, &md, width, cache));+ out.extend(render_markdown_block(text, md, width, cache));}MessageBlock::Thinking(text) => {- out.extend(render_thinking_block(text, theme, &md, width, cache));+ out.extend(render_thinking_block(text, theme, md, width, cache));// Thinking adds spacing only when another visible block follows.if index + 1 < visible_blocks.len() {out.push(spacer());diff --git a/crates/pa-tui/src/chat/tests.rs b/crates/pa-tui/src/chat/tests.rsindex 937d600d4d..68f6eb3747 100644--- a/crates/pa-tui/src/chat/tests.rs+++ b/crates/pa-tui/src/chat/tests.rs@@ -178,7 +178,7 @@ fn user_block_keeps_the_link_affordance() {),body.add_modifier(ratatui::style::Modifier::UNDERLINED)),- (" [https://x.dev/a]".to_string(), link_url),+ (" (https://x.dev/a)".to_string(), link_url),(" ".repeat(60 - 28), bg),]);diff --git a/crates/pa-tui/src/hyperlinks.rs b/crates/pa-tui/src/hyperlinks.rsindex 73fc9ca47b..1f3604ab62 100644--- a/crates/pa-tui/src/hyperlinks.rs+++ b/crates/pa-tui/src/hyperlinks.rs@@ -1,6 +1,8 @@-//! Terminal hyperlinks (OSC 8), gated on terminal capabilities: a-//! positively-capable terminal wraps the link text in an OSC 8 pair; the-//! legacy form prints the URL after the text. The renderer embeds the+//! Terminal hyperlinks (OSC 8). Markdown retains label metadata even when+//! the capability decision selects a visible URL fallback, matching TS's+//! fullscreen backend and keeping in-app hit testing available. Other UI+//! surfaces may gate their link presentation on terminal capabilities.+//! The renderer embeds the//! zero-width sequences in span content; `width` skips them, the ratatui//! paint path strips them (`to_ratatui_line`), and [`HyperlinkWriter`]//! re-emits them around the painted link cells (the sequences must ride@@ -46,19 +48,23 @@ pub fn rewrite_drive_path(url: &str) -> String {}/// TS `case "link"` href resolution: drive-path rewrite, then a WHATWG-/// `new URL(target, baseUrl)` pass (the deployed renderer always sets-/// `options.baseUrl`). Relative targets pass through raw — the one-/// documented gap (they need cwd plumbing the markdown pipeline does not-/// carry).+/// `new URL(target, baseUrl)` pass. Standalone callers have no base; the+/// session view supplies its own base through the crate-private Markdown context.#[must_use]pub fn resolve_link_href(token_href: &str) -> String {+ resolve_link_href_with_base(token_href, None)+}++pub(crate) fn resolve_link_href_with_base(token_href: &str, base: Option<&url::Url>) -> String {let target = rewrite_drive_path(token_href);if target.starts_with('#') {return sanitize_control_bytes(target);}match url::Url::parse(&target) {Ok(parsed) => parsed.to_string(),- Err(_) => sanitize_control_bytes(target),+ Err(_) => base+ .and_then(|base| base.join(&target).ok())+ .map_or_else(|| sanitize_control_bytes(target), |url| url.to_string()),}}@@ -666,6 +672,34 @@ mod tests {);}+ #[test]+ fn fallback_capability_keeps_markdown_label_metadata_and_hit_testing() {+ set_hyperlinks_override(Some(false));+ let style = crate::markdown::MarkdownStyle::default();+ let frame = vec",+ &style,+ )];+ let ranges = frame_link_ranges(&frame);+ assert_eq!(link_at(&ranges, 0, 0), Some("https://example.com/"));+ assert_eq!(+ link_at(&ranges, 0, 4),+ None,+ "fallback URL is outside the label's region"+ );+ // The actual render has separate label/body and dim fallback spans.+ // Pin that boundary; this is not a test of splitting a homogeneous+ // writer chunk across a link range in the middle of its bytes.+ let out = paint(&frame, &[b"\x1b[1;1Hdocs", b" (https://example.com)"]);+ assert!(out.contains(&format!(+ "{}docs{}",+ osc8_open("https://example.com/"),+ OSC8_CLOSE+ )));+ assert!(out.ends_with(" (https://example.com)"));+ set_hyperlinks_override(None);+ }+#[test]fn writer_keeps_region_open_across_style_changes() {// The link covers columns 0..4; a color SGR lands between the twodiff --git a/crates/pa-tui/src/markdown.rs b/crates/pa-tui/src/markdown.rsindex 3e5e3904d2..bd35a05fd7 100644--- a/crates/pa-tui/src/markdown.rs+++ b/crates/pa-tui/src/markdown.rs@@ -40,6 +40,7 @@ pub struct MarkdownStyle {/// over the highlight.js grammar). `None` renders every code line uniform in `code_block`/// — the TS no-valid-language fallback, and the quiet thinking theme.pub(crate) syntax: Option<crate::tool_card::highlight::SyntaxPalette>,+ pub(crate) link_base_url: Option<url::Url>,}impl Default for MarkdownStyle {@@ -67,18 +68,17 @@ impl MarkdownStyle {quote_border: theme.fg_style(C::MdQuoteBorder),hr: theme.fg_style(C::MdHr),list_bullet: theme.fg_style(C::MdListBullet),- // The TS source styles `**bold**`/`*ital*`/`~~strike~~` (and the heading taper)- // through chalk; in the deployed TS binary the chalk modifiers never reach the- // wire — only its raw-ANSI colors render (probe vs the installed 0.9.5 binary:- // headings render in mdHeading alone, inline strong/em/strike render plain, inline- // code stays colored). The markers survive parsing but carry no modifier.- bold: Modifier::empty(),- italic: Modifier::empty(),- strikethrough: Modifier::empty(),+ // TS `theme.bold/italic/strikethrough` are chalk; chalk emits its SGR+ // modifiers in any color terminal (its supports-color level is >= 1+ // whenever stdout is a TTY), so emphasis carries real modifiers.+ bold: Modifier::BOLD,+ italic: Modifier::ITALIC,+ strikethrough: Modifier::CROSSED_OUT,code_block_indent: " ".to_string(),syntax: Some(crate::tool_card::highlight::SyntaxPalette::from_theme(theme,)),+ link_base_url: None,}}}@@ -113,7 +113,13 @@ pub fn render_markdown_tagged(if block.sep_blank {lines.push(Vec::new());}- let key = block_cache_key(style_tag, &blocks, i, content_width);+ let key = block_cache_key(+ style_tag,+ &blocks,+ i,+ content_width,+ style.link_base_url.as_ref(),+ );if let Some(cached) = key.as_ref().and_then(|key| cache.0.get(key)) {lines.extend_from_slice(cached);continue;@@ -150,6 +156,7 @@ pub struct MarkdownBlockCache(std::collections::HashMap<BlockKey, Vec<Line>>);/// inputs structurally, so a field added later is covered automatically.#[derive(PartialEq, Eq, Hash)]struct BlockKey {+ link_base_url: Option<url::Url>,style_tag: String,width: usize,kind: BlockKind,@@ -166,6 +173,7 @@ fn block_cache_key(blocks: &[Block],index: usize,width: usize,+ link_base_url: Option<&url::Url>,) -> Option<BlockKey> {if index + 1 == blocks.len() {return None;@@ -173,6 +181,7 @@ fn block_cache_key(let next = blocks.get(index + 1);let block = &blocks[index];Some(BlockKey {+ link_base_url: link_base_url.cloned(),style_tag: style_tag.to_string(),width,kind: block.kind.clone(),@@ -184,17 +193,40 @@ fn block_cache_key(})}+/// One list level: its marker type and the items parsed at one indent.+#[derive(Debug, Clone, PartialEq, Eq, Hash)]+struct ListBlock {+ ordered: bool,+ start: usize,+ items: Vec<ListItem>,+}++/// One item part, in source order: a text line or a nested list.+#[derive(Debug, Clone, PartialEq, Eq, Hash)]+enum ListPart {+ Text(String),+ /// A lazy soft line within nested item text: TS inline newlines resume at column zero.+ LazyText(String),+ List(ListBlock),+}++/// One list item: its parts in source order (text lines and nested lists+/// interleaved — marked walks an item's block tokens the same way).+#[derive(Debug, Clone, PartialEq, Eq, Hash)]+struct ListItem {+ parts: Vec<ListPart>,+}+#[derive(Debug, Clone, PartialEq, Eq, Hash)]enum BlockKind {- Heading,+ Heading {+ depth: usize,+ },Paragraph,Code {lang: Option<String>,},- List {- ordered: bool,- start: usize,- },+ List(ListBlock),Quote,Hr,Table {@@ -213,8 +245,8 @@ struct Block {}fn parse_blocks(text: &str) -> Vec<Block> {- let mut blocks = Vec::new();let src_lines: Vec<&str> = text.lines().collect();+ let mut blocks = Vec::new();let mut i = 0usize;while i < src_lines.len() {let line = src_lines[i];@@ -230,7 +262,12 @@ fn parse_blocks(text: &str) -> Vec<Block> {.take_while(|l| l.trim().is_empty()).count()> 0;- if let Some(fence) = trimmed.strip_prefix("```") {+ if let Some(fence) = trimmed+ .strip_prefix("```")+ .map(|rest| (rest, "```"))+ .or_else(|| trimmed.strip_prefix("~~~").map(|rest| (rest, "~~~")))+ {+ let (fence, closer) = fence;let lang = if fence.is_empty() {None} else {@@ -238,7 +275,7 @@ fn parse_blocks(text: &str) -> Vec<Block> {};let mut code = Vec::new();i += 1;- while i < src_lines.len() && !src_lines[i].trim().starts_with("```") {+ while i < src_lines.len() && !src_lines[i].trim().starts_with(closer) {code.push(src_lines[i].to_string());i += 1;}@@ -251,11 +288,14 @@ fn parse_blocks(text: &str) -> Vec<Block> {continue;}let hashes = trimmed.chars().take_while(|&c| c == '#').count();- if hashes > 0 && trimmed.len() > hashes && trimmed.as_bytes()[hashes] == b' ' {+ if heading_marker(trimmed) && hashes <= 6 {+ let content = trimmed+ .get(hashes + 1..)+ .map_or_else(String::new, std::string::ToString::to_string);blocks.push(Block {- kind: BlockKind::Heading,+ kind: BlockKind::Heading { depth: hashes },sep_blank,- lines: vec![trimmed[hashes + 1..].to_string()],+ lines: vec![content],});i += 1;continue;@@ -269,20 +309,25 @@ fn parse_blocks(text: &str) -> Vec<Block> {i += 1;continue;}- if let Some(q) = trimmed.strip_prefix('>') {- let mut qlines = vec![q.trim_start().to_string()];- i += 1;- while i < src_lines.len()- && !src_lines[i].trim().is_empty()- && src_lines[i].trim().starts_with('>')- {- qlines.push(- src_lines[i]- .trim()- .trim_start_matches('>')- .trim_start()- .to_string(),- );+ if trimmed.starts_with('>') {+ // One quote block: `>`-prefixed lines plus marked's lazy continuation —+ // following non-blank lines that do not start another block stay inside+ // the quote (the blockquote token consumes until a blank line).+ let mut qlines = Vec::new();+ while i < src_lines.len() {+ let t = src_lines[i].trim();+ if t.is_empty() {+ break;+ }+ if let Some(q) = t.strip_prefix('>') {+ qlines.push(q.trim_start().to_string());+ i += 1;+ continue;+ }+ if interrupts_paragraph(t, src_lines.get(i + 1)) {+ break;+ }+ qlines.push(t.to_string());i += 1;}blocks.push(Block {@@ -292,34 +337,26 @@ fn parse_blocks(text: &str) -> Vec<Block> {});continue;}- if let Some(marker) = list_marker(trimmed) {- let (ordered, start) = marker;- let mut items: Vec<String> = Vec::new();- let mut item = trimmed[marker_width(trimmed)..].to_string();+ if let Some((ordered, start)) = list_marker(trimmed) {+ let indent = line.len() - line.trim_start().len();+ let first = ListItem {+ parts: vec![ListPart::Text(trimmed[marker_width(trimmed)..].to_string())],+ };+ let content_col = indent + marker_width(trimmed);i += 1;- while i < src_lines.len() {- let l = src_lines[i];- let t = l.trim();- if t.is_empty() {- break;- }- if list_marker(t).is_some() {- items.push(std::mem::take(&mut item));- item = t[marker_width(t)..].to_string();- i += 1;- } else if l.starts_with(" ") || l.starts_with('\t') {- item.push(' ');- item.push_str(t);- i += 1;- } else {- break;- }- }- items.push(item);+ let list = parse_list_level(+ &src_lines,+ &mut i,+ first,+ ordered,+ start,+ ListNesting { indent, depth: 0 },+ content_col,+ );blocks.push(Block {- kind: BlockKind::List { ordered, start },+ kind: BlockKind::List(list),sep_blank,- lines: items,+ lines: Vec::new(),});continue;}@@ -347,20 +384,23 @@ fn parse_blocks(text: &str) -> Vec<Block> {while i < src_lines.len() {let l = src_lines[i];let t = l.trim();- if t.is_empty()- || t.starts_with("```")- || t.starts_with('>')- || t.starts_with('#')- || list_marker(t).is_some()- || is_hr(t)- || crate::markdown_table::is_table_start(t, src_lines.get(i + 1))- {+ if t.is_empty() || interrupts_paragraph(t, src_lines.get(i + 1)) {break;}para_lines.push(t.to_string());last_raw = l;i += 1;}+ // A trailing unescaped backslash is a hard break (marked consumes it with+ // the newline); the paragraph's last line has no following row, so its+ // backslash stays literal.+ let hard_break_count = para_lines.len().saturating_sub(1);+ for line in para_lines.iter_mut().take(hard_break_count) {+ let backslashes = line.chars().rev().take_while(|&c| c == '\\').count();+ if backslashes % 2 == 1 {+ line.pop();+ }+ }let last = para_lines.last_mut().expect("paragraph has a line");last.push_str(&last_raw[last_raw.trim_end().len()..]);blocks.push(Block {@@ -372,6 +412,13 @@ fn parse_blocks(text: &str) -> Vec<Block> {blocks}+/// A heading line start: `#`s followed by a space (the depth cap lives where the+/// heading is parsed).+pub(crate) fn heading_marker(t: &str) -> bool {+ let hashes = t.chars().take_while(|&c| c == '#').count();+ hashes > 0 && (t.len() == hashes || t.as_bytes()[hashes] == b' ')+}+pub(crate) fn is_hr(t: &str) -> bool {let chars: Vec<char> = t.chars().filter(|&c| c != ' ').collect();(chars.len() >= 3)@@ -379,6 +426,19 @@ pub(crate) fn is_hr(t: &str) -> bool {&& (chars[0] == '-' || chars[0] == '*' || chars[0] == '_')}+/// A line that interrupts a paragraph (marked: a new block never continues one) —+/// the shared stop predicate for paragraph parsing and a blockquote's lazy+/// continuation.+fn interrupts_paragraph(t: &str, next: Option<&&str>) -> bool {+ t.starts_with("```")+ || t.starts_with("~~~")+ || t.starts_with('>')+ || t.starts_with('#')+ || list_marker(t).is_some()+ || is_hr(t)+ || crate::markdown_table::is_table_start(t, next)+}+pub(crate) fn list_marker(t: &str) -> Option<(bool, usize)> {if let Some(rest) = t.strip_prefix("- ") {let _ = rest;@@ -408,6 +468,162 @@ fn marker_width(t: &str) -> usize {}}+/// The bullet for one item of a list level (TS `renderList`: the token's start+/// number plus the item index, so sibling markers' own numbers are ignored).+fn list_bullet(ordered: bool, start: usize, index: usize) -> String {+ if ordered {+ format!("{}. ", start + index)+ } else {+ "- ".to_string()+ }+}++/// One list level's row geometry: the column its rows indent to (the level's own+/// indent plus the accumulated `indent + " "` frames of its ancestors, TS+/// `renderList`), and the indent its children's rows inherit from that frame.+fn list_level_geometry(depth: usize, outer: usize) -> (usize, usize) {+ (outer + 2 * depth, outer + 2 * depth + 2)+}++/// One item line's wrap budget: the first line shares its row with the level+/// indent and the bullet; later lines carry `indent + " "`.+fn list_line_budget(+ width: usize,+ indent_len: usize,+ bullet_width: usize,+ first_line: bool,+) -> usize {+ let used = if first_line {+ indent_len + bullet_width+ } else {+ indent_len + 2+ };+ width.saturating_sub(used).max(1)+}++#[derive(Clone, Copy)]+struct ListNesting {+ indent: usize,+ depth: usize,+}++/// Parse one list level (marked's list rule): marker lines of the level's own+/// type start items (a marker line indented between the level indent and the+/// current item's content column is absorbed as a sibling — marked renumbers+/// by index), blank lines stay internal (marked's loose list), indented+/// non-marker lines continue the current item as their own lines, and a marker+/// line at or past the content column opens a nested level. Anything else ends+/// the level; a trailing run of blanks is left for the gap before the next+/// block.+fn parse_list_level(+ src_lines: &[&str],+ i: &mut usize,+ first: ListItem,+ ordered: bool,+ start: usize,+ nesting: ListNesting,+ first_content_col: usize,+) -> ListBlock {+ let ListNesting { indent, depth } = nesting;+ let mut items = vec![first];+ // The column the current item's content starts at: a line indented to it is+ // the item's continuation or a nested list; a marker line between the level+ // indent and it is absorbed as a sibling (marked renumbers by index).+ let mut content_col = first_content_col;+ let mut pending_blank: Option<usize> = None;+ while *i < src_lines.len() {+ let line = src_lines[*i];+ let t = line.trim();+ let line_indent = line.len() - line.trim_start().len();+ if t.is_empty() {+ pending_blank.get_or_insert(*i);+ *i += 1;+ continue;+ }+ if let Some((child_ordered, child_start)) = list_marker(t) {+ let marker_w = marker_width(t);+ if line_indent >= content_col {+ // Keep untrusted message content from building an unbounded recursive tree.+ // Beyond this level the source marker stays literal; count, paint and Drop+ // therefore share the same bounded tree without losing the item text.+ if depth >= 64 {+ items+ .last_mut()+ .expect("the level has its first item")+ .parts+ .push(ListPart::Text(t.to_string()));+ pending_blank = None;+ *i += 1;+ continue;+ }+ let child_first = ListItem {+ parts: vec![ListPart::Text(t[marker_w..].to_string())],+ };+ *i += 1;+ let child = parse_list_level(+ src_lines,+ i,+ child_first,+ child_ordered,+ child_start,+ ListNesting {+ indent: line_indent,+ depth: depth + 1,+ },+ line_indent + marker_w,+ );+ items+ .last_mut()+ .expect("the level has its first item")+ .parts+ .push(ListPart::List(child));+ pending_blank = None;+ continue;+ }+ if child_ordered == ordered+ && (line_indent == indent || (line_indent > indent && line_indent < content_col))+ {+ items.push(ListItem {+ parts: vec![ListPart::Text(t[marker_w..].to_string())],+ });+ content_col = line_indent + marker_w;+ pending_blank = None;+ *i += 1;+ continue;+ }+ break;+ }+ if line_indent >= content_col {+ items+ .last_mut()+ .expect("the level has its first item")+ .parts+ .push(ListPart::Text(t.to_string()));+ pending_blank = None;+ *i += 1;+ continue;+ }+ if depth > 0 && pending_blank.is_none() && !interrupts_paragraph(t, src_lines.get(*i + 1)) {+ items+ .last_mut()+ .expect("the level has its first item")+ .parts+ .push(ListPart::LazyText(t.to_string()));+ *i += 1;+ continue;+ }+ break;+ }+ if let Some(blank) = pending_blank {+ *i = blank;+ }+ ListBlock {+ ordered,+ start,+ items,+ }+}+/// The fence languages the port highlights. TS `highlightCode` validates through highlight.js/// `getLanguage(name)`, which lowercases and matches the grammar's registered names and aliases/// (python registers `python` with `py`, `gyp`, `ipython`). `lang` here is marked's whole trimmed@@ -475,18 +691,33 @@ fn code_rows(block: &Block, lang: Option<&str>, style: &MarkdownStyle) -> Vec<Li/// collide) tapers to the heading color like any other span. Shared by/// the paint path and the row count, so a wrapped heading counts/// exactly what it paints.-fn heading_spans(text: &str, style: &MarkdownStyle) -> Vec<Span> {- let (mut spans, url_slots) = inline::render_inline_with_url_slots(text, style);+fn heading_spans(text: &str, depth: usize, style: &MarkdownStyle) -> Vec<Span> {+ let (mut spans, url_slots, code_slots) = inline::render_inline_tracked(text, style);+ // TS heading styleFn: h1 bold+underline, h2/h3 bold, h4 bold+italic, h5/h6 italic,+ // all over the heading color. The heading modifiers close around an inline-code+ // span and a link's URL bracket — both keep their own colors (tracked by origin);+ // an underlined link label keeps the underline affordance.+ let taper = style.heading.add_modifier(match depth {+ 1 => Modifier::BOLD | Modifier::UNDERLINED,+ 2 | 3 => Modifier::BOLD,+ 4 => Modifier::BOLD | Modifier::ITALIC,+ _ => Modifier::ITALIC,+ });// The slot indices arrive ascending, so one cursor walks them in// step with the span iteration — a link-heavy heading stays linear.let mut url_slot = 0;+ let mut code_slot = 0;for (i, s) in spans.iter_mut().enumerate() {if url_slots.get(url_slot) == Some(&i) {url_slot += 1;continue;}+ if code_slots.get(code_slot) == Some(&i) {+ code_slot += 1;+ continue;+ }let underlined = s.style.add_modifier.contains(Modifier::UNDERLINED);- s.style = style.heading;+ s.style = taper;if underlined {s.style = s.style.add_modifier(Modifier::UNDERLINED);}@@ -503,14 +734,10 @@ fn render_block() {let blank_after = |exclude_lists| geometry::blank_after(next, exclude_lists);match &block.kind {- BlockKind::Heading => {- // The TS source tapers headings by level (h1 bold+underline, h2/h3 bold, h4- // bold+italic, h5/h6 italic), all through chalk; in the deployed TS binary the- // chalk modifiers never reach the wire, so every level renders in the heading- // color alone (probe vs the installed 0.9.5 binary).+ BlockKind::Heading { depth } => {let text = block.lines.first().cloned().unwrap_or_default();- let spans = heading_spans(&text, style);- wrap_spans(&spans, width, style.heading, out);+ let spans = heading_spans(&text, *depth, style);+ wrap_spans(&spans, width, out);if blank_after(false) {out.push(Vec::new());}@@ -519,8 +746,8 @@ fn render_block(// Each soft-break line renders and wraps on its own (TS's paragraph token// carries the newlines through the width pass).for text in &block.lines {- let spans = render_inline(text, style);- wrap_spans(&spans, width, style.body, out);+ let spans = inline::render_inline_tracked(text, style).0;+ wrap_spans(&spans, width, out);}if blank_after(true) {out.push(Vec::new());@@ -536,39 +763,42 @@ fn render_block(// (wrapTextWithAnsi), so a continuation row starts at column 0// with no re-added indent.for row in code_rows(block, lang.as_deref(), style) {- wrap_spans(&row, width, style.code_block, out);+ wrap_spans(&row, width, out);}if blank_after(false) {out.push(Vec::new());}}- BlockKind::List { ordered, start } => {- for (i, item) in block.lines.iter().enumerate() {- let bullet = if *ordered {- format!("{}. ", start + i)- } else {- "- ".to_string()- };- let spans = render_inline(item, style);- wrap_list_item(&bullet, &spans, width, style, out);- }+ BlockKind::List(list) => {+ render_list_level(list, 0, 0, width, style, out);if blank_after(true) {out.push(Vec::new());}}BlockKind::Quote => {+ // TS renders the whole quote content in the quote color and italic+ // (its quoteStyle re-applies after every inline reset); inline-code+ // spans and link URL brackets keep their own colors, and emphasis+ // modifiers ride on top.for line in &block.lines {- let spans = render_inline(line, style);+ let (spans, url_slots, code_slots) = inline::render_inline_tracked(line, style);let mut quote_spans: Vec<Span> = Vec::new();- for mut s in spans {- s.style = style.quote.patch(s.style);+ for (i, mut s) in spans.into_iter().enumerate() {+ if s.content.is_empty() {+ continue;+ }+ s.style = s.style.add_modifier(Modifier::ITALIC);+ if !url_slots.contains(&i) && !code_slots.contains(&i) {+ s.style.fg = style.quote.fg;+ }quote_spans.push(s);}wrap_quote("e_spans, width, style, out);}}BlockKind::Hr => {- let bar: String = "─".repeat(width.max(1));+ // TS hr: `"─".repeat(Math.min(width, 80))`.+ let bar: String = "─".repeat(width.min(80));out.push(vec![Span::styled(bar, style.hr)]);}BlockKind::Table { header, rows } => {@@ -580,10 +810,122 @@ fn render_block(}}+/// One wrap token: a run of spaces, or a word run whose pieces may cross span+/// boundaries — TS `splitIntoTokensWithAnsi` never splits a word at an ANSI+/// boundary, so a styled span's trailing punctuation stays glued to the word.+#[derive(Debug)]+pub(super) enum WrapToken {+ Gap(String, Style),+ Word(Vec<(String, Style)>),+}++/// Split styled spans into wrap tokens: words and space runs, with words that+/// touch across a span boundary glued into one token.+#[must_use]+pub(super) fn tokenize_spans(spans: &[Span]) -> Vec<WrapToken> {+ let mut tokens: Vec<WrapToken> = Vec::new();+ for span in spans {+ let mut word = String::new();+ for ch in span.content.chars() {+ if ch == ' ' {+ if !word.is_empty() {+ push_word(&mut tokens, std::mem::take(&mut word), span.style);+ }+ match tokens.last_mut() {+ Some(WrapToken::Gap(text, _)) => text.push(' '),+ _ => tokens.push(WrapToken::Gap(" ".to_string(), span.style)),+ }+ } else {+ word.push(ch);+ }+ }+ if !word.is_empty() {+ push_word(&mut tokens, word, span.style);+ }+ }+ tokens+}++/// Place one piece of a glued word unit, breaking overlong pieces across rows;+/// escape sequences copy through atomically at zero width (OSC 8 sequences must+/// never split mid-sequence).+///+/// The remaining width is tracked arithmetically: measured once (the caller's+/// `w`), decremented by each row's emitted width, with `rest` sliced — never+/// re-measured per row (an unbroken token longer than the wrap width would+/// otherwise wrap in `O(token_len * rows)` time). For content whose per-char+/// widths sum to its grapheme width — every printable-ASCII/escape/tab token,+/// the catastrophic class — the arithmetic is exact; a row split inside a+/// multi-char grapheme cluster is the one non-additive case, so a tentative+/// exit is confirmed against one true measure before the leftover is pushed+/// (the correctness backstop, never the hot path).+fn push_word_piece(+ text: &str,+ style: Style,+ width: usize,+ col: &mut usize,+ out: &mut geometry::WrapOutput<'_>,+) {+ let mut rest: &str = text;+ let mut rest_width = str_width(rest);+ loop {+ if rest_width + *col <= width {+ if str_width(rest) + *col <= width {+ break;+ }+ // A non-additive cluster split drifted the arithmetic: re-sync from the+ // true measure and keep breaking.+ rest_width = str_width(rest);+ }+ let mut take = String::new();+ let mut tw = 0usize;+ let mut taken = 0usize;+ while taken < rest.len() {+ if let Some(len) = crate::width::escape_len(&rest[taken..]) {+ take.push_str(&rest[taken..taken + len]);+ taken += len;+ continue;+ }+ let c = rest[taken..].chars().next().expect("char at boundary");+ let cw = crate::width::char_width(c);+ if tw + cw + *col > width {+ break;+ }+ take.push(c);+ tw += cw;+ taken += c.len_utf8();+ }+ if take.is_empty() {+ // The piece cannot start on this row: open a fresh row and retry+ // it (a glued word never overflows its row; the count path+ // shares this break).+ if *col > 0 {+ out.finish_row(/*trim*/ false);+ *col = 0;+ continue;+ }+ break;+ }+ out.push(&take, style);+ out.finish_row(/*trim*/ false);+ *col = 0;+ rest = &rest[taken..];+ rest_width -= tw;+ }+ *col += str_width(rest);+ out.push(rest, style);+}++fn push_word(tokens: &mut Vec<WrapToken>, text: String, style: Style) {+ match tokens.last_mut() {+ Some(WrapToken::Word(pieces)) => pieces.push((text, style)),+ _ => tokens.push(WrapToken::Word(vec![(text, style)])),+ }+}+/// Wrap styled spans to `width`. Words break at whitespace; leading spaces are/// dropped after a wrap break. Adjacent same-style output pieces merge.-pub fn wrap_spans(spans: &[Span], width: usize, base: Style, out: &mut Vec<Line>) {- let _ = base;+pub fn wrap_spans(spans: &[Span], width: usize, out: &mut Vec<Line>) {wrap_spans_into(spans, width, &mut geometry::WrapOutput::render(out));}@@ -611,122 +953,159 @@ fn wrap_spans_into(spans: &[Span], width: usize, out: &mut geometry::WrapOutput<out.finish_row(/*trim*/ false);return;}- // tokens: (text, style); alternating words and whitespace-run gaps. TS- // `splitIntoTokensWithAnsi` keeps each whitespace RUN whole (a run at a span boundary- // joins the previous gap token), never collapsing it to a single space.- let mut tokens: Vec<(String, Style)> = Vec::new();- for span in spans {- let mut word = String::new();- for ch in span.content.chars() {- if ch == ' ' {- if !word.is_empty() {- tokens.push((std::mem::take(&mut word), span.style));- }- match tokens.last_mut() {- Some((text, _)) if text.chars().all(|c| c == ' ') => text.push(' '),- _ => tokens.push((" ".to_string(), span.style)),- }- } else {- word.push(ch);- }- }- if !word.is_empty() {- tokens.push((word, span.style));- }- }+ let tokens = tokenize_spans(spans);let mut col = 0usize;let mut i = 0usize;while i < tokens.len() {- let (text, style) = &tokens[i];- let w = str_width(text);- if col + w > width && out.has_content {- // A wrapped row never carries its trailing gap: TS wrapTextWithAnsi drops the boundary- // space, so the styled content ends at the last word and the plain padding follows.- out.finish_row(/*trim*/ true);- col = 0;- // drop leading whitespace at the new line start- if text.trim().is_empty() {- i += 1;- continue;- }- }- // break overlong words; escape sequences copy through atomically- // at zero width (OSC 8 sequences must never split mid-sequence)- let style = *style;- let mut rest: &str = text.as_str();- // The remaining width is tracked arithmetically: measured once (the caller's `w`),- // decremented by each row's emitted width, with `rest` sliced — never re-measured per row- // (an unbroken token longer than the wrap width would otherwise wrap in O(token_len * rows)- // time). For content whose per-char widths sum to its grapheme width — every- // printable-ASCII/escape/tab token, the catastrophic class — the arithmetic is exact; a row- // split inside a multi-char grapheme cluster is the one non-additive case, so a tentative- // exit is confirmed against one true measure before the leftover is pushed (the correctness- // backstop, never the hot path).- let mut rest_width = w;- loop {- if rest_width + col <= width {- if str_width(rest) + col <= width {- break;+ match &tokens[i] {+ WrapToken::Word(pieces) => {+ // The glued unit wraps as one word: it moves to the next row+ // together, never splitting at a span boundary.+ let w: usize = pieces.iter().map(|(t, _)| str_width(t)).sum();+ if col + w > width && out.has_content {+ // A wrapped row never carries its trailing gap: TS wrapTextWithAnsi drops the+ // boundary space, so the styled content ends at the last word.+ out.finish_row(/*trim*/ true);+ col = 0;}- // A non-additive cluster split drifted the arithmetic: re-sync from the- // true measure and keep breaking.- rest_width = str_width(rest);- }- let mut take = String::new();- let mut tw = 0usize;- let mut taken = 0usize;- while taken < rest.len() {- if let Some(len) = crate::width::escape_len(&rest[taken..]) {- take.push_str(&rest[taken..taken + len]);- taken += len;- continue;- }- let c = rest[taken..].chars().next().expect("char at boundary");- let cw = crate::width::char_width(c);- if tw + cw + col > width {- break;+ for (piece, style) in pieces {+ push_word_piece(piece, *style, width, &mut col, out);}- take.push(c);- tw += cw;- taken += c.len_utf8();}- if take.is_empty() {- break;+ WrapToken::Gap(text, style) => {+ let w = str_width(text);+ if col + w > width && out.has_content {+ // A wrapped row never carries its trailing gap: TS wrapTextWithAnsi drops the+ // boundary space, so the styled content ends at the last word.+ out.finish_row(/*trim*/ true);+ col = 0;+ // drop leading whitespace at the new line start+ if text.trim().is_empty() {+ i += 1;+ continue;+ }+ }+ push_word_piece(text, *style, width, &mut col, out);}- out.push(&take, style);- out.finish_row(/*trim*/ false);- col = 0;- rest = &rest[taken..];- rest_width -= tw;- }- col += str_width(rest);- out.push(rest, style);+ }i += 1;}out.finish_row(/*trim*/ false);}-fn wrap_list_item(- bullet: &str,- spans: &[Span],+/// The row count of one list level — the same geometry [`render_list_level`]+/// paints (level indent, bullet widths, budgets), counted without collecting+/// rows. An item with no lines still renders its bare bullet row.+fn list_row_count(+ list: &ListBlock,+ depth: usize,+ outer: usize,+ width: usize,+ style: &MarkdownStyle,+) -> usize {+ let (indent_len, child_outer) = list_level_geometry(depth, outer);+ let mut count = 0;+ for (index, item) in list.items.iter().enumerate() {+ let bullet_width = str_width(&list_bullet(list.ordered, list.start, index));+ let mut item_count = 0;+ let mut first_part = true;+ for part in &item.parts {+ match part {+ ListPart::Text(text) => {+ let budget = list_line_budget(width, indent_len, bullet_width, first_part);+ item_count += wrapped_span_count(&render_inline(text, style), budget);+ first_part = false;+ }+ ListPart::List(child) => {+ item_count += list_row_count(child, depth + 1, child_outer, width, style);+ first_part = false;+ }+ ListPart::LazyText(text) => {+ item_count += wrapped_span_count(&render_inline(text, style), width);+ first_part = false;+ }+ }+ }+ count += item_count.max(1);+ }+ count+}++/// Render one list level (TS `renderList`): every item line beyond the first+/// prefixes with `indent + " "` (each nesting level accumulates one frame), the+/// item's first line carries the level's bullet, wrapped rows keep the+/// bullet-width continuation indent, and empty items render the bare bullet.+fn render_list_level(+ list: &ListBlock,+ depth: usize,+ outer: usize,width: usize,style: &MarkdownStyle,out: &mut Vec<Line>,) {- let bullet_width = str_width(bullet);- let content_width = width.saturating_sub(bullet_width).max(1);- let mut wrapped: Vec<Line> = Vec::new();- wrap_spans(spans, content_width, style.body, &mut wrapped);- for (i, line) in wrapped.into_iter().enumerate() {- if i == 0 {- let mut l = vec![Span::styled(bullet.to_string(), style.list_bullet)];- l.extend(line);- out.push(l);+ let (indent_len, child_outer) = list_level_geometry(depth, outer);+ let indent = " ".repeat(indent_len);+ for (index, item) in list.items.iter().enumerate() {+ let bullet = list_bullet(list.ordered, list.start, index);+ let bullet_width = str_width(&bullet);+ let mut item_rows: Vec<Line> = Vec::new();+ // The item's parts walk in source order; the first part carries the+ // level's bullet (a nested list renders into the same rows before any+ // later text lines, matching marked's token order).+ let mut first_part = true;+ for part in &item.parts {+ match part {+ ListPart::Text(text) => {+ let spans = inline::render_inline_tracked(text, style).0;+ let mut wrapped: Vec<Line> = Vec::new();+ if first_part {+ let budget = list_line_budget(width, indent_len, bullet_width, true);+ wrap_spans(&spans, budget, &mut wrapped);+ for (k, line) in wrapped.into_iter().enumerate() {+ let mut row: Line = Vec::new();+ if !indent.is_empty() {+ row.push(Span::raw(indent.clone()));+ }+ if k == 0 {+ row.push(Span::styled(bullet.clone(), style.list_bullet));+ } else {+ row.push(Span::raw(" ".repeat(bullet_width)));+ }+ row.extend(line);+ item_rows.push(row);+ }+ } else {+ let sub_indent = format!("{indent} ");+ let budget = list_line_budget(width, indent_len, bullet_width, false);+ wrap_spans(&spans, budget, &mut wrapped);+ for line in wrapped {+ let mut row = vec![Span::raw(sub_indent.clone())];+ row.extend(line);+ item_rows.push(row);+ }+ }+ first_part = false;+ }+ ListPart::List(child) => {+ render_list_level(child, depth + 1, child_outer, width, style, &mut item_rows);+ first_part = false;+ }+ ListPart::LazyText(text) => {+ wrap_spans(&render_inline(text, style), width, &mut item_rows);+ first_part = false;+ }+ }+ }+ if item_rows.is_empty() {+ let mut row: Line = Vec::new();+ if !indent.is_empty() {+ row.push(Span::raw(indent.clone()));+ }+ row.push(Span::styled(bullet, style.list_bullet));+ out.push(row);} else {- let mut l = vec![Span::styled(" ".repeat(bullet_width), style.body)];- l.extend(line);- out.push(l);+ out.extend(item_rows);}}}@@ -734,9 +1113,9 @@ fn wrap_list_item(fn wrap_quote(spans: &[Span], width: usize, style: &MarkdownStyle, out: &mut Vec<Line>) {let quote_width = width.saturating_sub(2).max(1);let mut wrapped: Vec<Line> = Vec::new();- wrap_spans(spans, quote_width, style.quote, &mut wrapped);+ wrap_spans(spans, quote_width, &mut wrapped);for line in wrapped {- let mut l = vec![Span::styled("▐ ", style.quote_border)];+ let mut l = vec![Span::styled("│ ", style.quote_border)];l.extend(line);out.push(l);}diff --git a/crates/pa-tui/src/markdown/geometry.rs b/crates/pa-tui/src/markdown/geometry.rsindex 93bf6ac90a..ee382ca1bc 100644--- a/crates/pa-tui/src/markdown/geometry.rs+++ b/crates/pa-tui/src/markdown/geometry.rs@@ -60,7 +60,7 @@ impl<'a> WrapOutput<'a> {pub(super) fn blank_after(next: Option<&Block>, exclude_lists: bool) -> bool {match next {Some(next) => {- !(next.sep_blank || exclude_lists && matches!(next.kind, BlockKind::List { .. }))+ !(next.sep_blank || exclude_lists && matches!(next.kind, BlockKind::List(..)))}None => false,}@@ -91,7 +91,15 @@ pub(crate) fn markdown_row_count_tagged(total += usize::from(block.sep_blank);// The key build clones the block's lines, so an empty cache skips it.let cached = (!cache.0.is_empty())- .then(|| block_cache_key(style_tag, &blocks, index, width))+ .then(|| {+ block_cache_key(+ style_tag,+ &blocks,+ index,+ width,+ style.link_base_url.as_ref(),+ )+ }).flatten().and_then(|key| cache.0.get(&key));if let Some(rows) = cached {@@ -99,9 +107,9 @@ pub(crate) fn markdown_row_count_tagged(continue;}let count = match &block.kind {- BlockKind::Heading => {+ BlockKind::Heading { depth } => {let text = block.lines.first().cloned().unwrap_or_default();- wrapped_span_count(&heading_spans(&text, style), width)+ wrapped_span_count(&heading_spans(&text, *depth, style), width)+ usize::from(blank_after(next, false))}BlockKind::Hr => 1,@@ -120,20 +128,9 @@ pub(crate) fn markdown_row_count_tagged(.sum::<usize>()+ usize::from(blank_after(next, true))}- BlockKind::List { ordered, start } => {- let mut count = 0;- for (index, text) in block.lines.iter().enumerate() {- let bullet = if *ordered {- format!("{}. ", start + index)- } else {- "- ".to_owned()- };- let content_width = width- .saturating_sub(crate::width::str_width(&bullet))- .max(1);- count += wrapped_span_count(&render_inline(text, style), content_width);- }- count + usize::from(blank_after(next, true))+ BlockKind::List(list) => {+ crate::markdown::list_row_count(list, 0, 0, width, style)+ + usize::from(blank_after(next, true))}BlockKind::Quote => block.linesdiff --git a/crates/pa-tui/src/markdown/inline.rs b/crates/pa-tui/src/markdown/inline.rsindex c4bf7dc9db..aec9bc59d6 100644--- a/crates/pa-tui/src/markdown/inline.rs+++ b/crates/pa-tui/src/markdown/inline.rs@@ -4,27 +4,34 @@ use ratatui::style::Modifier;/// Inline rendering: bold, italic, strikethrough, code, links.#[must_use]pub fn render_inline(text: &str, style: &MarkdownStyle) -> Line {- render_inline_with_url_slots(text, style).0+ render_inline_tracked(text, style).0}-/// The same inline render, plus the `link_url` slot indices: which spans of the-/// returned line carry a link's `[url]` bracket, in ascending span order.-/// Style-tapering callers preserve those spans by origin — a span that merely-/// renders in the `link_url` style is not a slot.+/// The same inline render, plus origin tracking: which spans of the returned line+/// carry a link's `(url)` bracket (`url_slots`) and which are inline-code spans+/// (`code_slots`), in ascending span order. Style-tapering callers (headings,+/// quotes) preserve those spans by origin — a span that merely renders in the same+/// style is not a slot.#[must_use]-pub(crate) fn render_inline_with_url_slots(+pub(crate) fn render_inline_tracked(text: &str,style: &MarkdownStyle,-) -> (Line, Vec<usize>) {+) -> (Line, Vec<usize>, Vec<usize>) {render_inline_ctx(text, style, false)}/// `in_link` mirrors marked's `lexer.state.inLink`: the gfm bare-url rule is/// skipped inside a link label's tokens (the angle `autolink` rule is not).-/// Returns the `[url]` bracket indices, remapped across every recursive extend.-fn render_inline_ctx(text: &str, style: &MarkdownStyle, in_link: bool) -> (Line, Vec<usize>) {+/// Returns the `(url)`-bracket and inline-code span indices, remapped across every+/// recursive extend.+fn render_inline_ctx(+ text: &str,+ style: &MarkdownStyle,+ in_link: bool,+) -> (Line, Vec<usize>, Vec<usize>) {let mut spans: Vec<Span> = Vec::new();let mut url_slots: Vec<usize> = Vec::new();+ let mut code_slots: Vec<usize> = Vec::new();let bytes: Vec<char> = text.chars().collect();// Byte offset per char index: the autolink rules run on a slice of the original text, so a// candidate-heavy line stays linear in its attempts.@@ -64,6 +71,13 @@ fn render_inline_ctx(text: &str, style: &MarkdownStyle, in_link: bool) -> (Line,while i < bytes.len() {let c = bytes[i];+ // marked's escape rule: a backslash before ASCII punctuation consumes the+ // backslash, so the punctuation never opens a construct.+ if c == '\\' && i + 1 < bytes.len() && bytes[i + 1].is_ascii_punctuation() {+ buf.push(bytes[i + 1]);+ i += 2;+ continue;+ }// inline codeif c == '`' {let mut j = i + 1;@@ -74,12 +88,13 @@ fn render_inline_ctx(text: &str, style: &MarkdownStyle, in_link: bool) -> (Line,}if j < bytes.len() {flush!();+ code_slots.push(spans.len());spans.push(Span::styled(code, style.code));i = j + 1;continue;}}- // links [text](url)+ // links: [text](url)if c == '[' {let mut j = i + 1;let mut label = String::new();@@ -87,84 +102,84 @@ fn render_inline_ctx(text: &str, style: &MarkdownStyle, in_link: bool) -> (Line,label.push(bytes[j]);j += 1;}- if j + 1 < bytes.len() && bytes[j] == ']' && bytes[j + 1] == '(' {- let mut k = j + 2;- let mut url = String::new();- // CommonMark link destination: parens ride as a balanced pair (the- // destination ends at its own `)`, not the first one inside); an- // escaped char rides verbatim and never counts toward the balance.- let mut paren_depth = 0usize;- while k < bytes.len() {- if bytes[k] == ')' && paren_depth == 0 {- break;- }- if bytes[k] == '\\' && k + 1 < bytes.len() {- url.push(bytes[k]);- k += 1;- url.push(bytes[k]);- k += 1;- continue;- }- if bytes[k] == '(' {- paren_depth += 1;- } else if bytes[k] == ')' {- paren_depth -= 1;- }- url.push(bytes[k]);- k += 1;+ let mut parsed = None;+ if j < bytes.len() && bytes[j] == ']' && j + 1 < bytes.len() && bytes[j + 1] == '(' {+ parsed = link_destination(&bytes, j + 2);+ }+ if let Some((url, k)) = parsed {+ // An image (``): marked's image token falls through to the+ // alt text in the TS renderer — no `!`, no link affordance, no bracket.+ // The marker is read from the SOURCE: an escaped `\!` is a literal+ // bang, not an image marker (an odd backslash run escapes it).+ let image = image_marker_before(&bytes, i);+ let mut modifier = Modifier::empty();+ if bold {+ modifier |= style.bold;+ }+ if italic {+ modifier |= style.italic;}- if k < bytes.len() {+ if image {+ buf.pop();flush!();- let mut modifier = Modifier::empty();- if bold {- modifier |= style.bold;- }- if italic {- modifier |= style.italic;- }- // The label renders underlined (the standard- // terminal link affordance); `modifier` carries the- // emphasis context.- let href = crate::hyperlinks::resolve_link_href(&url);- let (mut label_spans, mut label_slots) = render_inline_ctx(&label, style, true);- for s in &mut label_spans {- s.style = s.style.add_modifier(modifier | Modifier::UNDERLINED);- }- if crate::hyperlinks::hyperlinks_enabled() {- // OSC 8: the label stays clickable (TS `hyperlink()`).- let open = crate::hyperlinks::osc8_open(&href);- if let Some(first) = label_spans.first_mut() {- first.content.insert_str(0, &open);- }- if let Some(last) = label_spans.last_mut() {- last.content.push_str(crate::hyperlinks::OSC8_CLOSE);- }- }- let offset = spans.len();- for slot in &mut label_slots {- *slot += offset;- }- url_slots.append(&mut label_slots);- spans.extend(label_spans);- // The URL rides after the wrap (the OSC 8 region covers the label- // only), in the dim `link_url` slot unless the label already is- // the URL (mailto stripped for the comparison).- let comparison = url.strip_prefix("mailto:").unwrap_or(url.as_str());- if label != url && label != comparison {- // The bracket renders the destination as visible text, so it- // gets the same control-byte hardening as the OSC 8 target: a- // smuggled escape byte can never re-enter the terminal live.- let shown = crate::hyperlinks::sanitize_control_bytes(url.clone());- url_slots.push(spans.len());- spans.push(Span::styled(format!(" [{shown}]"), style.link_url));- }+ spans.push(Span::styled(label, base.add_modifier(modifier)));i = k + 1;continue;}+ flush!();+ // The label renders underlined (the standard+ // terminal link affordance); `modifier` carries the+ // emphasis context.+ let href = crate::hyperlinks::resolve_link_href_with_base(+ &url,+ style.link_base_url.as_ref(),+ );+ let (label_spans, label_slots, label_codes) =+ render_inline_ctx(&label, style, true);+ let mut label_spans = label_spans;+ {+ // OSC 8: the label stays clickable (TS `hyperlink()`).+ let open = crate::hyperlinks::osc8_open(&href);+ if let Some(first) = label_spans.first_mut() {+ first.content.insert_str(0, &open);+ }+ if let Some(last) = label_spans.last_mut() {+ last.content.push_str(crate::hyperlinks::OSC8_CLOSE);+ }+ }+ append_inner(+ &mut spans,+ &mut url_slots,+ &mut code_slots,+ (label_spans, label_slots, label_codes),+ modifier | Modifier::UNDERLINED,+ );+ // The URL rides after the wrap (the OSC 8 region covers the label+ // only), in the dim `link_url` slot unless the label already is+ // the URL (mailto stripped for the comparison).+ let comparison = url.strip_prefix("mailto:").unwrap_or(url.as_str());+ if label != url && label != comparison {+ // The bracket renders the destination as visible text, so it+ // gets the same control-byte hardening as the OSC 8 target: a+ // smuggled escape byte can never re-enter the terminal live.+ let shown = crate::hyperlinks::sanitize_control_bytes(url.clone());+ url_slots.push(spans.len());+ spans.push(Span::styled(format!(" ({shown})"), style.link_url));+ }+ i = k + 1;+ continue;}}// emphasisif (c == '*' || c == '_') && i + 1 < bytes.len() {+ // CommonMark flanking for `_`: a delimiter between word characters+ // can neither open nor close emphasis (snake_case prose keeps its+ // underscores); `*` has no intraword restriction.+ if c == '_' && !underscore_can_open(&bytes, i) {+ buf.push(c);+ i += 1;+ continue;+ }let is_triple = i + 2 < bytes.len() && bytes[i + 1] == c && bytes[i + 2] == c;if is_triple {if let Some(close) = find_closing(&bytes, i + 3, c, 3) {@@ -172,10 +187,13 @@ fn render_inline_ctx(text: &str, style: &MarkdownStyle, in_link: bool) -> (Line,bold = !bold;italic = !italic;let inner: String = bytes[i + 3..close].iter().collect();- spans.push(Span::styled(- inner,- base.add_modifier(style.bold | style.italic),- ));+ append_inner(+ &mut spans,+ &mut url_slots,+ &mut code_slots,+ render_inline_ctx(&inner, style, in_link),+ style.bold | style.italic,+ );bold = !bold;italic = !italic;i = close + 3;@@ -184,7 +202,99 @@ fn render_inline_ctx(text: &str, style: &MarkdownStyle, in_link: bool) -> (Line,}let doubled = i + 1 < bytes.len() && bytes[i + 1] == c;let (len, close_search) = if doubled { (2, i + 2) } else { (1, i + 1) };- if let Some(close) = find_closing(&bytes, close_search, c, len) {+ // An intraword `_` cannot close, but the opener still matches a+ // later one — keep searching instead of giving up.+ let mut close = find_closing(&bytes, close_search, c, len);+ while close.is_some_and(|k| c == '_' && !underscore_can_close(&bytes, k, len)) {+ let resume = close.expect("checked Some") + len;+ close = find_closing(&bytes, resume, c, len);+ }+ if let Some(close) = close {+ if doubled {+ // A closer run of three (`**outer *inner***`): the extra star+ // pairs with the inner single delimiter, the outer keeps the+ // last two — marked renders bold(outer + italic(inner)).+ let run = run_length(&bytes, close, c);+ // Only an unmatched inner opener owns that third closer. A+ // completed *inner* pair (or a star inside code) cannot steal+ // the opener of the following adjacent emphasis span.+ let mut nested_opener = false;+ let mut nested_opener_right_flanking = false;+ let mut at = close_search;+ while at < close {+ if bytes[at] == '\\'+ && bytes.get(at + 1).is_some_and(char::is_ascii_punctuation)+ {+ at += 2;+ continue;+ }+ if bytes[at] == '`' {+ let end = (at + 1..close).find(|&k| bytes[k] == '`');+ if let Some(end) = end {+ at = end + 1;+ continue;+ }+ }+ if bytes[at] == c {+ let length = run_length(&bytes, at, c);+ if length == 1 {+ let can_open =+ bytes.get(at + 1).is_some_and(|next| !next.is_whitespace())+ && (c != '_' || underscore_can_open(&bytes, at));+ let can_close = at > 0+ && !bytes[at - 1].is_whitespace()+ && (!bytes[at - 1].is_ascii_punctuation()+ || bytes.get(at + 1).is_none_or(|next| {+ next.is_whitespace() || next.is_ascii_punctuation()+ }))+ && (c != '_' || underscore_can_close(&bytes, at, 1));+ if nested_opener && can_close {+ nested_opener = false;+ } else if !nested_opener && can_open {+ nested_opener = true;+ nested_opener_right_flanking = can_close;+ }+ }+ at += length;+ } else {+ at += 1;+ }+ }+ if run == 3 && nested_opener {+ let inner: String = bytes[close_search..=close].iter().collect();+ if !inner.trim().is_empty() {+ flush!();+ bold = !bold;+ append_inner(+ &mut spans,+ &mut url_slots,+ &mut code_slots,+ render_inline_ctx(&inner, style, in_link),+ style.bold,+ );+ bold = !bold;+ i = close + 3;+ continue;+ }+ }+ if run == 2 && nested_opener && !nested_opener_right_flanking {+ // Marked pairs an unmatched inner single opener with the+ // first closer: the outside becomes literal '*' + em,+ // not strong. Actual TS: **a\**b** => '*' + italic(a*b).+ buf.push(c);+ flush!();+ let inner: String = bytes[close_search..=close].iter().collect();+ append_inner(+ &mut spans,+ &mut url_slots,+ &mut code_slots,+ render_inline_ctx(&inner, style, in_link),+ style.italic,+ );+ i = close + 2;+ continue;+ }+ }let inner: String = bytes[close_search..close].iter().collect();if inner.trim().is_empty() {buf.push(c);@@ -194,31 +304,23 @@ fn render_inline_ctx(text: &str, style: &MarkdownStyle, in_link: bool) -> (Line,flush!();if doubled {bold = !bold;- let (mut inner_spans, mut inner_slots) =- render_inline_ctx(&inner, style, in_link);- let offset = spans.len();- for slot in &mut inner_slots {- *slot += offset;- }- url_slots.append(&mut inner_slots);- for s in &mut inner_spans {- s.style = s.style.add_modifier(style.bold);- }- spans.extend(inner_spans);+ append_inner(+ &mut spans,+ &mut url_slots,+ &mut code_slots,+ render_inline_ctx(&inner, style, in_link),+ style.bold,+ );bold = !bold;} else {italic = !italic;- let (mut inner_spans, mut inner_slots) =- render_inline_ctx(&inner, style, in_link);- let offset = spans.len();- for slot in &mut inner_slots {- *slot += offset;- }- url_slots.append(&mut inner_slots);- for s in &mut inner_spans {- s.style = s.style.add_modifier(style.italic);- }- spans.extend(inner_spans);+ append_inner(+ &mut spans,+ &mut url_slots,+ &mut code_slots,+ render_inline_ctx(&inner, style, in_link),+ style.italic,+ );italic = !italic;}i = close + len;@@ -230,17 +332,13 @@ fn render_inline_ctx(text: &str, style: &MarkdownStyle, in_link: bool) -> (Line,let inner: String = bytes[i + 2..close].iter().collect();if !inner.trim().is_empty() {flush!();- let (mut inner_spans, mut inner_slots) =- render_inline_ctx(&inner, style, in_link);- let offset = spans.len();- for slot in &mut inner_slots {- *slot += offset;- }- url_slots.append(&mut inner_slots);- for s in &mut inner_spans {- s.style = s.style.add_modifier(style.strikethrough);- }- spans.extend(inner_spans);+ append_inner(+ &mut spans,+ &mut url_slots,+ &mut code_slots,+ render_inline_ctx(&inner, style, in_link),+ style.strikethrough,+ );i = close + 2;continue;}@@ -273,8 +371,11 @@ fn render_inline_ctx(text: &str, style: &MarkdownStyle, in_link: bool) -> (Line,token.text.clone(),base.add_modifier(m | Modifier::UNDERLINED),);- if crate::hyperlinks::hyperlinks_enabled() {- let href = crate::hyperlinks::resolve_link_href(&token.href);+ {+ let href = crate::hyperlinks::resolve_link_href_with_base(+ &token.href,+ style.link_base_url.as_ref(),+ );label.content.insert_str(0, &crate::hyperlinks::osc8_open(&href));@@ -289,7 +390,7 @@ fn render_inline_ctx(text: &str, style: &MarkdownStyle, in_link: bool) -> (Line,if token.text != token.href && token.text != comparison {let shown = crate::hyperlinks::sanitize_control_bytes(token.href.clone());url_slots.push(spans.len());- spans.push(Span::styled(format!(" [{shown}]"), style.link_url));+ spans.push(Span::styled(format!(" ({shown})"), style.link_url));}i += token.raw.chars().count();continue;@@ -301,7 +402,110 @@ fn render_inline_ctx(text: &str, style: &MarkdownStyle, in_link: bool) -> (Line,if spans.is_empty() {spans.push(Span::raw(""));}- (spans, url_slots)+ (spans, url_slots, code_slots)+}++/// True when the `!` immediately before the `[` at `at` is an image marker:+/// an even run of backslashes precedes it (an escaped `!` is a literal bang).+fn image_marker_before(bytes: &[char], at: usize) -> bool {+ if at == 0 || bytes[at - 1] != '!' {+ return false;+ }+ let mut backslashes = 0usize;+ let mut j = at - 1;+ while j > 0 && bytes[j - 1] == '\\' {+ backslashes += 1;+ j -= 1;+ }+ backslashes.is_multiple_of(2)+}++/// Recurse into one inline construct's inner text and merge the result into the+/// outer span stream: the tracked slots remap across the extend, and the+/// construct's own modifier applies on top of the inner styles.+fn append_inner(+ outer: &mut Vec<Span>,+ url_slots: &mut Vec<usize>,+ code_slots: &mut Vec<usize>,+ inner: (Line, Vec<usize>, Vec<usize>),+ modifier: Modifier,+) {+ let (mut spans, mut slots, mut codes) = inner;+ let offset = outer.len();+ for slot in &mut slots {+ *slot += offset;+ }+ for slot in &mut codes {+ *slot += offset;+ }+ url_slots.append(&mut slots);+ code_slots.append(&mut codes);+ for s in &mut spans {+ s.style = s.style.add_modifier(modifier);+ }+ outer.extend(spans);+}++/// The `CommonMark` inline-link destination after `[label](` (marked's link rule):+/// parens ride as a balanced pair (the destination ends at its own `)`, not the+/// first one inside), escaped punctuation consumes its backslash, and the destination stops at+/// whitespace — a quoted title may follow before the closing paren. Returns the+/// destination and the index of the closing `)`.+fn link_destination(bytes: &[char], from: usize) -> Option<(String, usize)> {+ let mut k = from;+ while k < bytes.len() && (bytes[k] == ' ' || bytes[k] == '\t') {+ k += 1;+ }+ let mut url = String::new();+ let mut paren_depth = 0usize;+ while k < bytes.len() {+ let b = bytes[k];+ if b == ')' && paren_depth == 0 {+ return Some((url, k));+ }+ if b == ' ' || b == '\t' {+ // Pinned TS/marked accepts an unmatched '(' before a quoted title;+ // its actual frame still links foo(bar in [t](foo(bar "title").+ // Preserve that product behavior rather than imposing a stricter parser.+ break;+ }+ if b == '\\' && bytes.get(k + 1).is_some_and(char::is_ascii_punctuation) {+ k += 1;+ url.push(bytes[k]);+ k += 1;+ continue;+ }+ if b == '(' {+ paren_depth += 1;+ } else if b == ')' {+ paren_depth = paren_depth.saturating_sub(1);+ }+ url.push(b);+ k += 1;+ }+ while k < bytes.len() && (bytes[k] == ' ' || bytes[k] == '\t') {+ k += 1;+ }+ if k < bytes.len() && (bytes[k] == '"' || bytes[k] == '\'') {+ let quote = bytes[k];+ k += 1;+ while k < bytes.len() && bytes[k] != quote {+ // An escaped quote belongs to the title, not its closing delimiter.+ k += if bytes[k] == '\\' && k + 1 < bytes.len() {+ 2+ } else {+ 1+ };+ }+ if k == bytes.len() {+ return None;+ }+ k += 1;+ while k < bytes.len() && (bytes[k] == ' ' || bytes[k] == '\t') {+ k += 1;+ }+ }+ (k < bytes.len() && bytes[k] == ')').then_some((url, k))}/// Run the marked autolink rules on the text starting at `rest` (the caller's slice of the original@@ -315,9 +519,43 @@ fn autolink_token_at(rest: &str, angle: bool) -> Option<crate::autolink::Autolin}}+/// `CommonMark` flanking for `_` openers: left-flanking and not intraword (the+/// preceding char is not a word character), so `snake_case` never opens.+fn underscore_can_open(bytes: &[char], at: usize) -> bool {+ let next = bytes.get(at + 1);+ let next_ws = next.is_none_or(|c: &char| c.is_whitespace());+ let prev_word = at > 0 && (bytes[at - 1].is_alphanumeric() || bytes[at - 1] == '_');+ !next_ws && !prev_word+}++/// The `_` closer mirror: right-flanking and not intraword (the char after the+/// closer run is not a word character).+fn underscore_can_close(bytes: &[char], at: usize, len: usize) -> bool {+ let prev = at > 0 && !bytes[at - 1].is_whitespace();+ let next_word = bytes+ .get(at + len)+ .is_some_and(|c| c.is_alphanumeric() || *c == '_');+ prev && !next_word+}++/// The length of the run of `delim` starting at `at`.+fn run_length(bytes: &[char], at: usize, delim: char) -> usize {+ let mut n = 0;+ while bytes.get(at + n) == Some(&delim) {+ n += 1;+ }+ n+}+fn find_closing(chars: &[char], from: usize, delim: char, len: usize) -> Option<usize> {let mut i = from;while i + len <= chars.len() {+ // Escaped punctuation is literal, including a would-be closer. Walking+ // escape pairs also leaves a delimiter after an even backslash run live.+ if chars[i] == '\\' && chars.get(i + 1).is_some_and(char::is_ascii_punctuation) {+ i += 2;+ continue;+ }if (0..len).all(|k| chars[i + k] == delim) {return Some(i);}diff --git a/crates/pa-tui/src/markdown/tests.rs b/crates/pa-tui/src/markdown/tests.rsindex f5a94c53b1..7a9887161b 100644--- a/crates/pa-tui/src/markdown/tests.rs+++ b/crates/pa-tui/src/markdown/tests.rs@@ -1,4 +1,303 @@use super::*;+use std::fmt::Write as FmtWrite;++#[test]+fn review_markdown_escaped_delimiters_keep_their_emphasis() {+ let style = MarkdownStyle::default();+ for (text, shown, modifier) in [+ (r"*a\*b*", "a*b", Modifier::ITALIC),+ (r"**a\*b**", "a*b", Modifier::BOLD),+ (r"***a\*b***", "a*b", Modifier::BOLD | Modifier::ITALIC),+ (r"~~a\~~b~~", "a~~b", Modifier::CROSSED_OUT),+ ] {+ let spans = render_inline(text, &style);+ assert_eq!(+ spans+ .iter()+ .map(|span| span.content.as_str())+ .collect::<String>(),+ shown+ );+ assert!(+ spans+ .iter()+ .all(|span| span.style.add_modifier.contains(modifier)),+ "{text}"+ );+ }+ // Two backslashes escape each other; the following star closes emphasis.+ let spans = render_inline(r"*a\\* tail", &style);+ assert_eq!(+ spans+ .iter()+ .map(|span| span.content.as_str())+ .collect::<String>(),+ "a\\ tail"+ );+ assert!(spans[0].style.add_modifier.contains(Modifier::ITALIC));+ assert_eq!(spans.last().unwrap().style.add_modifier, Modifier::empty());+}++#[test]+fn review_markdown_destinations_balance_before_a_title() {+ let style = MarkdownStyle::default();+ // The pinned real TS frame accepts this destination before a quoted title.+ let permissive = "[t](foo(bar \"title\")";+ let spans = super::render_inline(permissive, &style);+ assert_eq!(+ spans,+ vec![+ Span::styled(+ format!(+ "{}t{}",+ crate::hyperlinks::osc8_open("foo(bar"),+ crate::hyperlinks::OSC8_CLOSE+ ),+ style.body.add_modifier(Modifier::UNDERLINED)+ ),+ Span::styled(" (foo(bar)", style.link_url)+ ],+ );+ for capability in [false, true] {+ crate::hyperlinks::set_hyperlinks_override(Some(capability));+ for text in ["[t](foo(bar) \"title\")", r#"[t](foo\(bar\) "a \"b\"")"#] {+ let mut spans = super::render_inline(text, &style);+ let ranges = crate::hyperlinks::frame_link_ranges(&[spans.clone()]);+ assert_eq!(ranges.len(), 1);+ assert_eq!(ranges[0].url, "foo(bar)");+ // Visible text excludes zero-width metadata under either capability.+ crate::hyperlinks::strip_osc8(&mut spans);+ assert_eq!(+ spans,+ vec![+ Span::styled("t", style.body.add_modifier(Modifier::UNDERLINED)),+ Span::styled(" (foo(bar))", style.link_url)+ ]+ );+ }+ }+ crate::hyperlinks::set_hyperlinks_override(None);+}++#[test]+fn review_markdown_unmatched_single_in_strong_matches_ts_emphasis_pairing() {+ let style = MarkdownStyle::default();+ // Actual TS styled frame: one literal star, then italic a*b; the escaped+ // star is literal while the next star opens a nested emphasis span.+ let spans = render_inline(r"**a\**b**", &style);+ assert_eq!(+ spans+ .iter()+ .map(|span| span.content.as_str())+ .collect::<String>(),+ "*a*b"+ );+ assert_eq!(spans[0], Span::styled("*", style.body));+ assert!(spans+ .iter()+ .skip(1)+ .all(|span| span.style.add_modifier == Modifier::ITALIC));+ let complete = render_inline("**outer *inner* tail**", &style);+ assert_eq!(+ complete+ .iter()+ .map(|span| span.content.as_str())+ .collect::<String>(),+ "outer inner tail"+ );+ assert!(complete+ .iter()+ .all(|span| span.style.add_modifier.contains(Modifier::BOLD)));+ // An intraword single star is both-flanking; marked keeps this strong.+ let intraword = render_inline("**a*b**", &style);+ assert_eq!(+ intraword,+ vec![Span::styled("a*b", style.body.add_modifier(Modifier::BOLD))]+ );+ // Pinned marked18.0.7 tokens also use literal '*' + nested em for the+ // ordinary left-only inner opener; it is not restricted to an escape case.+ let ordinary = render_inline("**a *b**", &style);+ assert_eq!(+ ordinary,+ vec![+ Span::styled("*", style.body),+ Span::styled("a ", style.body.add_modifier(Modifier::ITALIC)),+ Span::styled("b", style.body.add_modifier(Modifier::ITALIC)),+ ]+ );+}++// Visible-text tests discard metadata only in the legacy-capability fixture;+// raw metadata contracts use super::render_inline/render_markdown explicitly.+fn render_inline(text: &str, style: &MarkdownStyle) -> Line {+ let mut line = super::render_inline(text, style);+ if !crate::hyperlinks::hyperlinks_enabled() {+ crate::hyperlinks::strip_osc8(&mut line);+ }+ line+}++fn render_markdown(text: &str, width: usize, style: &MarkdownStyle) -> Vec<Line> {+ let mut rows = super::render_markdown(text, width, style);+ if !crate::hyperlinks::hyperlinks_enabled() {+ for row in &mut rows {+ crate::hyperlinks::strip_osc8(row);+ }+ }+ rows+}++#[test]+fn review_markdown_legacy_links_keep_metadata_and_session_base() {+ crate::hyperlinks::set_hyperlinks_override(Some(false));+ let mut style = MarkdownStyle {+ link_base_url: Some(url::Url::parse("file:///workspace/project/").unwrap()),+ ..MarkdownStyle::default()+ };+ let line = super::render_inline("[link](url)", &style);+ assert_eq!(+ line,+ vec![+ Span::styled(+ format!(+ "{}link{}",+ crate::hyperlinks::osc8_open("file:///workspace/project/url"),+ crate::hyperlinks::OSC8_CLOSE+ ),+ style.body.add_modifier(Modifier::UNDERLINED)+ ),+ Span::styled(" (url)", style.link_url),+ ]+ );+ let mut cache = MarkdownBlockCache::default();+ let text = "[link](url)\n\ntail";+ let before = super::render_markdown_tagged(text, 80, &style, "", &mut cache);+ style.link_base_url = Some(url::Url::parse("file:///workspace/other/").unwrap());+ let after = super::render_markdown_tagged(text, 80, &style, "", &mut cache);+ assert_ne!(before, after);+ assert_eq!(after, super::render_markdown(text, 80, &style));+ crate::hyperlinks::set_hyperlinks_override(None);+}++#[test]+fn review_markdown_outdented_nested_text_matches_captured_ts_frame() {+ let text = "1. parent\n - child\n - leaf\n continuation\n2. sibling";+ let style = MarkdownStyle::default();+ let rows = render_markdown(text, 80, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|row| row.iter().map(|span| span.content.as_str()).collect())+ .collect();+ assert_eq!(+ flat,+ vec![+ "1. parent",+ " - child",+ " - leaf",+ "continuation",+ "2. sibling"+ ]+ );+ for width in [40, 80, 120] {+ assert_eq!(+ markdown_row_count(text, width, &style),+ render_markdown(text, width, &style).len()+ );+ }+}++#[test]+fn review_markdown_adjacent_emphasis_keeps_the_next_opener() {+ let style = MarkdownStyle::default();+ let spans = render_inline("**bold***italic*", &style);+ let actual: Vec<_> = spans+ .iter()+ .map(|s| (s.content.as_str(), s.style.add_modifier))+ .collect();+ assert_eq!(+ actual,+ vec![("bold", Modifier::BOLD), ("italic", Modifier::ITALIC)]+ );+ // A real nested opener still owns the first star of the triple closer.+ let spans = render_inline("**outer *inner***", &style);+ assert_eq!(+ spans.iter().map(|s| s.content.as_str()).collect::<String>(),+ "outer inner"+ );+ assert!(spans+ .last()+ .unwrap()+ .style+ .add_modifier+ .contains(Modifier::BOLD | Modifier::ITALIC));+ for text in ["**outer *inner* tail***next*", "**outer `*` tail***next*"] {+ let spans = render_inline(text, &style);+ let joined: String = spans.iter().map(|s| s.content.as_str()).collect();+ let expected = if text.contains('`') {+ "outer * tailnext"+ } else {+ "outer inner tailnext"+ };+ assert_eq!(joined, expected);+ let next = spans.last().unwrap();+ assert_eq!(next.content, "next");+ assert_eq!(next.style.add_modifier, Modifier::ITALIC);+ }+}++#[test]+fn review_markdown_link_titles_skip_escaped_quotes() {+ let style = MarkdownStyle::default();+ for text in [r#"[t](url "a \"b\"")"#, r"[t](url 'a \'b\'')"] {+ let mut spans = render_inline(text, &style);+ crate::hyperlinks::strip_osc8(&mut spans);+ assert_eq!(+ spans.iter().map(|s| s.content.as_str()).collect::<String>(),+ "t (url)"+ );+ assert!(spans[0].style.add_modifier.contains(Modifier::UNDERLINED));+ }+}++#[test]+fn review_markdown_deep_lists_keep_text_with_a_bounded_tree() {+ let text = (0..96).fold(String::new(), |mut text, n| {+ writeln!(text, "{}- item{n}", " ".repeat(n)).expect("writing a String cannot fail");+ text+ });+ let blocks = parse_blocks(&text);+ let mut pending = Vec::new();+ for block in &blocks {+ if let BlockKind::List(list) = &block.kind {+ pending.push((list, 0));+ }+ }+ let mut deepest = 0;+ let mut literal_tail = false;+ while let Some((list, depth)) = pending.pop() {+ deepest = deepest.max(depth);+ for item in &list.items {+ for part in &item.parts {+ match part {+ ListPart::List(child) => pending.push((child, depth + 1)),+ ListPart::Text(text) | ListPart::LazyText(text) => {+ literal_tail |= text == "- item95";+ }+ }+ }+ }+ }+ assert!(deepest <= 64, "nested tree depth was {deepest}");+ assert!(+ literal_tail,+ "overflow nesting must preserve literal content"+ );+ let style = MarkdownStyle::default();+ assert_eq!(+ markdown_row_count(&text, 16_000, &style),+ render_markdown(&text, 16_000, &style).len()+ );+}/// The pre-fix overlong-word break loop from origin/rust (the quadratic re-measure version): the/// output oracle for [`wrap_spans_into`]'s arithmetic-tracked rewrite — every corpus below must@@ -101,7 +400,7 @@ fn assert_wrap_parity(spans: &[Span], widths: &[usize]) {let mut legacy: Vec<Line> = Vec::new();legacy_wrap_spans(spans, width, &mut legacy);let mut current: Vec<Line> = Vec::new();- wrap_spans(spans, width, Style::default(), &mut current);+ wrap_spans(spans, width, &mut current);assert_eq!(legacy, current,"wrap parity (styled spans) broke at width {width}: spans={spans:?}"@@ -205,7 +504,7 @@ fn wrap_stress_megabyte_monoword_candidate_only() {let spans = vec![Span::styled(token.clone(), Style::default())];let width = 80usize;let mut current: Vec<Line> = Vec::new();- wrap_spans(&spans, width, Style::default(), &mut current);+ wrap_spans(&spans, width, &mut current);// 1048576 chars at 80 columns: CEIL rows (a floor here fails the 16-char remainder)assert_eq!(current.len(),@@ -249,7 +548,7 @@ fn heading_link_wraps_keeps_the_affordance_and_counts() {.iter().map(|s| crate::hyperlinks::strip_osc8_content(&s.content)).collect();- assert_eq!(visible, "docs [https://x.dev/a]");+ assert_eq!(visible, "docs (https://x.dev/a)");// A narrow heading wraps like any row: the bracket — closing `]` included —// lands on its own row instead of clipping (count==paint at every width).let rows = render_markdown("# [docs](https://x.dev/a)", 20, &style);@@ -264,7 +563,7 @@ fn heading_link_wraps_keeps_the_affordance_and_counts() {.collect();assert_eq!(visible,- vec!["docs".to_string(), "[https://x.dev/a]".to_string()]+ vec!["docs".to_string(), "(https://x.dev/a)".to_string()]);for width in 1..40 {assert_eq!(@@ -284,21 +583,26 @@ fn heading_link_wraps_keeps_the_affordance_and_counts() {}#[test]-fn heading_tapers_code_and_body_that_merely_match_the_link_url_style() {+fn heading_keeps_code_and_bracket_spans_by_origin() {// A theme can collide `mdCode`/`mdBody` onto the same color as `mdLinkUrl`; the- // taper reads the bracket slots by origin, not style equality.+ // taper reads the code and bracket spans by origin, not style equality: the+ // codespan keeps its own color while plain text and labels taper to the h1+ // heading style (bold + underline), the label keeping its underline affordance.let mut style = MarkdownStyle::default();style.code = style.link_url;style.body = style.link_url;crate::hyperlinks::set_hyperlinks_override(Some(false));let lines = render_markdown("# `c` [d](https://x.dev/a)", 40, &style);+ let taper = style+ .heading+ .add_modifier(Modifier::BOLD | Modifier::UNDERLINED);assert_eq!(lines,vec![vec![- Span::styled("c", style.heading),- Span::styled(" ", style.heading),- Span::styled("d", style.heading.add_modifier(Modifier::UNDERLINED)),- Span::styled(" [https://x.dev/a]", style.link_url),+ Span::styled("c", style.code),+ Span::styled(" ", taper),+ Span::styled("d", taper),+ Span::styled(" (https://x.dev/a)", style.link_url),]]);crate::hyperlinks::set_hyperlinks_override(None);@@ -544,7 +848,7 @@ fn inline_bold_code_link() {let style = MarkdownStyle::default();let spans = render_inline("a **b** `c` [d](http://e)", &style);let texts: Vec<&str> = spans.iter().map(|s| s.content.as_str()).collect();- assert_eq!(texts, vec!["a ", "b", " ", "c", " ", "d", " [http://e]"]);+ assert_eq!(texts, vec!["a ", "b", " ", "c", " ", "d", " (http://e)"]);crate::hyperlinks::set_hyperlinks_override(None);}@@ -559,7 +863,7 @@ fn legacy_link_row_is_underlined_and_shows_the_url() {vec!["see ".to_string(),"docs".to_string(),- " [https://x.dev/a]".to_string()+ " (https://x.dev/a)".to_string()]);assert!(spans[1].style.add_modifier.contains(Modifier::UNDERLINED));@@ -585,7 +889,7 @@ fn osc8_gated_link_row_wraps_the_label_in_a_hyperlink_and_shows_the_url() {assert_eq!(joined,format!(- "see {}docs{} [https://x.dev/a]",+ "see {}docs{} (https://x.dev/a)",crate::hyperlinks::osc8_open("https://x.dev/a"),crate::hyperlinks::OSC8_CLOSE)@@ -597,7 +901,7 @@ fn osc8_gated_link_row_wraps_the_label_in_a_hyperlink_and_shows_the_url() {assert_eq!(spans[2].style.fg, style.link_url.fg);// The sequences are zero-width: the row measures like plain text plus the// bracketed URL.- assert_eq!(str_width(&joined), str_width("see docs [https://x.dev/a]"));+ assert_eq!(str_width(&joined), str_width("see docs (https://x.dev/a)"));// Windows drive-letter targets classify as file paths; the label is// the URL, so no bracket follows it.let drive = render_inline("[c:\\src](c:\\src)", &style);@@ -622,7 +926,7 @@ fn balanced_parens_in_a_link_destination_stay_intact() {spans,vec![Span::styled("math", style.body.add_modifier(Modifier::UNDERLINED)),- Span::styled(format!(" [{url}]"), style.link_url),+ Span::styled(format!(" ({url})"), style.link_url),]);crate::hyperlinks::set_hyperlinks_override(None);@@ -639,7 +943,7 @@ fn balanced_parens_in_a_link_destination_stay_intact() {),style.body.add_modifier(Modifier::UNDERLINED),),- Span::styled(format!(" [{url}]"), style.link_url),+ Span::styled(format!(" ({url})"), style.link_url),]);crate::hyperlinks::set_hyperlinks_override(None);@@ -647,21 +951,45 @@ fn balanced_parens_in_a_link_destination_stay_intact() {#[test]fn escaped_parens_never_open_or_close_the_link_destination() {- // An escaped paren rides verbatim without counting toward the balance: `\(`- // does not swallow the real closer, and `[a](b\)` is plain text.+ // Marked consumes the escape before publishing token.href. Escaped parens+ // stay literal without affecting balance; the visible URL and OSC8 href+ // both use that unescaped destination. A sole escaped closer is not a link.crate::hyperlinks::set_hyperlinks_override(Some(false));- let style = MarkdownStyle::default();+ let style = MarkdownStyle {+ link_base_url: Some(url::Url::parse("file:///fixture/root/").unwrap()),+ ..MarkdownStyle::default()+ };let spans = render_inline("[a](b\\(c)", &style);assert_eq!(spans,vec![Span::styled("a", style.body.add_modifier(Modifier::UNDERLINED)),- Span::styled(" [b\\(c]", style.link_url),+ Span::styled(" (b(c)", style.link_url),]);+ for (text, destination, href) in [+ (r"[a](b\(c)", "b(c", "file:///fixture/root/b(c"),+ (r"[a](b\))", "b)", "file:///fixture/root/b)"),+ ] {+ assert_eq!(+ super::render_inline(text, &style),+ vec![+ Span::styled(+ format!(+ "{}a{}",+ crate::hyperlinks::osc8_open(href),+ crate::hyperlinks::OSC8_CLOSE+ ),+ style.body.add_modifier(Modifier::UNDERLINED)+ ),+ Span::styled(format!(" ({destination})"), style.link_url),+ ]+ );+ }let plain = render_inline("[a](b\\)", &style);assert_eq!(plain.len(), 1);- assert_eq!(plain[0].content, "[a](b\\)");+ // Not a link, but marked's escape rule still processes the backslash.+ assert_eq!(plain[0].content, "[a](b)");crate::hyperlinks::set_hyperlinks_override(None);}@@ -675,12 +1003,12 @@ fn the_url_bracket_scrubs_terminal_control_bytes() {let url = "https://x.dev/\u{1b}]52;c;base64";let spans = render_inline(&format!("[a]({url})"), &style);let bracket = spans.last().expect("bracket span");- assert_eq!(bracket.content, " [https://x.dev/%1B]52;c;base64]");+ assert_eq!(bracket.content, " (https://x.dev/%1B]52;c;base64)");// The bare-url autolink form hardens the same way (the regex tail only excludes// whitespace); the www form's href gains its scheme, so the bracket shows.let www = render_inline("www.x.dev/\u{1b}]52;c=base64", &style);let bracket = www.last().expect("bracket span");- assert_eq!(bracket.content, " [http://www.x.dev/%1B]52;c=base64]");+ assert_eq!(bracket.content, " (http://www.x.dev/%1B]52;c=base64)");assert!(!bracket.content.contains('\u{1b}'),"no raw escape byte in the visible bracket"@@ -715,7 +1043,7 @@ fn link_url_bracket_wraps_and_counts_like_text() {visible,vec!["see docs".to_string(),- "[https://x.dev/a]".to_string(),+ "(https://x.dev/a)".to_string(),"tail".to_string()],"the bracket wraps like normal text"@@ -749,7 +1077,7 @@ fn long_link_url_wraps_without_breaking_the_clickable_region() {.collect();// The wrap drops the boundary gap (a wrapped row never carries its leading or// trailing space), so the visible text is `docs` then the bracketed URL.- assert_eq!(visible, format!("docs[{url}]"));+ assert_eq!(visible, format!("docs({url})"));for width in 1..40 {assert_eq!(markdown_row_count(&format!("[docs]({url})"), width, &style),@@ -853,7 +1181,7 @@ fn www_autolink_gains_scheme_and_shows_the_url_in_both_forms() {let texts: Vec<&str> = spans.iter().map(|s| s.content.as_str()).collect();assert_eq!(texts,- vec!["www.example.com/path", " [http://www.example.com/path]"]+ vec!["www.example.com/path", " (http://www.example.com/path)"]);assert!(spans[0].style.add_modifier.contains(Modifier::UNDERLINED),@@ -867,7 +1195,7 @@ fn www_autolink_gains_scheme_and_shows_the_url_in_both_forms() {assert_eq!(joined,format!(- "{}www.example.com/path{} [http://www.example.com/path]",+ "{}www.example.com/path{} (http://www.example.com/path)",crate::hyperlinks::osc8_open("http://www.example.com/path"),crate::hyperlinks::OSC8_CLOSE)@@ -941,7 +1269,7 @@ fn bare_url_is_not_autolinked_inside_a_link_label() {let style = MarkdownStyle::default();let spans = render_inline("[see https://in.dev/x](https://out.dev/y)", &style);let texts: Vec<&str> = spans.iter().map(|s| s.content.as_str()).collect();- assert_eq!(texts, vec!["see https://in.dev/x", " [https://out.dev/y]"]);+ assert_eq!(texts, vec!["see https://in.dev/x", " (https://out.dev/y)"]);crate::hyperlinks::set_hyperlinks_override(None);}@@ -1013,7 +1341,7 @@ fn styled_span_boundaries_keep_their_spaces() {fn wrap_spans_to_text(spans: &[Span], width: usize) -> String {let mut lines: Vec<Line> = Vec::new();- wrap_spans(spans, width, Style::default(), &mut lines);+ wrap_spans(spans, width, &mut lines);lines.iter().flat_map(|l| l.iter().map(|s| s.content.as_str()))@@ -1035,6 +1363,532 @@ fn wrapping() {fn quote_block() {let style = MarkdownStyle::default();let lines = render_markdown("> wisdom", 40, &style);- assert_eq!(lines[0][0].content, "▐ ");+ assert_eq!(lines[0][0].content, "│ ");assert_eq!(lines[0][1].content, "wisdom");}++#[test]+fn emphasis_renders_chalk_modifiers() {+ // TS `theme.bold/italic/strikethrough` are chalk; chalk emits its SGR modifiers in+ // any color terminal (supports-color level >= 1 whenever stdout is a TTY), so+ // `**bold**`, `*italic*`, and `~~strike~~` carry BOLD/ITALIC/CROSSED_OUT.+ let style = MarkdownStyle::default();+ let lines = render_markdown("**b** *i* ~~s~~", 40, &style);+ assert_eq!(lines.len(), 1);+ let spans = &lines[0];+ let find = |content: &str| {+ spans+ .iter()+ .find(|s| s.content == content)+ .unwrap_or_else(|| panic!("no span {content}: {spans:?}"))+ };+ assert_eq!(find("b").style.add_modifier, Modifier::BOLD);+ assert_eq!(find("i").style.add_modifier, Modifier::ITALIC);+ assert_eq!(find("s").style.add_modifier, Modifier::CROSSED_OUT);+ assert_eq!(find("b").style.fg, style.body.fg);+}++#[test]+fn headings_taper_by_level_like_the_theme_functions() {+ // TS heading styleFn: h1 bold+underline, h2/h3 bold, h4 bold+italic, h5/h6 italic —+ // all over the heading color.+ let style = MarkdownStyle::default();+ for (md, expected) in [+ ("# one", Modifier::BOLD | Modifier::UNDERLINED),+ ("## two", Modifier::BOLD),+ ("### three", Modifier::BOLD),+ ("#### four", Modifier::BOLD | Modifier::ITALIC),+ ("##### five", Modifier::ITALIC),+ ("###### six", Modifier::ITALIC),+ ] {+ let lines = render_markdown(md, 40, &style);+ let span = &lines[0][0];+ assert_eq!(span.style.fg, style.heading.fg, "{md}");+ assert_eq!(span.style.add_modifier, expected, "{md}");+ }+ // Seven hashes is not a heading (marked: `#{1,6}`).+ let lines = render_markdown("####### seven", 40, &style);+ assert_eq!(lines[0][0].content, "####### seven");+ assert_eq!(lines[0][0].style.fg, style.body.fg);+}++#[test]+fn heading_keeps_inline_code_color() {+ // TS `renderInlineTokens` codespan: `theme.code(text) + stylePrefix` — the heading+ // modifiers close around the codespan, and it keeps the mdCode color.+ let style = MarkdownStyle::default();+ let lines = render_markdown("# a `c` b", 40, &style);+ let spans = &lines[0];+ let find = |content: &str| {+ spans+ .iter()+ .find(|s| s.content == content)+ .unwrap_or_else(|| panic!("no span {content}: {spans:?}"))+ };+ let code = find("c");+ assert_eq!(code.style.fg, style.code.fg);+ assert_eq!(code.style.add_modifier, Modifier::empty());+ let text = find("a ");+ assert_eq!(text.style.fg, style.heading.fg);+ assert_eq!(+ text.style.add_modifier,+ Modifier::BOLD | Modifier::UNDERLINED+ );+}++#[test]+fn quote_renders_the_ts_style_and_box_border() {+ // TS blockquote: border `│ `; the whole quote content renders in the quote color+ // and italic (`theme.quote(theme.italic(...))` re-applied after every inline reset),+ // with emphasis modifiers riding on top; a codespan keeps its own color.+ let style = MarkdownStyle::default();+ let lines = render_markdown("> q **b** `c`", 40, &style);+ let spans = &lines[0];+ assert_eq!(spans[0].content, "│ ");+ assert_eq!(spans[0].style.fg, style.quote_border.fg);+ let find = |content: &str| {+ spans+ .iter()+ .find(|s| s.content == content)+ .unwrap_or_else(|| panic!("no span {content}: {spans:?}"))+ };+ let q = find("q ");+ assert_eq!(q.style.fg, style.quote.fg);+ assert_eq!(q.style.add_modifier, Modifier::ITALIC);+ let b = find("b");+ assert_eq!(b.style.fg, style.quote.fg);+ assert_eq!(b.style.add_modifier, Modifier::BOLD | Modifier::ITALIC);+ let c = find("c");+ assert_eq!(c.style.fg, style.code.fg);+ assert_eq!(c.style.add_modifier, Modifier::ITALIC);+}++#[test]+fn link_fallback_prints_parens_and_parses_titles() {+ crate::hyperlinks::set_hyperlinks_override(Some(false));+ let style = MarkdownStyle::default();+ let lines = render_markdown("[t](https://x.dev/a)", 40, &style);+ let visible: String = lines[0].iter().map(|s| s.content.as_str()).collect();+ assert_eq!(visible, "t (https://x.dev/a)");+ // marked's link rule: a quoted title after the destination parses out of the URL.+ let lines = render_markdown(r#"[t](https://x.dev/a "Title")"#, 40, &style);+ let visible: String = lines[0].iter().map(|s| s.content.as_str()).collect();+ assert_eq!(visible, "t (https://x.dev/a)");+ crate::hyperlinks::set_hyperlinks_override(None);+}++#[test]+fn hr_caps_at_eighty_columns() {+ // TS hr: `"─".repeat(Math.min(width, 80))`.+ let style = MarkdownStyle::default();+ let lines = render_markdown("---", 120, &style);+ assert_eq!(lines[0][0].content.chars().count(), 80);+ let lines = render_markdown("---", 40, &style);+ assert_eq!(lines[0][0].content.chars().count(), 40);+}++#[test]+fn quote_lazy_continuation_stays_inside_the_quote() {+ // marked's blockquote token lazily consumes following non-blank lines that do+ // not start another block: the paragraph stays inside the quote border.+ let style = MarkdownStyle::default();+ let lines = render_markdown("> quoted\ncontinued", 40, &style);+ assert_eq!(lines.len(), 2, "rows: {lines:?}");+ for row in &lines {+ assert_eq!(row[0].content, "│ ", "border on every quote row");+ }+ let joined: String = lines[1]+ .iter()+ .skip(1)+ .map(|s| s.content.as_str())+ .collect();+ assert_eq!(joined, "continued");+ // A new block still escapes the quote (CommonMark lazy rules).+ let lines = render_markdown("> quoted\n- list", 40, &style);+ assert_eq!(lines[1][0].content, "- ");+ let joined: String = lines[1].iter().map(|s| s.content.as_str()).collect();+ assert_eq!(joined, "- list");+}++#[test]+fn quote_blank_line_renders_a_bare_border_row() {+ let style = MarkdownStyle::default();+ let lines = render_markdown("> a\n>\n> b", 40, &style);+ assert_eq!(lines.len(), 3, "rows: {lines:?}");+ assert_eq!(lines[1], vec![Span::styled("│ ", style.quote_border)]);+ let joined: String = lines[2]+ .iter()+ .skip(1)+ .map(|s| s.content.as_str())+ .collect();+ assert_eq!(joined, "b");+}++#[test]+fn nested_lists_render_indented_and_keep_parent_numbering() {+ // marked nests indented markers; TS `renderList` frames each nesting level+ // (`indent + " "` per ancestor), giving nested rows their own indent and+ // bullets. The parent's numbering never shifts.+ let style = MarkdownStyle::default();+ let rows = render_markdown("- alpha\n - beta\n - gamma\n- delta", 60, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(+ flat,+ vec![+ "- alpha".to_string(),+ " - beta".to_string(),+ " - gamma".to_string(),+ "- delta".to_string(),+ ]+ );+ let rows = render_markdown(+ "1. first\n2. second\n - nested under second\n3. third",+ 60,+ &style,+ );+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(+ flat,+ vec![+ "1. first".to_string(),+ "2. second".to_string(),+ " - nested under second".to_string(),+ "3. third".to_string(),+ ]+ );+}++#[test]+fn an_ordered_list_after_an_unordered_list_is_its_own_block() {+ // marked ends a list at a same-indent marker of a different type.+ let style = MarkdownStyle::default();+ let rows = render_markdown("- bullet\n1. ordered", 60, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(flat, vec!["- bullet".to_string(), "1. ordered".to_string()]);+ assert_eq!(rows[1][0].content, "1. ");+ assert_eq!(rows[1][0].style.fg, style.list_bullet.fg);+}++#[test]+fn loose_lists_keep_blank_lines_internal() {+ // A blank line between items does not break the list (marked's loose list):+ // no blank rows render between items.+ let style = MarkdownStyle::default();+ let rows = render_markdown("- item one\n\n- item two\n\n- item three", 60, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(+ flat,+ vec![+ "- item one".to_string(),+ "- item two".to_string(),+ "- item three".to_string(),+ ]+ );+ // Blank lines before a following block belong to the gap, not the list.+ let rows = render_markdown("- item one\n\nafter", 60, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(+ flat,+ vec!["- item one".to_string(), String::new(), "after".to_string()]+ );+}++#[test]+fn list_item_continuation_lines_render_as_their_own_rows() {+ // TS `renderList` prefixes each item line beyond the first with `indent + " "`.+ let style = MarkdownStyle::default();+ let rows = render_markdown("- item\n continued", 60, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(flat, vec!["- item".to_string(), " continued".to_string()]);+ // Long continuation lines wrap; the wrapped rows keep the item-line prefix.+ let rows = render_markdown(+ "- item\n continued text that is long enough to wrap here",+ 22,+ &style,+ );+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(flat[1].trim(), "continued text that");+ for row in &rows {+ let w: usize = row.iter().map(|s| str_width(&s.content)).sum();+ assert!(w <= 22, "row over the width: {w}");+ }+ // count==paint at every width.+ for width in 1..60 {+ assert_eq!(+ markdown_row_count("- item\n continued", width, &style),+ render_markdown("- item\n continued", width, &style).len(),+ "count==paint at width {width}"+ );+ }+}++#[test]+fn punctuation_after_a_styled_span_never_wraps_alone() {+ // TS `splitIntoTokensWithAnsi` never splits a word at an ANSI boundary: the+ // text after a bold/code close stays glued to the preceding word when the+ // row wraps, so a trailing period can never land alone on its own row.+ let style = MarkdownStyle::default();+ let rows = render_markdown("alpha beta **gamma**.", 16, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(flat, vec!["alpha beta".to_string(), "gamma.".to_string()]);+ let rows = render_markdown("alpha beta `code`.", 12, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(flat, vec!["alpha beta".to_string(), "code.".to_string()]);+ for width in 1..40 {+ for text in ["alpha beta **gamma**.", "alpha beta `code`."] {+ assert_eq!(+ markdown_row_count(text, width, &style),+ render_markdown(text, width, &style).len(),+ "count==paint at width {width}"+ );+ }+ }+}++#[test]+fn backslash_escapes_render_the_punctuation_literally() {+ // marked's escape rule: a backslash before ASCII punctuation consumes the+ // backslash — the punctuation never opens emphasis, code, or a heading.+ let style = MarkdownStyle::default();+ let spans = render_inline(+ r"\*not emphasis\* snake\_case \#five no code via \` here",+ &style,+ );+ let joined: String = spans.iter().map(|s| s.content.as_str()).collect();+ assert_eq!(+ joined,+ r"*not emphasis* snake_case #five no code via ` here"+ );+ for s in &spans {+ assert_eq!(s.style.add_modifier, Modifier::empty(), "{joined}");+ }+ // A trailing unescaped backslash is a hard break: the paragraph consumes it.+ let rows = render_markdown("tail\\\nnext", 40, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(flat, vec!["tail".to_string(), "next".to_string()]);+}++#[test]+fn intraword_underscores_do_not_emphasize() {+ // CommonMark flanking: `_` between word characters can neither open nor+ // close emphasis, so snake_case prose keeps its underscores.+ let style = MarkdownStyle::default();+ let spans = render_inline("intra_word_underscores and snake_case_name", &style);+ let joined: String = spans.iter().map(|s| s.content.as_str()).collect();+ assert_eq!(joined, "intra_word_underscores and snake_case_name");+ for s in &spans {+ assert_eq!(s.style.add_modifier, Modifier::empty(), "{joined}");+ }+ // Boundary underscores still emphasize.+ let spans = render_inline("_real italic_ tail", &style);+ let joined: String = spans.iter().map(|s| s.content.as_str()).collect();+ assert_eq!(joined, "real italic tail");+ assert!(spans[0].style.add_modifier.contains(Modifier::ITALIC));+ // Doubled underscores keep the same flanking rule.+ let spans = render_inline("__bold__ tail", &style);+ let joined: String = spans.iter().map(|s| s.content.as_str()).collect();+ assert_eq!(joined, "bold tail");+ assert!(spans[0].style.add_modifier.contains(Modifier::BOLD));+}++#[test]+fn a_triple_close_pairs_the_inner_single_delimiter() {+ // `**outer *inner***`: the closer run's extra star closes the inner em and+ // the outer keeps the last two — marked renders bold(outer + italic(inner)).+ let style = MarkdownStyle::default();+ let spans = render_inline("**bold with *nested italic***", &style);+ let joined: String = spans.iter().map(|s| s.content.as_str()).collect();+ assert_eq!(joined, "bold with nested italic");+ for s in &spans {+ assert!(s.style.add_modifier.contains(Modifier::BOLD), "{joined}");+ }+ let inner = spans+ .iter()+ .find(|s| s.content.contains("nested italic"))+ .expect("inner span");+ assert!(inner.style.add_modifier.contains(Modifier::ITALIC));+}++#[test]+fn tilde_fences_render_as_code_blocks() {+ // marked accepts `~~~` fences exactly like ```` ``` ```` (same language rule).+ let style = MarkdownStyle::default();+ let rows = render_markdown("~~~\nplain code\n~~~", 40, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(flat.len(), 1, "rows: {flat:?}");+ assert_eq!(flat[0].trim(), "plain code");+ // The fence rows render in the code_block style, and count==paint holds.+ let code_spans: Vec<&crate::Span> = rows[0]+ .iter()+ .filter(|s| !s.content.trim().is_empty())+ .collect();+ assert!(code_spans.iter().all(|s| s.style.fg == style.code_block.fg));+ for width in 1..40 {+ assert_eq!(+ markdown_row_count("~~~\nplain code\n~~~", width, &style),+ render_markdown("~~~\nplain code\n~~~", width, &style).len(),+ "count==paint at width {width}"+ );+ }+}++#[test]+fn images_render_their_alt_text_only() {+ // marked's image token: the custom TS renderer falls through to the alt text —+ // no `!`, no link affordance, no URL bracket.+ let style = MarkdownStyle::default();+ let rows = render_markdown("An image  here", 40, &style);+ let flat: String = rows[0].iter().map(|s| s.content.as_str()).collect();+ assert_eq!(flat, "An image alt text here");+ for s in &rows[0] {+ assert!(+ !s.style.add_modifier.contains(Modifier::UNDERLINED),+ "{flat}"+ );+ }+ let spans = render_markdown("", 40, &style);+ let flat: String = spans[0].iter().map(|s| s.content.as_str()).collect();+ assert_eq!(flat, "only");+}++#[test]+fn a_bare_hash_renders_an_empty_heading_row() {+ // marked: `#` alone is an h1 with empty text — a blank row plus the+ // heading's trailing spacing.+ let style = MarkdownStyle::default();+ let rows = render_markdown("#\n\nafter", 40, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(+ flat,+ vec![String::new(), String::new(), "after".to_string()]+ );+}++#[test]+fn a_glued_word_wider_than_the_row_wraps_to_the_next_row() {+ // A glued unit that cannot START on the current row opens a fresh row+ // instead of overflowing it (`abcd` glued to the code span `ef`).+ let style = MarkdownStyle::default();+ let rows = render_markdown("abcd`ef`", 4, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(flat, vec!["abcd".to_string(), "ef".to_string()]);+ for row in &rows {+ let w: usize = row.iter().map(|s| str_width(&s.content)).sum();+ assert!(w <= 4, "row over the width: {w}");+ }+ for width in 1..12 {+ assert_eq!(+ markdown_row_count("abcd`ef`", width, &style),+ render_markdown("abcd`ef`", width, &style).len(),+ "count==paint at width {width}"+ );+ }+}++#[test]+fn continuation_after_a_nested_list_keeps_source_order() {+ // marked walks an item's block tokens in source order: text, then the+ // nested list, then the continuation text. The item model keeps that+ // order; a lazy soft continuation resumes at column zero in the TS frame.+ let style = MarkdownStyle::default();+ let rows = render_markdown("- parent\n - child\n after", 60, &style);+ let flat: Vec<String> = rows+ .iter()+ .map(|r| r.iter().map(|s| s.content.as_str()).collect())+ .collect();+ assert_eq!(+ flat,+ vec![+ "- parent".to_string(),+ " - child".to_string(),+ "after".to_string(),+ ]+ );+ for width in 1..60 {+ assert_eq!(+ markdown_row_count("- parent\n - child\n after", width, &style),+ render_markdown("- parent\n - child\n after", width, &style).len(),+ "count==paint at width {width}"+ );+ }+}++#[test]+fn an_underscore_opener_keeps_searching_for_a_closer() {+ // `_foo_bar_`: the first candidate closer is intraword (cannot close),+ // so the opener keeps searching — marked renders em(`foo_bar`).+ let style = MarkdownStyle::default();+ let spans = render_inline("_foo_bar_", &style);+ let joined: String = spans.iter().map(|s| s.content.as_str()).collect();+ assert_eq!(joined, "foo_bar");+ assert!(spans[0].style.add_modifier.contains(Modifier::ITALIC));+ // The intraword rejection still holds when no closer exists.+ let spans = render_inline("snake_case_name", &style);+ let joined: String = spans.iter().map(|s| s.content.as_str()).collect();+ assert_eq!(joined, "snake_case_name");+ for s in &spans {+ assert_eq!(s.style.add_modifier, Modifier::empty());+ }+}++#[test]+fn an_escaped_bang_before_a_link_is_not_an_image() {+ // The image marker is decided from the SOURCE text: `\!` is an escaped+ // bang (literal), not an image marker — the link still renders as a link.+ let style = MarkdownStyle::default();+ crate::hyperlinks::set_hyperlinks_override(Some(false));+ let spans = render_inline(r"Wow\", &style);+ let joined: String = spans.iter().map(|s| s.content.as_str()).collect();+ assert_eq!(joined, "Wow!docs (https://x.dev/a)");+ assert!(spans+ .iter()+ .any(|s| s.style.add_modifier.contains(Modifier::UNDERLINED)));+ crate::hyperlinks::set_hyperlinks_override(None);+ // An unescaped bang still marks an image: alt text only.+ let spans = render_inline("", &style);+ let joined: String = spans.iter().map(|s| s.content.as_str()).collect();+ assert_eq!(joined, "alt");+ // An escaped backslash before the bang stays an image marker.+ let spans = render_inline(r"\\", &style);+ let joined: String = spans.iter().map(|s| s.content.as_str()).collect();+ assert_eq!(joined, "\\alt");+}diff --git a/crates/pa-tui/src/markdown_table.rs b/crates/pa-tui/src/markdown_table.rsindex 8509afefc5..f3344e0600 100644--- a/crates/pa-tui/src/markdown_table.rs+++ b/crates/pa-tui/src/markdown_table.rs@@ -297,7 +297,7 @@ pub(crate) fn render_table(else {// The narrow fallback preserves the raw markdown without styling.for raw_line in raw {- wrap_spans(&[Span::raw(raw_line.clone())], width, Style::default(), out);+ wrap_spans(&[Span::raw(raw_line.clone())], width, out);}return;};@@ -309,11 +309,9 @@ pub(crate) fn render_table(};out.push(vec![Span::raw(join('┌', '┬', '┐'))]);- // The observed TS binary output (0.9.5, the parity ground truth) draws the header row exactly- // like the data rows. The TS source's `theme.bold(headerCell)` never reaches the wire, so the- // port must not emit the bold modifier.-- // Header row: wrapped and padded like the data rows (no header bold).+ // Header row: TS `renderTable` wraps every header cell in `theme.bold(...)`+ // (padding included); data rows stay unstyled.+ let bold = Style::default().add_modifier(style.bold);let header_cells: Vec<Vec<Line>> = header_spans.iter().zip(&column_widths)@@ -328,9 +326,12 @@ pub(crate) fn render_table(}let text = cell.get(line_idx).cloned().unwrap_or_default();let pad = column_widths[col].saturating_sub(spans_width(&text));- row.extend(text);+ row.extend(text.iter().cloned().map(|mut s| {+ s.style = s.style.add_modifier(style.bold);+ s+ }));if pad > 0 {- row.push(Span::raw(" ".repeat(pad)));+ row.push(Span::styled(" ".repeat(pad), bold));}}row.push(Span::raw(" │"));@@ -375,7 +376,7 @@ pub(crate) fn render_table(/// delegates to the ANSI-aware wrapTextWithAnsi).fn wrap_cell(spans: &Line, width: usize) -> Vec<Line> {let mut wrapped: Vec<Line> = Vec::new();- wrap_spans(spans, width.max(1), Style::default(), &mut wrapped);+ wrap_spans(spans, width.max(1), &mut wrapped);if wrapped.is_empty() {wrapped.push(Vec::new());}@@ -442,27 +443,23 @@ mod tests {}#[test]- fn header_row_matches_the_observed_ts_binary_output() {- // The TS binary draws the header exactly like the data rows: cell text in the body color,- // unstyled padding, no bold anywhere.+ fn header_row_renders_bold_and_data_rows_do_not() {+ // TS `renderTable` wraps every header cell in `theme.bold(...)`; data rows+ // keep the plain cell styles.let style = MarkdownStyle::default();let mut out = Vec::new();let header = vec!["a".to_string(), "b".to_string()];let rows_in = vec![vec!["1".to_string(), "2".to_string()]];let raw = vec!["| a | b |".to_string()];render_table(&header, &rows_in, &raw, 40, &style, &mut out);- for row in &out {- for span in row {- assert!(- !span.style.add_modifier.contains(Modifier::BOLD),- "no bold in table rows: {row:?}"- );- }- }- // Cell text carries the body color; borders and padding are plain.+ // Borders stay plain; the header cells are bold.assert_eq!(out[1][0].style, Style::default());+ assert!(out[1][1].style.add_modifier.contains(Modifier::BOLD));assert_eq!(out[1][1].style.fg, style.body.fg);- assert_eq!(out[1][3].style.fg, style.body.fg);+ assert!(!out[1][2].style.add_modifier.contains(Modifier::BOLD));+ // Data rows are not bold.+ assert_eq!(out[3][1].style.fg, style.body.fg);+ assert!(!out[3][1].style.add_modifier.contains(Modifier::BOLD));}#[test]diff --git a/crates/pa-tui/src/view.rs b/crates/pa-tui/src/view.rsindex 05ca63a590..62bcc2acde 100644--- a/crates/pa-tui/src/view.rs+++ b/crates/pa-tui/src/view.rs@@ -161,7 +161,7 @@ pub struct AgentView {/// The width the cached rows were laid out for.pub(crate) layout_width: usize,/// Rendering options that affect cached entry rows.- layout_options: Option<(Theme, String, bool, bool)>,+ layout_options: Option<layout::LayoutOptions>,/// Row texts of the inline frame at the last main-screen flush: the/// next flush diffs against this, so exits never duplicate scrollback.flushed_frame: Vec<String>,diff --git a/crates/pa-tui/src/view/handoff.rs b/crates/pa-tui/src/view/handoff.rsindex 31bece5086..4c120af3c5 100644--- a/crates/pa-tui/src/view/handoff.rs+++ b/crates/pa-tui/src/view/handoff.rs@@ -5,12 +5,11 @@use super::layout::EntryLayout;use super::AgentView;-use crate::theme::Theme;use std::sync::{Mutex, OnceLock};/// The render-shape inputs a packed layout's rows depend on: the width/// plus the view's `layout_options` tuple.-pub(super) type LayoutShape = (usize, (Theme, String, bool, bool));+pub(super) type LayoutShape = (usize, super::layout::LayoutOptions);/// One visible-window entry's held layouts, per detail slot (a pack is/// valid for the slot it was built under).diff --git a/crates/pa-tui/src/view/handoff_tests.rs b/crates/pa-tui/src/view/handoff_tests.rsindex 9be3af50a3..7b22c8665e 100644--- a/crates/pa-tui/src/view/handoff_tests.rs+++ b/crates/pa-tui/src/view/handoff_tests.rs@@ -43,6 +43,49 @@ fn stash(left: &mut AgentView, sequence: u64) -> (String, String, u64) {(session_id, generation, sequence)}+#[test]+fn a_changed_link_base_rejects_the_held_layout() {+ let first_path = std::env::temp_dir().join("markdown-first");+ let second_path = std::env::temp_dir().join("markdown-second");+ let first_url = url::Url::from_directory_path(&first_path)+ .unwrap()+ .join("url")+ .unwrap()+ .to_string();+ let second_url = url::Url::from_directory_path(&second_path)+ .unwrap()+ .join("url")+ .unwrap()+ .to_string();+ let _guard = HANDOFF_TEST_LOCK+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ reset();+ let entry = || ChatEntry::User {+ text: "[link](url)".to_string(),+ };+ let mut left = view();+ left.chrome.cwd = first_path.to_string_lossy().into_owned();+ left.push_entry(entry());+ let (session_id, generation, sequence) = stash(&mut left, 7);+ let mut reentry = view();+ reentry.chrome.cwd = second_path.to_string_lossy().into_owned();+ reentry.push_entry(entry());+ reentry.adopt_layout_handoff(&session_id, &generation, sequence);+ assert!(+ reentry.pending_handoff.is_some(),+ "attach key matches before render-shape validation"+ );+ ENTRY_RENDERS.with(|count| count.set(0));+ let rows = reentry.visible_transcript_window(80, 6).0;+ assert_eq!(reentry.handoff_seeds, 0);+ assert!(ENTRY_RENDERS.with(std::cell::Cell::get) > 0);+ let links = crate::hyperlinks::frame_link_ranges(&rows);+ assert!(links.iter().any(|link| link.url == second_url));+ assert!(links.iter().all(|link| link.url != first_url));+ reset();+}+#[test]fn the_reentry_window_serves_the_held_packs_byte_identically() {let _guard = HANDOFF_TEST_LOCKdiff --git a/crates/pa-tui/src/view/layout.rs b/crates/pa-tui/src/view/layout.rsindex bd7f78d613..b308a3f395 100644--- a/crates/pa-tui/src/view/layout.rs+++ b/crates/pa-tui/src/view/layout.rs@@ -7,6 +7,9 @@ use crate::chat::{render_loader, ChatEntry};use crate::chrome::render_splash;use crate::Line;+/// Inputs shared by local entry caches and cross-view handoffs.+pub(super) type LayoutOptions = (crate::theme::Theme, String, bool, bool, Option<url::Url>);+#[cfg(test)]thread_local! {pub(super) static ENTRY_VISITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };@@ -318,6 +321,7 @@ impl AgentView {self.code_block_indent.clone(),self.show_images,crate::image_component::fullscreen_image_fallback_active(),+ url::Url::from_directory_path(&self.chrome.cwd).ok(),);if self.layout_width != width || self.layout_options.as_ref() != Some(&options) {self.entry_heights.clear();diff --git a/crates/pa-tui/src/view/rows.rs b/crates/pa-tui/src/view/rows.rsindex c92f96e290..5d198bf85e 100644--- a/crates/pa-tui/src/view/rows.rs+++ b/crates/pa-tui/src/view/rows.rs@@ -4,7 +4,9 @@//! a chat entry's cached layout rows.use super::AgentView;-use crate::chat::{render_assistant, render_text_rows, render_user_block, ChatEntry};+use crate::chat::{+ render_assistant_with_style, render_text_rows, render_user_block_with_style, ChatEntry,+};use crate::theme::ThemeColor;use crate::Line;@@ -112,6 +114,9 @@ impl AgentView {rows}ChatEntry::User { text } => {+ let mut md = crate::markdown::MarkdownStyle::from_theme(&self.theme);+ md.code_block_indent.clone_from(&self.code_block_indent);+ md.link_base_url = url::Url::from_directory_path(&self.chrome.cwd).ok();let mut rows = Vec::new();// TS `addMessageToChat`: a user submission leads with `Spacer(1)` EXCEPT the// skill invocation's argument text, which joins the card below.@@ -120,12 +125,7 @@ impl AgentView {if !first && !follows_skill_card {rows.push(Vec::new());}- rows.extend(render_user_block(- text,- &self.theme,- &self.code_block_indent,- width,- ));+ rows.extend(render_user_block_with_style(text, &self.theme, &md, width));rows}ChatEntry::SlashCommand { text } => {@@ -167,16 +167,19 @@ impl AgentView {rows}ChatEntry::Assistant(message) => {+ let mut md = crate::markdown::MarkdownStyle::from_theme(&self.theme);+ md.code_block_indent.clone_from(&self.code_block_indent);+ md.link_base_url = url::Url::from_directory_path(&self.chrome.cwd).ok();// The per-entry block cache (TS's per-component `blockCache`): a streaming// message's settled blocks replay per frame; a settled message drops the copy.if message.streaming {let mut caches = self.md_caches.borrow_mut();let cache = caches.entry(index).or_default();- render_assistant(+ render_assistant_with_style(message,detail,&self.theme,- &self.code_block_indent,+ &md,width,preceded_by_tool_activity,cache,@@ -184,11 +187,11 @@ impl AgentView {} else {self.md_caches.borrow_mut().remove(&index);let mut settled = crate::markdown::MarkdownBlockCache::default();- render_assistant(+ render_assistant_with_style(message,detail,&self.theme,- &self.code_block_indent,+ &md,width,preceded_by_tool_activity,&mut settled,diff --git a/crates/pa-tui/src/view/tests.rs b/crates/pa-tui/src/view/tests.rsindex c19a6054b5..aff3cf0982 100644--- a/crates/pa-tui/src/view/tests.rs+++ b/crates/pa-tui/src/view/tests.rs@@ -12,6 +12,42 @@ fn view() -> AgentView {AgentView::new(Theme::builtin("prime", ColorMode::TrueColor))}+#[test]+fn markdown_link_targets_use_the_owning_view_cwd() {+ let first_path = std::env::temp_dir().join("markdown-first");+ let second_path = std::env::temp_dir().join("markdown-second");+ let first_url = url::Url::from_directory_path(&first_path)+ .unwrap()+ .join("url")+ .unwrap()+ .to_string();+ let second_url = url::Url::from_directory_path(&second_path)+ .unwrap()+ .join("url")+ .unwrap()+ .to_string();+ let mut first = view();+ first.chrome.cwd = first_path.to_string_lossy().into_owned();+ first.push_entry(ChatEntry::User {+ text: "[link](url)".to_string(),+ });+ let mut second = view();+ second.chrome.cwd = second_path.to_string_lossy().into_owned();+ second.push_entry(ChatEntry::User {+ text: "[link](url)".to_string(),+ });+ let targets =+ |v: &mut AgentView| crate::hyperlinks::frame_link_ranges(&v.render_transcript(80));+ let first_targets = targets(&mut first);+ let second_targets = targets(&mut second);+ assert!(first_targets.iter().any(|link| link.url == first_url));+ assert!(second_targets.iter().any(|link| link.url == second_url));+ assert_eq!(targets(&mut first), first_targets);+ first.chrome.cwd = second_path.to_string_lossy().into_owned();+ assert_eq!(targets(&mut first), second_targets);+ assert!(targets(&mut first).iter().all(|link| link.url != first_url));+}+/// The hardware caret never sits over the factory page: the page is an/// input-less replacement view, so `frame_cursor` suppresses the stale/// editor dock position while it is open (the settings menu's guarddiff --git a/crates/pa-tui/tests/replay_tables.rs b/crates/pa-tui/tests/replay_tables.rsindex fb235eec74..5e2fd7a102 100644--- a/crates/pa-tui/tests/replay_tables.rs+++ b/crates/pa-tui/tests/replay_tables.rs@@ -69,12 +69,12 @@ fn replayed_table_renders_boxed_aligned_rows() {// Links render the legacy observability form under the gate (tmux// parity): the label, then the bracketed URL in the dim slot.assert!(- flat.contains("docs [https://example.com/docs]"),+ flat.contains("docs (https://example.com/docs)"),"link row: {flat}");// The second link wraps mid-row at this width, so only its URL tail// stays contiguous.- assert!(flat.contains("example.com/log]"), "link row 2: {flat}");+ assert!(flat.contains("example.com/log)"), "link row 2: {flat}");}#[test]diff --git a/scripts/review_markdown_frames_parity.py b/scripts/review_markdown_frames_parity.pynew file mode 100644index 0000000000..d7b5211ae2--- /dev/null+++ b/scripts/review_markdown_frames_parity.py@@ -0,0 +1,315 @@+#!/usr/bin/env python3+"""Pinned real-binary Markdown assistant-history frames, including SGR and OSC8.++Compares the bounded transcript region between literal sentinel paragraphs, not+the unrelated app chrome. Does not claim streaming, scrolling or mouse parity.+Only hosted Linux runs execute binaries; raw PTY captures remain in the receipt.+"""+import argparse+import codecs+import fcntl+import json+import os+from pathlib import Path+import pty+import re+import struct+import subprocess+import sys+import tempfile+import termios+import unicodedata++from review_draft_disconnect_parity import (+ Screen, Supervisor, Terminal, actual_hello, digest, env_for,+ TS_ARCHIVE_SHA256, TS_SOURCE_COMMIT,+)++START, END = "MARKDOWN FRAME START", "MARKDOWN FRAME END"+CORPUS = {+ "adjacent_emphasis": "**bold***italic*\n**outer *inner***\n**outer *inner* tail***next*\n**outer `*` tail***next*\n" + r'''*a\*b* **a\**b**+***a\*b*** ~~a\~~b~~+*a\\* tail+**a *b** **a*b**''',+ "escaped_titles": r'''[t](https://example.com "a \"b\"")+[t](https://example.com 'a \'b\'')+[t](foo(bar) "title")+[t](foo\(bar\) "a \"b\"")+[t](foo(bar "title")''',+ "headings": "# Heading one\n## Heading two\n#### Heading four\n###### Heading six",+ "nested_lists": "1. parent\n - child\n - leaf\n continuation\n2. sibling",+ "quotes": "> quoted **bold** and `code`\n> lazy continuation",+ "fences": "~~~python\nx = 1\nprint(x)\n~~~",+ "escapes_images": r"\*literal\* snake_case_name \",+ "unicode_wrap": "A **styledword**. Unicode café 界 words with `code` and trailing punctuation. " * 2,+}+++class StyledScreen(Screen):+ """Reuse the existing VT grid; retain style/link state on every painted cell."""+ def __init__(self, width, height=40):+ super().__init__(width, height)+ self.stream = ""+ self.stream_decoder = codecs.getincrementaldecoder("utf-8")("strict")+ self.fg = self.bg = self.link = None+ self.mods = set()+ self.styles = [[None] * width for _ in range(height)]+ self.style_errors = []+ self.keyboard_controls = []++ def sgr(self, params):+ nums = [int(x or 0) for x in params.split(";")]+ i = 0+ while i < len(nums):+ n = nums[i]+ if n == 0:+ self.fg = self.bg = None+ self.mods.clear()+ elif n in (1, 2, 3, 4, 5, 7, 8, 9): self.mods.add(n)+ elif n == 22: self.mods.difference_update((1, 2))+ elif n in (23, 24, 25, 27, 28, 29): self.mods.discard(n - 20)+ elif n in (39, 49):+ if n == 39: self.fg = None+ else: self.bg = None+ elif 30 <= n <= 37 or 90 <= n <= 97:+ self.fg = ("indexed", n - 30 if n < 90 else n - 90 + 8)+ elif 40 <= n <= 47 or 100 <= n <= 107:+ self.bg = ("indexed", n - 40 if n < 100 else n - 100 + 8)+ elif n in (38, 48) and i + 1 < len(nums):+ mode = nums[i + 1]+ count = 1 if mode == 5 else (3 if mode == 2 else 0)+ if not count or i + 2 + count > len(nums):+ self.style_errors.append(f"unsupported color SGR {params}")+ return+ color = tuple(["indexed" if mode == 5 else "rgb"] + nums[i + 2:i + 2 + count])+ if n == 38: self.fg = color+ else: self.bg = color+ i += 1 + count+ else: self.style_errors.append(f"unsupported SGR {n}")+ i += 1++ def feed(self, data):+ self.stream += self.stream_decoder.decode(data)+ replies = []+ while self.stream:+ if self.stream.startswith("\x1b"):+ if len(self.stream) < 2: break+ if self.stream[1] in "]P_^":+ end = re.search(r"\x07|\x1b\\", self.stream[2:])+ if end is None: break+ size = 2 + end.end()+ token, self.stream = self.stream[:size], self.stream[size:]+ if token.startswith("\x1b]8;"):+ payload = token[2:-len(end.group())]+ self.link = payload.split(";", 2)[2] or None+ replies.extend(super().feed(token.encode()))+ continue+ if self.stream[1] == "[":+ hit = re.match(r"\x1b\[([0-?]*)([ -/]*)([@-~])", self.stream)+ if hit is None: break+ token, self.stream = self.stream[:hit.end()], self.stream[hit.end():]+ params, _, command = hit.groups()+ # These private controls configure/query keyboard input. They+ # never modify painted cells; interpreting >4 as SGR4 would+ # invent underline, and int('>4') is not a color parser.+ if command == 'm' and params in ('>4;0', '>4;1', '>4;2'):+ self.keyboard_controls.append(token)+ continue+ if command == 'u' and params == '?':+ self.keyboard_controls.append(token)+ # A flags response, even zero, advertises Kitty support.+ # Stay silent; the following DA1 query is answered by the+ # legacy Screen and settles crossterm's probe as non-Kitty.+ continue+ if command == 'u' and (re.fullmatch(r'[>=]\d+(?:;\d+)?', params)+ or re.fullmatch(r'<\d*', params)):+ self.keyboard_controls.append(token)+ continue+ if params.startswith(('?', '<', '>')) and command not in 'hlnc':+ self.style_errors.append(f"unsupported private VT command {token!r}")+ continue+ nums = [int(x) if x.isdigit() else 0 for x in params.lstrip("?<>").split(";")]+ n = nums[0] or 1+ if command == "m": self.sgr(params)+ elif command == "J":+ if nums[0] in (2, 3): self.styles = [[None] * self.width for _ in range(self.height)]+ elif nums[0] == 0:+ self.styles[self.row][self.col:] = [None] * (self.width - self.col)+ for row in range(self.row + 1, self.height): self.styles[row] = [None] * self.width+ elif command == "K":+ start, end = (0, self.width) if nums[0] == 2 else ((0, self.col + 1) if nums[0] == 1 else (self.col, self.width))+ self.styles[self.row][start:end] = [None] * (end - start)+ elif command == "P":+ row = self.styles[self.row]+ row[self.col:] = (row[self.col + n:] + [None] * n)[:self.width - self.col]+ elif command == "X":+ end = min(self.width, self.col + n)+ self.styles[self.row][self.col:end] = [None] * (end - self.col)+ elif command not in "HfABCDGnc" and not (command in "hl" and params.startswith("?")):+ self.style_errors.append(f"unsupported VT command {token!r}")+ replies.extend(super().feed(token.encode()))+ continue+ size = 3 if self.stream[1] in "()" else 2+ if len(self.stream) < size: break+ token, self.stream = self.stream[:size], self.stream[size:]+ if token[1] not in "78()=><":+ self.style_errors.append(f"unsupported VT escape {token!r}")+ replies.extend(super().feed(token.encode()))+ continue+ char, self.stream = self.stream[0], self.stream[1:]+ if char == "\n" and self.row == self.height - 1:+ self.styles.pop(0)+ self.styles.append([None] * self.width)+ elif char >= " " and not unicodedata.combining(char):+ row, col = self.row, self.col+ if col >= self.width: col, row = 0, min(self.height - 1, row + 1)+ self.styles[row][col] = (self.fg, self.bg, tuple(sorted(self.mods)), self.link)+ if unicodedata.east_asian_width(char) in "WF" and col + 1 < self.width:+ self.cells[row][col + 1] = " "+ self.styles[row][col + 1] = None+ if char == "\u200d" or "\ufe00" <= char <= "\ufe0f":+ self.style_errors.append("unsupported joined/variation-selector grapheme")+ elif unicodedata.combining(char) and self.col > 0:+ self.cells[self.row][self.col - 1] += char+ replies.extend(super().feed(char.encode()))+ return replies++ def region(self):+ lines = ["".join(row) for row in self.cells]+ starts = [(n, row.index(START)) for n, row in enumerate(lines) if START in row]+ ends = [n for n, row in enumerate(lines) if END in row]+ if len(starts) != 1 or len(ends) != 1 or ends[0] <= starts[0][0]:+ raise ValueError("complete bounded Markdown frame is not visible")+ first, left = starts[0]+ rows = []+ for y in range(first + 1, ends[0]):+ last = len(lines[y].rstrip())+ rows.append([(self.cells[y][x], None if self.cells[y][x].isspace() else self.styles[y][x])+ for x in range(left, max(left, last))])+ while rows and not rows[0]: rows.pop(0)+ while rows and not rows[-1]: rows.pop()+ return rows+++class StyledTerminal(Terminal):+ def __init__(self, command, root, env, width):+ self.master, slave = pty.openpty()+ attrs = termios.tcgetattr(slave)+ attrs[3] &= ~(termios.ECHO | termios.ICANON)+ termios.tcsetattr(slave, termios.TCSANOW, attrs)+ fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack("HHHH", 40, width, 0, 0))+ self.process = subprocess.Popen(command, cwd=root, env=env, stdin=slave,+ stdout=slave, stderr=slave, start_new_session=True)+ os.close(slave)+ self.screen, self.raw = StyledScreen(width), bytearray()+++class MarkdownSupervisor(Supervisor):+ def __init__(self, path, hello, root, cwd):+ self.cwd = str(cwd)+ super().__init__(path, hello, 'markdown', root)++ def send(self, sock, data):+ if data.get('type') == 'response' and data.get('command') == 'attach':+ data['data']['snapshot']['state']['cwd'] = self.cwd+ data['data']['snapshot']['summary']['cwd'] = self.cwd+ elif data.get('type') == 'response' and data.get('command') in ('get_state', 'get_connection_state'):+ data['data']['cwd'] = self.cwd+ super().send(sock, data)+++def assistant(text):+ return {"role": "assistant", "stopReason": "stop", "api": "openai-completions",+ "provider": "parity-local", "model": "parity-1", "timestamp": 1750000000000,+ "content": [{"type": "text", "text": f"{START}\n\n{text}\n\n{END}"}],+ "usage": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0, "totalTokens": 0,+ "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0, "total": 0}}}+++def capture(kind, binary, root, hello, text, width, cwd):+ root.mkdir()+ env = env_for(root, kind)+ server = MarkdownSupervisor(root / "fixture.sock", hello, root, cwd)+ server.messages = [assistant(text)]+ terminal = None+ result = {"executed": False}+ try:+ terminal = StyledTerminal([str(binary), "--daemon-socket", str(server.path)], cwd, env, width)+ terminal.wait("Markdown history fully painted", lambda: START in terminal.screen.text()+ and END in terminal.screen.text(), server)+ if terminal.screen.style_errors: raise RuntimeError(terminal.screen.style_errors)+ region = terminal.screen.region()+ painted = [style for row in region for char, style in row if not char.isspace()]+ if not painted:+ raise RuntimeError("Markdown corpus body was not painted")+ if r'\' in text:+ targets = {style[3] for style in painted if style and style[3]}+ expected = cwd.as_uri() + '/url'+ if targets != {expected}:+ raise RuntimeError(f"relative hyperlink target differs: {targets!r} vs {expected!r}")+ if "**bold***italic*" in text and not all(any(style and modifier in style[2] for style in painted)+ for modifier in (1, 3)):+ raise RuntimeError("actual PTY did not render both bold and italic modifiers")+ result.update(executed=True, styled_region=region, commands=server.commands,+ osc8_observed=any(style and style[3] for style in painted),+ keyboard_controls=terminal.screen.keyboard_controls,+ explicit_session_cwd=str(cwd),+ screen=terminal.screen.text())+ except Exception as error: result["error"] = f"{type(error).__name__}: {error}"+ finally:+ try:+ if terminal:+ result["raw_terminal"] = bytes(terminal.raw).decode("utf-8", "replace")+ terminal.close()+ finally:+ server.close()+ return result+++def main():+ parser = argparse.ArgumentParser()+ parser.add_argument("--receipt", type=Path, required=True)+ args = parser.parse_args()+ receipt = {"verified": False, "results": {}, "comparisons": {}, "corpus": CORPUS,+ "scope": "Actual assistant-history transcript region cells, foreground/background, modifiers and OSC8 at widths40/80/120; excludes chrome, streaming, scrolling and mouse actions.",+ "source_commit": TS_SOURCE_COMMIT,+ "ci": {k: os.environ.get(k) for k in ("GITHUB_RUN_ID", "GITHUB_SHA")}}+ try:+ if sys.platform != "linux": raise RuntimeError("binary execution requires hosted Linux")+ if digest(Path(os.environ["PA_TS_ARCHIVE"])) != TS_ARCHIVE_SHA256:+ raise RuntimeError("pinned official TS archive hash mismatch")+ receipt["ts_archive_sha256"] = TS_ARCHIVE_SHA256+ with tempfile.TemporaryDirectory(prefix="pa-md-frames-") as temp:+ cwd = Path(temp) / 'project'+ cwd.mkdir()+ for kind in ("ts", "rust"):+ binary = Path(os.environ[f"PA_{kind.upper()}_BINARY"]).resolve()+ root = Path(temp) / kind+ root.mkdir()+ env = env_for(root, kind)+ hello = actual_hello(binary, root, env)+ side = receipt["results"][kind] = {"binary_sha256": digest(binary), "hello": hello, "cases": {}}+ version = subprocess.run([str(binary), "--version"], cwd=root, env=env,+ capture_output=True, text=True, timeout=15, check=True).stdout.strip()+ side["version"] = version+ if kind == "ts" and "0.9.8" not in version:+ raise RuntimeError("unexpected TS binary version")+ for name, text in CORPUS.items():+ for width in (40, 80, 120):+ key = f"{name}-{width}"+ side["cases"][key] = capture(kind, binary, root / key, hello, text, width, cwd)+ for key in receipt["results"]["ts"]["cases"]:+ a, b = [receipt["results"][kind]["cases"][key] for kind in ("ts", "rust")]+ receipt["comparisons"][key] = bool(a.get("executed") and b.get("executed")+ and a["styled_region"] == b["styled_region"])+ receipt["verified"] = bool(receipt["comparisons"] and all(receipt["comparisons"].values()))+ except Exception as error: receipt["error"] = f"{type(error).__name__}: {error}"+ args.receipt.parent.mkdir(parents=True, exist_ok=True)+ args.receipt.write_text(json.dumps(receipt, indent=2, sort_keys=True) + "\n")+ print(json.dumps({"verified": receipt["verified"], "comparisons": receipt["comparisons"],+ "error": receipt.get("error")}))+ return 0 if receipt["verified"] else 1+++if __name__ == "__main__":+ sys.exit(main())diff --git a/scripts/test_review_markdown_frames_parity.py b/scripts/test_review_markdown_frames_parity.pynew file mode 100644index 0000000000..abdf8b26e6--- /dev/null+++ b/scripts/test_review_markdown_frames_parity.py@@ -0,0 +1,65 @@+#!/usr/bin/env python3+"""Lightweight capture parser checks; never execute product binaries."""+import unittest+from review_markdown_frames_parity import StyledScreen, START, END, CORPUS+++class CaptureTests(unittest.TestCase):+ def test_captured_keyboard_controls_are_not_sgr(self):+ screen = StyledScreen(40)+ screen.feed(b'\x1b[1mA\x1b[>4;0mB')+ replies = screen.feed(b'\x1b[?uC')+ self.assertEqual(replies, [])+ self.assertEqual(screen.keyboard_controls, ['\x1b[>4;0m', '\x1b[?u'])+ self.assertEqual(screen.styles[0][:3], [(None, None, (1,), None)] * 3)+ self.assertEqual(screen.style_errors, [])++ def test_partial_keyboard_query_and_config_wait_for_terminator(self):+ screen = StyledScreen(40)+ self.assertEqual(screen.feed(b'\x1b[>4;'), [])+ self.assertEqual(screen.keyboard_controls, [])+ screen.feed(b'0m\x1b[?')+ self.assertEqual(screen.keyboard_controls, ['\x1b[>4;0m'])+ self.assertEqual(screen.feed(b'u\x1b[c'), [b'\x1b[?1;2c'])+ self.assertEqual(screen.style_errors, [])++ def test_unknown_private_styles_and_geometry_still_fail(self):+ screen = StyledScreen(40)+ screen.feed(b'\x1b[>31m\x1b[3S')+ self.assertEqual(len(screen.style_errors), 2)+ self.assertEqual(screen.fg, None)+ self.assertEqual(screen.mods, set())++ def test_fragmented_sgr_and_osc8_keep_cell_attributes(self):+ screen = StyledScreen(40)+ chunks = [b'\x1b[38;2;1;', b'2;3m\x1b[1mA\x1b[22mB',+ b'\x1b]8;;https://example.com\x1b\\C\x1b]8;;\x1b\\', b'D']+ for chunk in chunks:+ screen.feed(chunk)+ self.assertEqual(screen.cells[0][:4], list('ABCD'))+ self.assertEqual(screen.styles[0][:4], [+ (('rgb', 1, 2, 3), None, (1,), None),+ (('rgb', 1, 2, 3), None, (), None),+ (('rgb', 1, 2, 3), None, (), 'https://example.com'),+ (('rgb', 1, 2, 3), None, (), None)])+ self.assertEqual(screen.style_errors, [])++ def test_repaint_clears_stale_text_and_style(self):+ screen = StyledScreen(40)+ screen.feed(b'\x1b[1mABCDE\r\x1b[0mXY\x1b[K')+ self.assertEqual(screen.cells[0][:5], list('XY '))+ self.assertEqual(screen.styles[0][2:5], [None] * 3)++ def test_sentinel_crop_preserves_indentation_and_nonblank_styles(self):+ screen = StyledScreen(40)+ screen.feed(f' {START}\r\n '.encode() + b'\x1b[3mtext\x1b[0m\r\n' + f' {END}'.encode())+ self.assertEqual(screen.region(), [[(' ', None), (' ', None)] ++ [(char, (None, None, (3,), None)) for char in 'text']])++ def test_title_corpus_has_single_backslash_escapes(self):+ self.assertIn(r'\"b\"', CORPUS['escaped_titles'])+ self.assertNotIn(r'\\"', CORPUS['escaped_titles'])+++if __name__ == '__main__':+ unittest.main()