| 1 | //! Terminal markdown rendering — a port of the fx coding agent's markdown |
| 2 | //! processing (`src/core/agent/presentation/`). The fx design splits work into |
| 3 | //! block parsing, inline emphasis rendering, block rendering, and ANSI styling |
| 4 | //! so each concern can scale independently. This module mirrors that layering: |
| 5 | //! |
| 6 | //! - [`block_parse`] recognizes line-level constructs (headings, fences, lists, |
| 7 | //! blockquotes, tables, horizontal rules, task list items, setext underlines). |
| 8 | //! - [`write_inline`] turns inline emphasis (`**bold**`, `*italic*`, `_em_`, |
| 9 | //! `` `code` ``, `~~strike~~`, `[text](url)`, `<autolink>`, bare URLs) into |
| 10 | //! SGR-styled bytes with correctly nested open/close sequences. |
| 11 | //! - [`render_markdown`] emits the final styled bytes for each block, using |
| 12 | //! the theme colors from [`theme::Theme`]. |
| 13 | //! |
| 14 | //! The public entry point is [`render_markdown`], a one-shot renderer used by |
| 15 | //! the TUI's assistant-text and tool-output paths. A streaming wrapper |
| 16 | //! ([`MarkdownStream`]) is re-exported from [`md`] for backward compatibility. |
| 17 | |
| 18 | #![allow(dead_code |
| 19 | |
| 20 | use std::fmt:: |
| 21 | |
| 22 | use crate::theme::Theme |
| 23 | |
| 24 | // ─── ANSI styling primitives (port of ansi.zig) ───────────────────────────── |
| 25 | |
| 26 | /// SGR escape sequences grouped by role, resolved from a [`Theme`]. fx uses |
| 27 | /// semantic open/close pairs (bold_open/bold_close) rather than raw colors so |
| 28 | /// inline emphasis can nest without leaking attributes across spans. |
| 29 | pub struct AnsiStyles |
| 30 | pub bold_open: &' |
| 31 | pub bold_close: &' |
| 32 | pub italic_open: &' |
| 33 | pub italic_close: &' |
| 34 | pub dim_open: String |
| 35 | pub dim_close: &' |
| 36 | pub underline_open: &' |
| 37 | pub underline_close: &' |
| 38 | pub strike_open: &' |
| 39 | pub strike_close: &' |
| 40 | pub code_open: String |
| 41 | pub code_close: &' |
| 42 | pub hr_char: char |
| 43 | pub bullet: &' |
| 44 | pub rule_prefix: &' |
| 45 | pub task_pending: &' |
| 46 | pub task_done: &' |
| 47 | pub col_sep: &' |
| 48 | pub junction: &' |
| 49 | pub table_horiz: char |
| 50 | pub link_color: String |
| 51 | } |
| 52 | |
| 53 | impl AnsiStyles { |
| 54 | /// Resolve styles from a theme. fx's presentation layer uses a fixed |
| 55 | /// scheme — real SGR dim for markers/rules, 256-color 245 for inline code |
| 56 | /// — so the transcript looks the same under every theme. |
| 57 | pub fn from_theme(_theme |
| 58 | AnsiStyles { |
| 59 | bold_open: "\x1b[1m |
| 60 | bold_close: "\x1b[22m |
| 61 | italic_open: "\x1b[3m |
| 62 | italic_close: "\x1b[23m |
| 63 | // fx writeDim: discrete dim attribute, not a gray foreground. |
| 64 | dim_open: "\x1b[2m |
| 65 | dim_close: "\x1b[22m |
| 66 | underline_open: "\x1b[4m |
| 67 | underline_close: "\x1b[24m |
| 68 | strike_open: "\x1b[9m |
| 69 | strike_close: "\x1b[29m |
| 70 | // fx inline code: dark palette 245, closed with default-fg 39. |
| 71 | code_open: "\x1b[38;5;245m |
| 72 | code_close: "\x1b[39m |
| 73 | hr_char: '─', |
| 74 | bullet: "• ", |
| 75 | rule_prefix: "│ ", |
| 76 | task_pending: "☐", |
| 77 | task_done: "✓", |
| 78 | col_sep: " │ ", |
| 79 | junction: "─┼─", |
| 80 | table_horiz: '─', |
| 81 | link_color: _theme.md_link |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // ─── Text utilities (port of text_util.zig) ──────────────────────────────── |
| 87 | |
| 88 | fn left_trim(line: |
| 89 | line.trim_start_matches(|c |
| 90 | } |
| 91 | |
| 92 | fn is_space(c: |
| 93 | c == b' |
| 94 | } |
| 95 | |
| 96 | fn is_blank_line(line: |
| 97 | line.trim_matches(|c |
| 98 | } |
| 99 | |
| 100 | fn is_ascii_alpha(c: |
| 101 | c.is_ascii_alphabetic() |
| 102 | } |
| 103 | |
| 104 | fn is_ascii_alphanumeric(c: |
| 105 | c.is_ascii_alphanumeric() |
| 106 | } |
| 107 | |
| 108 | fn is_ascii_word_byte(c: |
| 109 | is_ascii_alphanumeric(c) || |
| 110 | } |
| 111 | |
| 112 | fn is_ascii_whitespace(c: |
| 113 | matches!(c, |
| 114 | } |
| 115 | |
| 116 | fn is_trailing_url_punctuation(c: |
| 117 | matches!(c, |
| 118 | } |
| 119 | |
| 120 | fn is_escapable_punctuation(byte: |
| 121 | matches!( |
| 122 | byte, |
| 123 | b'!' | b' |
| 124 | | b'#' |
| 125 | | b'$' |
| 126 | | b'%' |
| 127 | | b'&' |
| 128 | | b'\'' |
| 129 | | b'(' |
| 130 | | b')' |
| 131 | | b'*' |
| 132 | | b'+' |
| 133 | | b',' |
| 134 | | b'-' |
| 135 | | b'.' |
| 136 | | b'/' |
| 137 | | b':' |
| 138 | | b';' |
| 139 | | b'<' |
| 140 | | b'=' |
| 141 | | b'>' |
| 142 | | b'?' |
| 143 | | b'@' |
| 144 | | b'[' |
| 145 | | b'\\' |
| 146 | | b']' |
| 147 | | b'^' |
| 148 | | b'_' |
| 149 | | b'`' |
| 150 | | b'{' |
| 151 | | b'|' |
| 152 | | b'}' |
| 153 | | b'~' |
| 154 | ) |
| 155 | } |
| 156 | |
| 157 | /// True when the byte at `index` is escaped by an odd number of backslashes. |
| 158 | fn is_escaped_punctuation_at(text: |
| 159 | let bytes = |
| 160 | if index == 0 |
| 161 | return false; |
| 162 | } |
| 163 | let mut slash_count |
| 164 | let mut cursor |
| 165 | while cursor > 0 |
| 166 | slash_count += 1; |
| 167 | cursor -= 1; |
| 168 | } |
| 169 | slash_count % 2 == |
| 170 | } |
| 171 | |
| 172 | /// Append `text` with backslash-escaped punctuation unwrapped to its literal |
| 173 | /// character (e.g. `\*` → `*`), matching fx's `appendEscapedPunctuation`. |
| 174 | fn append_escaped_punctuation(out: |
| 175 | let bytes = |
| 176 | let mut i |
| 177 | while i < bytes |
| 178 | if bytes[i] |
| 179 | out.push(bytes |
| 180 | i += 2; |
| 181 | continue; |
| 182 | } |
| 183 | // Non-ASCII: push the full UTF-8 character (see write_inline). |
| 184 | if bytes[i] |
| 185 | let ch = |
| 186 | out.push(ch |
| 187 | i += ch. |
| 188 | continue; |
| 189 | } |
| 190 | out.push(bytes |
| 191 | i += 1; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /// Strip a trailing backslash hard-break marker when the line ends a paragraph |
| 196 | /// (fx renders the line break, not the marker). |
| 197 | fn without_terminal_hard_break(line: |
| 198 | if !has_lf || |
| 199 | return line; |
| 200 | } |
| 201 | let bytes = |
| 202 | if *bytes.last |
| 203 | return line; |
| 204 | } |
| 205 | let mut slash_start |
| 206 | while slash_start > 0 |
| 207 | slash_start -= 1; |
| 208 | } |
| 209 | if (bytes.len |
| 210 | return line; |
| 211 | } |
| 212 | &line[..bytes. |
| 213 | } |
| 214 | |
| 215 | /// A run of `marker_len` underscores (1 or 2) not adjacent to other underscores. |
| 216 | fn is_exact_underscore_run(text: |
| 217 | if marker_len != 1 |
| 218 | return false; |
| 219 | } |
| 220 | let bytes = |
| 221 | if index + marker_len |
| 222 | return false; |
| 223 | } |
| 224 | if index > 0 |
| 225 | return false; |
| 226 | } |
| 227 | if index + marker_len |
| 228 | return false; |
| 229 | } |
| 230 | bytes[index..index |
| 231 | } |
| 232 | |
| 233 | /// Intraword underscores must not open emphasis (CommonMark left-flanking). |
| 234 | fn is_valid_underscore_open(text: |
| 235 | if !is_exact_underscore_run(text |
| 236 | return false; |
| 237 | } |
| 238 | let bytes = |
| 239 | let after = |
| 240 | if after >= bytes |
| 241 | return false; |
| 242 | } |
| 243 | index == 0 || |
| 244 | } |
| 245 | |
| 246 | /// Intraword underscores must not close emphasis (CommonMark right-flanking). |
| 247 | fn is_valid_underscore_close(text: |
| 248 | if !is_exact_underscore_run(text |
| 249 | return false; |
| 250 | } |
| 251 | let bytes = |
| 252 | if index == 0 |
| 253 | return false; |
| 254 | } |
| 255 | let after = |
| 256 | after >= bytes. |
| 257 | } |
| 258 | |
| 259 | fn find_underscore_closer(text: |
| 260 | let bytes = |
| 261 | let mut i |
| 262 | let mut in_code |
| 263 | while i < bytes |
| 264 | if bytes[i] |
| 265 | in_code = !in_code |
| 266 | i += 1; |
| 267 | continue; |
| 268 | } |
| 269 | if !in_code && |
| 270 | return Some(i |
| 271 | } |
| 272 | i += 1; |
| 273 | } |
| 274 | None |
| 275 | } |
| 276 | |
| 277 | // ─── Block parsing (port of block_parse.zig) ───────────────────────────────── |
| 278 | |
| 279 | pub mod block_parse { |
| 280 | use super::*; |
| 281 | |
| 282 | /// The fence marker char (`` ` `` or `~`) if this line opens/closes one. |
| 283 | pub fn code_fence_marker(line |
| 284 | let bytes = |
| 285 | if bytes.len() |
| 286 | return None; |
| 287 | } |
| 288 | let marker = |
| 289 | if marker != b |
| 290 | return None; |
| 291 | } |
| 292 | if bytes[1] |
| 293 | return None; |
| 294 | } |
| 295 | Some(marker) |
| 296 | } |
| 297 | |
| 298 | fn is_code_fence(line: |
| 299 | code_fence_marker(line).is_some |
| 300 | } |
| 301 | |
| 302 | pub fn code_fence_language(line |
| 303 | let info = |
| 304 | let end = |
| 305 | .find(|c: |
| 306 | .unwrap_or(info.len |
| 307 | &info[..end] |
| 308 | } |
| 309 | |
| 310 | pub fn parse_header(line |
| 311 | let bytes = |
| 312 | if bytes.is_empty() |
| 313 | return None; |
| 314 | } |
| 315 | let mut level |
| 316 | while level < 6 |
| 317 | level += 1; |
| 318 | } |
| 319 | if level == 0 |
| 320 | return None; |
| 321 | } |
| 322 | if bytes[level] |
| 323 | return None; |
| 324 | } |
| 325 | Some((level, |
| 326 | } |
| 327 | |
| 328 | /// Setext heading underline: a line of `=` (h1) or `-` (h2) under text. |
| 329 | pub fn parse_setext_underline(line |
| 330 | let mut level |
| 331 | for &byte in |
| 332 | if byte == b |
| 333 | continue; |
| 334 | } |
| 335 | let next_level = |
| 336 | b'=' => 1, |
| 337 | b'-' => 2, |
| 338 | _ => return None |
| 339 | }; |
| 340 | if let Some |
| 341 | if existing != next_level |
| 342 | return None; |
| 343 | } |
| 344 | } else { |
| 345 | level = Some |
| 346 | } |
| 347 | } |
| 348 | level |
| 349 | } |
| 350 | |
| 351 | pub fn is_setext_candidate(line |
| 352 | if line.is_empty() |
| 353 | return false; |
| 354 | } |
| 355 | let first = |
| 356 | if first == b |
| 357 | return false; |
| 358 | } |
| 359 | if parse_header(line) |
| 360 | return false; |
| 361 | } |
| 362 | if parse_unordered_list(line) |
| 363 | return false; |
| 364 | } |
| 365 | !is_code_fence(line) |
| 366 | && !is_pipe_line(line |
| 367 | && !is_horizontal_rule(line |
| 368 | && parse_setext_underline(line) |
| 369 | } |
| 370 | |
| 371 | #[derive(Debug |
| 372 | pub struct BlockquotePrefix |
| 373 | pub indent: usize |
| 374 | pub depth: usize |
| 375 | } |
| 376 | |
| 377 | pub fn parse_blockquote(line |
| 378 | let bytes = |
| 379 | let mut i |
| 380 | while i < bytes |
| 381 | i += 1; |
| 382 | } |
| 383 | let indent_end = |
| 384 | if i == bytes |
| 385 | return None; |
| 386 | } |
| 387 | i += 1; |
| 388 | if i == bytes |
| 389 | return Some(BlockquotePrefix |
| 390 | indent: indent_end, |
| 391 | depth: 1, |
| 392 | }); |
| 393 | } |
| 394 | if bytes[i] |
| 395 | return None; |
| 396 | } |
| 397 | i += 1; |
| 398 | let mut depth |
| 399 | while i < bytes |
| 400 | let after_marker = |
| 401 | if after_marker < bytes |
| 402 | break; |
| 403 | } |
| 404 | depth += 1; |
| 405 | i = after_marker; |
| 406 | if i == bytes |
| 407 | break; |
| 408 | } |
| 409 | i += 1; |
| 410 | } |
| 411 | Some(BlockquotePrefix |
| 412 | indent: indent_end, |
| 413 | depth, |
| 414 | }) |
| 415 | } |
| 416 | |
| 417 | pub fn parse_unordered_list(line |
| 418 | let bytes = |
| 419 | let mut i |
| 420 | while i < bytes |
| 421 | i += 1; |
| 422 | } |
| 423 | if i + 1 |
| 424 | return None; |
| 425 | } |
| 426 | if bytes[i] |
| 427 | return None; |
| 428 | } |
| 429 | if bytes[i |
| 430 | return None; |
| 431 | } |
| 432 | Some((&line |
| 433 | } |
| 434 | |
| 435 | pub fn parse_ordered_list(line |
| 436 | let bytes = |
| 437 | let mut i |
| 438 | while i < bytes |
| 439 | i += 1; |
| 440 | } |
| 441 | let indent_end = |
| 442 | while i < bytes |
| 443 | i += 1; |
| 444 | } |
| 445 | if i == indent_end |
| 446 | return None; |
| 447 | } |
| 448 | if i + 1 |
| 449 | return None; |
| 450 | } |
| 451 | if bytes[i] |
| 452 | return None; |
| 453 | } |
| 454 | if bytes[i |
| 455 | return None; |
| 456 | } |
| 457 | Some((&line |
| 458 | } |
| 459 | |
| 460 | pub fn parse_task_list_item(content |
| 461 | let bytes = |
| 462 | if bytes.len() |
| 463 | return None; |
| 464 | } |
| 465 | let completed = |
| 466 | b' ' => false, |
| 467 | b'x' | b' |
| 468 | _ => return None |
| 469 | }; |
| 470 | if bytes.len() |
| 471 | return Some((completed |
| 472 | } |
| 473 | if bytes[3] |
| 474 | return None; |
| 475 | } |
| 476 | Some((completed, |
| 477 | } |
| 478 | |
| 479 | pub fn is_horizontal_rule(line |
| 480 | let trimmed = |
| 481 | if trimmed.len() |
| 482 | return false; |
| 483 | } |
| 484 | let rule_char = |
| 485 | if rule_char != b |
| 486 | return false; |
| 487 | } |
| 488 | let mut count |
| 489 | for &c in |
| 490 | if c == rule_char |
| 491 | count += 1; |
| 492 | } else if c |
| 493 | return false; |
| 494 | } |
| 495 | } |
| 496 | count >= 3 |
| 497 | } |
| 498 | |
| 499 | pub fn is_pipe_line(line |
| 500 | let trimmed = |
| 501 | if trimmed.is_empty() |
| 502 | return false; |
| 503 | } |
| 504 | for (index, & |
| 505 | if byte == b |
| 506 | return true; |
| 507 | } |
| 508 | } |
| 509 | false |
| 510 | } |
| 511 | |
| 512 | fn is_separator_line(line: |
| 513 | let trimmed = |
| 514 | if trimmed.is_empty() |
| 515 | return false; |
| 516 | } |
| 517 | let mut seen_dash |
| 518 | let mut seen_pipe |
| 519 | for &c in |
| 520 | match c { |
| 521 | b'|' => seen_pipe |
| 522 | b':' | b' |
| 523 | b'-' => seen_dash |
| 524 | _ => return false |
| 525 | } |
| 526 | } |
| 527 | seen_dash && seen_pipe |
| 528 | } |
| 529 | |
| 530 | /// A valid GFM table needs a header, a separator (`| --- |`), and rows. |
| 531 | pub fn is_valid_table(buf |
| 532 | let mut line_count |
| 533 | let mut saw_separator |
| 534 | for line in buf |
| 535 | if line_count == 1 |
| 536 | saw_separator = true; |
| 537 | } |
| 538 | line_count += 1; |
| 539 | } |
| 540 | line_count >= 2 && |
| 541 | } |
| 542 | |
| 543 | pub fn definition_marker_body(line |
| 544 | let bytes = |
| 545 | if bytes.len() |
| 546 | return None; |
| 547 | } |
| 548 | let mut body_start |
| 549 | while body_start < bytes |
| 550 | && (bytes[body_start |
| 551 | { |
| 552 | body_start += 1; |
| 553 | } |
| 554 | if body_start == 1 |
| 555 | return None; |
| 556 | } |
| 557 | Some(&line |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | // ─── Inline rendering (port of inline_render.zig) ─────────────────────────── |
| 562 | |
| 563 | /// Render inline markdown emphasis into `out` with SGR styling. Mirrors fx's |
| 564 | /// state machine: bold/italic/underscore-bold/underscore-italic/strike/code are |
| 565 | /// tracked independently so nesting (e.g. `**bold _and italic_**`) restores the |
| 566 | /// outer attribute when the inner span closes. |
| 567 | pub fn write_inline(text |
| 568 | let bytes = |
| 569 | let n = |
| 570 | let mut i |
| 571 | let mut in_bold |
| 572 | let mut in_italic |
| 573 | let mut in_underscore_bold |
| 574 | let mut in_underscore_italic |
| 575 | let mut in_strike |
| 576 | let mut in_code |
| 577 | |
| 578 | while i < n |
| 579 | let c = |
| 580 | |
| 581 | // Inline code toggles on a single backtick; content is emitted raw |
| 582 | // (no further emphasis inside code). |
| 583 | if c == b |
| 584 | if in_code { |
| 585 | out.push_str(styles |
| 586 | in_code = false; |
| 587 | } else { |
| 588 | out.push_str(&styles |
| 589 | in_code = true; |
| 590 | } |
| 591 | i += 1; |
| 592 | continue; |
| 593 | } |
| 594 | if in_code { |
| 595 | // Non-ASCII inside code: push the full UTF-8 character. |
| 596 | if c >= 0x80 |
| 597 | let ch = |
| 598 | out.push(ch |
| 599 | i += ch. |
| 600 | } else { |
| 601 | out.push(c |
| 602 | i += 1; |
| 603 | } |
| 604 | continue; |
| 605 | } |
| 606 | |
| 607 | // Backslash escapes: emit the next byte literally if it is escapable |
| 608 | // punctuation. |
| 609 | if c == b |
| 610 | out.push(bytes |
| 611 | i += 2; |
| 612 | continue; |
| 613 | } |
| 614 | |
| 615 | // Strikethrough: `~~text~~`. Opening requires a non-space follower; |
| 616 | // closing requires a non-space predecessor (matching fx). |
| 617 | if c == b |
| 618 | if in_strike { |
| 619 | if i == 0 |
| 620 | out.push(c |
| 621 | i += 1; |
| 622 | continue; |
| 623 | } |
| 624 | out.push_str(styles |
| 625 | in_strike = false; |
| 626 | } else { |
| 627 | if i + 2 |
| 628 | out.push(c |
| 629 | i += 1; |
| 630 | continue; |
| 631 | } |
| 632 | out.push_str(styles |
| 633 | in_strike = true; |
| 634 | } |
| 635 | i += 2; |
| 636 | continue; |
| 637 | } |
| 638 | |
| 639 | // Bold: `**text**` (asterisk form). Underscore bold (`__text__`) is |
| 640 | // handled separately below because it has stricter flanking rules. |
| 641 | if c == b |
| 642 | if in_bold { |
| 643 | if i == 0 |
| 644 | out.push(c |
| 645 | i += 1; |
| 646 | continue; |
| 647 | } |
| 648 | out.push_str(styles |
| 649 | in_bold = false; |
| 650 | if in_underscore_bold { |
| 651 | out.push_str(styles |
| 652 | } |
| 653 | } else { |
| 654 | if i + 2 |
| 655 | out.push(c |
| 656 | i += 1; |
| 657 | continue; |
| 658 | } |
| 659 | out.push_str(styles |
| 660 | in_bold = true; |
| 661 | } |
| 662 | i += 2; |
| 663 | continue; |
| 664 | } |
| 665 | |
| 666 | // Underscore bold: `__text__` (intraword restrictions apply). |
| 667 | if c == b |
| 668 | if in_underscore_bold { |
| 669 | if !is_valid_underscore_close(text |
| 670 | out.push(c |
| 671 | i += 1; |
| 672 | continue; |
| 673 | } |
| 674 | out.push_str(styles |
| 675 | in_underscore_bold = false; |
| 676 | if in_bold { |
| 677 | out.push_str(styles |
| 678 | } |
| 679 | } else { |
| 680 | if !is_valid_underscore_open(text |
| 681 | out.push(c |
| 682 | i += 1; |
| 683 | continue; |
| 684 | } |
| 685 | out.push_str(styles |
| 686 | in_underscore_bold = true; |
| 687 | } |
| 688 | i += 2; |
| 689 | continue; |
| 690 | } |
| 691 | |
| 692 | // Italic (asterisk form): `*text*`. |
| 693 | if c == b |
| 694 | if in_italic { |
| 695 | if i == 0 |
| 696 | out.push(c |
| 697 | i += 1; |
| 698 | continue; |
| 699 | } |
| 700 | out.push_str(styles |
| 701 | in_italic = false; |
| 702 | if in_underscore_italic { |
| 703 | out.push_str(styles |
| 704 | } |
| 705 | } else { |
| 706 | if i + 1 |
| 707 | out.push(c |
| 708 | i += 1; |
| 709 | continue; |
| 710 | } |
| 711 | out.push_str(styles |
| 712 | in_italic = true; |
| 713 | } |
| 714 | i += 1; |
| 715 | continue; |
| 716 | } |
| 717 | |
| 718 | // Italic (underscore form): `_text_` (intraword restrictions apply). |
| 719 | if c == b |
| 720 | if in_underscore_italic { |
| 721 | if !is_valid_underscore_close(text |
| 722 | out.push(c |
| 723 | i += 1; |
| 724 | continue; |
| 725 | } |
| 726 | out.push_str(styles |
| 727 | in_underscore_italic = false; |
| 728 | if in_italic { |
| 729 | out.push_str(styles |
| 730 | } |
| 731 | } else { |
| 732 | if !is_valid_underscore_open(text |
| 733 | out.push(c |
| 734 | i += 1; |
| 735 | continue; |
| 736 | } |
| 737 | out.push_str(styles |
| 738 | in_underscore_italic = true; |
| 739 | } |
| 740 | i += 1; |
| 741 | continue; |
| 742 | } |
| 743 | |
| 744 | // Inline links: `[text](url)`. |
| 745 | if c == b |
| 746 | if let Some |
| 747 | emit_inline_link(&label, |
| 748 | i = end; |
| 749 | continue; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | // Images: `` — rendered as `[image: alt]` since the TUI |
| 754 | // cannot inline images into assistant text. |
| 755 | if c == b |
| 756 | if let Some |
| 757 | let _ = |
| 758 | i = end; |
| 759 | continue; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | // Angle autolinks: `<https://…>` / `<user@example.com>`. |
| 764 | if c == b |
| 765 | if let Some |
| 766 | emit_inline_link(text_val, url |
| 767 | i = end; |
| 768 | continue; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | // Bare URLs: `https://…` / `http://…` when preceded by a boundary. |
| 773 | if c == b |
| 774 | if let Some |
| 775 | emit_inline_link(url_text, url_text |
| 776 | i = end; |
| 777 | continue; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | // Non-ASCII byte: push the full UTF-8 character so multi-byte runes |
| 782 | // (emoji, accented letters) survive intact. fx works on bytes in Zig |
| 783 | // where this is implicit; Rust's String is UTF-8 so we must copy the |
| 784 | // whole encoding unit, not cast a single byte to char. |
| 785 | if c >= 0x80 |
| 786 | let ch = |
| 787 | let len = |
| 788 | out.push(ch |
| 789 | i += len; |
| 790 | continue; |
| 791 | } |
| 792 | |
| 793 | out.push(c |
| 794 | i += 1; |
| 795 | } |
| 796 | |
| 797 | // Close any unclosed spans so attributes never leak past the line. |
| 798 | if in_bold { |
| 799 | out.push_str(styles |
| 800 | } |
| 801 | if in_italic { |
| 802 | out.push_str(styles |
| 803 | } |
| 804 | if in_underscore_bold { |
| 805 | out.push_str(styles |
| 806 | } |
| 807 | if in_underscore_italic { |
| 808 | out.push_str(styles |
| 809 | } |
| 810 | if in_strike { |
| 811 | out.push_str(styles |
| 812 | } |
| 813 | if in_code { |
| 814 | out.push_str(styles |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | /// Inline rendering with bold suppressed (used for headings, where the heading |
| 819 | /// style already applies bold). fx strips `**` markers before delegating. |
| 820 | pub fn write_inline_no_bold(text |
| 821 | let bytes = |
| 822 | let mut stripped |
| 823 | let mut i |
| 824 | let mut in_code |
| 825 | while i < bytes |
| 826 | let c = |
| 827 | if c == b |
| 828 | in_code = !in_code |
| 829 | stripped.push('` |
| 830 | i += 1; |
| 831 | continue; |
| 832 | } |
| 833 | if !in_code |
| 834 | && c == b |
| 835 | && i + 1 |
| 836 | && is_escaped_punctuation_at(text, |
| 837 | { |
| 838 | stripped.push_str(&text |
| 839 | i += 2; |
| 840 | continue; |
| 841 | } |
| 842 | if !in_code && |
| 843 | if let Some |
| 844 | stripped.push_str(&text |
| 845 | i = closer |
| 846 | continue; |
| 847 | } |
| 848 | } |
| 849 | if !in_code && |
| 850 | i += 2; |
| 851 | continue; |
| 852 | } |
| 853 | // Non-ASCII: push the full UTF-8 character (see write_inline). |
| 854 | if c >= 0x80 |
| 855 | let ch = |
| 856 | stripped.push(ch |
| 857 | i += ch. |
| 858 | continue; |
| 859 | } |
| 860 | stripped.push(c |
| 861 | i += 1; |
| 862 | } |
| 863 | write_inline(&stripped, |
| 864 | } |
| 865 | |
| 866 | #[derive(Debug |
| 867 | struct InlineLink<' |
| 868 | text: &'a |
| 869 | url: &'a |
| 870 | end: usize |
| 871 | } |
| 872 | |
| 873 | fn parse_inline_link(text: |
| 874 | parse_inline_bracket_destination(text, start |
| 875 | } |
| 876 | |
| 877 | fn parse_inline_bracket_destination( |
| 878 | text: &str |
| 879 | start: usize |
| 880 | allow_empty_text: bool |
| 881 | ) -> Option<( |
| 882 | let bytes = |
| 883 | if start >= bytes |
| 884 | return None; |
| 885 | } |
| 886 | let mut j |
| 887 | while j < bytes |
| 888 | j += 1; |
| 889 | } |
| 890 | if j >= bytes |
| 891 | return None; |
| 892 | } |
| 893 | let text_end = |
| 894 | if !allow_empty_text && |
| 895 | return None; |
| 896 | } |
| 897 | if text_end + 1 |
| 898 | return None; |
| 899 | } |
| 900 | let mut k |
| 901 | while k < bytes |
| 902 | k += 1; |
| 903 | } |
| 904 | if k >= bytes |
| 905 | return None; |
| 906 | } |
| 907 | let url = |
| 908 | if !is_valid_link_url(url |
| 909 | return None; |
| 910 | } |
| 911 | Some((text[ |
| 912 | } |
| 913 | |
| 914 | fn parse_angle_autolink(text: |
| 915 | let bytes = |
| 916 | if start >= bytes |
| 917 | return None; |
| 918 | } |
| 919 | let mut end |
| 920 | while end < bytes |
| 921 | end += 1; |
| 922 | } |
| 923 | if end > start |
| 924 | let value = |
| 925 | if is_valid_angle_autolink_uri(value) |
| 926 | return Some((value |
| 927 | } |
| 928 | } |
| 929 | None |
| 930 | } |
| 931 | |
| 932 | fn is_valid_angle_autolink_uri(value: |
| 933 | let bytes = |
| 934 | let mut colon |
| 935 | while colon < bytes |
| 936 | colon += 1; |
| 937 | } |
| 938 | if colon < 2 |
| 939 | return false; |
| 940 | } |
| 941 | for &byte in |
| 942 | if !is_ascii_alphanumeric(byte |
| 943 | return false; |
| 944 | } |
| 945 | } |
| 946 | for &byte in |
| 947 | if byte <= b |
| 948 | return false; |
| 949 | } |
| 950 | } |
| 951 | true |
| 952 | } |
| 953 | |
| 954 | fn parse_bare_url(text: |
| 955 | let bytes = |
| 956 | if start == 0 |
| 957 | || (!is_ascii_word_byte(bytes |
| 958 | { |
| 959 | let scheme_len = |
| 960 | "https://".len() |
| 961 | } else if text |
| 962 | "http://".len() |
| 963 | } else { |
| 964 | return None; |
| 965 | }; |
| 966 | let mut end |
| 967 | while end < bytes |
| 968 | && !is_bare_url_terminator(bytes |
| 969 | { |
| 970 | end += 1; |
| 971 | } |
| 972 | while end > start |
| 973 | end -= 1; |
| 974 | } |
| 975 | let url = |
| 976 | if is_valid_link_url(url) |
| 977 | return Some((url |
| 978 | } |
| 979 | } |
| 980 | None |
| 981 | } |
| 982 | |
| 983 | fn is_bare_url_terminator(c: |
| 984 | is_ascii_whitespace(c) || |
| 985 | } |
| 986 | |
| 987 | fn is_valid_link_url(url: |
| 988 | if url.is_empty() |
| 989 | return false; |
| 990 | } |
| 991 | !url.bytes(). |
| 992 | } |
| 993 | |
| 994 | fn emit_inline_link(label: |
| 995 | // wynba lacks OSC 8 hyperlink support in the TUI's writer, so links are |
| 996 | // styled with the link color and the URL is shown inline (matching the |
| 997 | // existing md.rs behavior). fx uses OSC 8 when the terminal supports it. |
| 998 | let _ = |
| 999 | append_escaped_punctuation(out, label |
| 1000 | let _ = |
| 1001 | out.push_str(styles |
| 1002 | // Close the link SGR so the rest of the line doesn't inherit md_link |
| 1003 | // underline/color. RESET (not link_color) terminates the span. |
| 1004 | out.push_str(crate:: |
| 1005 | } |
| 1006 | |
| 1007 | // ─── Block rendering (port of block_render.zig) ───────────────────────────── |
| 1008 | |
| 1009 | /// Per-level heading styling, matching fx: h1 bold+underline, h2 bold, h3 |
| 1010 | /// underline, h4 bold+dim, h5 dim+underline, h6 dim. |
| 1011 | pub fn write_heading(level |
| 1012 | match level { |
| 1013 | 1 => { |
| 1014 | out.push_str(styles |
| 1015 | out.push_str(styles |
| 1016 | } |
| 1017 | 2 => out.push_str |
| 1018 | 3 => out.push_str |
| 1019 | 4 => { |
| 1020 | out.push_str(styles |
| 1021 | out.push_str(&styles |
| 1022 | } |
| 1023 | 5 => { |
| 1024 | out.push_str(&styles |
| 1025 | out.push_str(styles |
| 1026 | } |
| 1027 | 6 => out.push_str |
| 1028 | _ => unreachable! |
| 1029 | } |
| 1030 | write_inline_no_bold(content, out |
| 1031 | match level { |
| 1032 | 1 => { |
| 1033 | out.push_str(styles |
| 1034 | out.push_str(styles |
| 1035 | } |
| 1036 | 2 | 4 | 6 |
| 1037 | 3 => out.push_str |
| 1038 | 5 => { |
| 1039 | out.push_str(styles |
| 1040 | out.push_str(styles |
| 1041 | } |
| 1042 | _ => unreachable! |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | pub fn write_horizontal_rule(out |
| 1047 | out.push_str(&styles |
| 1048 | for _ in 0 |
| 1049 | out.push(styles |
| 1050 | } |
| 1051 | out.push_str(styles |
| 1052 | } |
| 1053 | |
| 1054 | pub fn write_blockquote_line( |
| 1055 | indent: usize |
| 1056 | depth: usize |
| 1057 | content: &str |
| 1058 | out: &mut |
| 1059 | styles: &AnsiStyles |
| 1060 | ) { |
| 1061 | out.push_str(&" |
| 1062 | for _ in 0 |
| 1063 | out.push_str(&styles |
| 1064 | out.push_str(styles |
| 1065 | out.push_str(styles |
| 1066 | } |
| 1067 | write_inline(content, out |
| 1068 | } |
| 1069 | |
| 1070 | pub fn write_code_block_line(line |
| 1071 | out.push_str(&styles |
| 1072 | out.push_str(styles |
| 1073 | out.push_str(styles |
| 1074 | out.push_str(line |
| 1075 | } |
| 1076 | |
| 1077 | // ─── One-shot renderer ────────────────────────────────────────────────────── |
| 1078 | |
| 1079 | /// Render a complete markdown document to styled terminal bytes. This is the |
| 1080 | /// public entry point used by the TUI for assistant text and tool output. Each |
| 1081 | /// block is separated by a newline; inline emphasis is rendered per-line. |
| 1082 | pub fn render_markdown(text |
| 1083 | let styles = |
| 1084 | let mut out |
| 1085 | let mut in_code |
| 1086 | let mut lines |
| 1087 | |
| 1088 | while let Some |
| 1089 | // Code fence handling: a line starting with ``` or ~~~ toggles a code |
| 1090 | // block. Inside, every line gets the vertical-rule prefix. |
| 1091 | if let Some |
| 1092 | if in_code { |
| 1093 | in_code = false; |
| 1094 | write_horizontal_rule(&mut out |
| 1095 | out.push('\ |
| 1096 | continue; |
| 1097 | } |
| 1098 | in_code = true; |
| 1099 | write_horizontal_rule(&mut out |
| 1100 | out.push('\ |
| 1101 | continue; |
| 1102 | } |
| 1103 | if in_code { |
| 1104 | write_code_block_line(line, & |
| 1105 | out.push('\ |
| 1106 | continue; |
| 1107 | } |
| 1108 | |
| 1109 | // Horizontal rule. |
| 1110 | if block_parse::is_horizontal_rule( |
| 1111 | write_horizontal_rule(&mut out |
| 1112 | out.push('\ |
| 1113 | continue; |
| 1114 | } |
| 1115 | |
| 1116 | // ATX headings (`# Title`). |
| 1117 | if let Some |
| 1118 | write_heading(level, content |
| 1119 | out.push('\ |
| 1120 | continue; |
| 1121 | } |
| 1122 | |
| 1123 | // Setext heading: a `===`/`---` underline under the previous text line. |
| 1124 | if let Some |
| 1125 | // Replace the last emitted line (the heading text) with a styled |
| 1126 | // heading at the detected level. fx buffers paragraph lines for |
| 1127 | // this; here we trim the trailing newline and re-render. Own the |
| 1128 | // previous line as a String so the immutable borrow of `out` ends |
| 1129 | // before we truncate/write back into it. |
| 1130 | if let Some |
| 1131 | let prev = |
| 1132 | let keep = |
| 1133 | out.truncate(keep |
| 1134 | write_heading(level, prev |
| 1135 | out.push('\ |
| 1136 | } |
| 1137 | continue; |
| 1138 | } |
| 1139 | |
| 1140 | // Blockquote: `> text` (supports nested depth `>> text`). |
| 1141 | if let Some |
| 1142 | let content = |
| 1143 | write_blockquote_line(bq.indent, |
| 1144 | out.push('\ |
| 1145 | continue; |
| 1146 | } |
| 1147 | |
| 1148 | // Unordered list: `- item` / `* item`. |
| 1149 | if let Some |
| 1150 | // Task list item: `- [ ]` / `- [x]`. |
| 1151 | if let Some |
| 1152 | block_parse::parse_task_list_item(content |
| 1153 | { |
| 1154 | out.push_str(indent |
| 1155 | if done { |
| 1156 | out.push_str(&styles |
| 1157 | out.push_str(styles |
| 1158 | out.push_str(styles |
| 1159 | } else { |
| 1160 | out.push_str(&styles |
| 1161 | out.push_str(styles |
| 1162 | out.push_str(styles |
| 1163 | } |
| 1164 | if has_sep { |
| 1165 | out.push(' |
| 1166 | } |
| 1167 | write_inline(task_content, & |
| 1168 | out.push('\ |
| 1169 | continue; |
| 1170 | } |
| 1171 | out.push_str(indent |
| 1172 | out.push_str(&styles |
| 1173 | out.push_str(styles |
| 1174 | out.push_str(styles |
| 1175 | write_inline(content, & |
| 1176 | out.push('\ |
| 1177 | continue; |
| 1178 | } |
| 1179 | |
| 1180 | // Ordered list: `1. item`. |
| 1181 | if let Some |
| 1182 | out.push_str(indent |
| 1183 | out.push_str(&styles |
| 1184 | let _ = |
| 1185 | out.push_str(styles |
| 1186 | write_inline(content, & |
| 1187 | out.push('\ |
| 1188 | continue; |
| 1189 | } |
| 1190 | |
| 1191 | // Blank line. |
| 1192 | if is_blank_line(line) |
| 1193 | out.push('\ |
| 1194 | continue; |
| 1195 | } |
| 1196 | |
| 1197 | // GFM table: a pipe line followed by a separator and more pipe rows. |
| 1198 | if block_parse::is_pipe_line( |
| 1199 | && block_parse::is_valid_table( |
| 1200 | { |
| 1201 | // For the streaming TUI we render tables simply: each pipe row is |
| 1202 | // emitted with inline emphasis. A full column-width-aligned table |
| 1203 | // renderer (fx's renderTable) is left as a follow-up since the TUI |
| 1204 | // reflows per resize anyway. |
| 1205 | write_inline(line, & |
| 1206 | out.push('\ |
| 1207 | // collect_table_block above only peeks; drain the consumed rows here |
| 1208 | // so separator + body rows are rendered instead of dropped. |
| 1209 | while let Some |
| 1210 | if block_parse::is_pipe_line( |
| 1211 | let row = |
| 1212 | write_inline(row, & |
| 1213 | out.push('\ |
| 1214 | } else { |
| 1215 | break; |
| 1216 | } |
| 1217 | } |
| 1218 | continue; |
| 1219 | } |
| 1220 | |
| 1221 | // Definition: `: body` (fx supports `: term` definition markers). |
| 1222 | if let Some |
| 1223 | out.push_str(&styles |
| 1224 | out.push_str(" |
| 1225 | out.push_str(styles |
| 1226 | write_inline(without_terminal_hard_break(body |
| 1227 | out.push('\ |
| 1228 | continue; |
| 1229 | } |
| 1230 | |
| 1231 | // Plain paragraph text. |
| 1232 | write_inline(line, & |
| 1233 | out.push('\ |
| 1234 | } |
| 1235 | |
| 1236 | out |
| 1237 | } |
| 1238 | |
| 1239 | /// Extract the content after `depth` blockquote `>` markers. |
| 1240 | fn extract_blockquote_content(line: |
| 1241 | let mut content |
| 1242 | for _ in 0 |
| 1243 | if let Some |
| 1244 | content = stripped. |
| 1245 | } |
| 1246 | } |
| 1247 | content |
| 1248 | } |
| 1249 | |
| 1250 | /// Collect consecutive pipe lines (for table detection) without consuming the |
| 1251 | /// iterator past the block — a simplified table boundary check. |
| 1252 | fn collect_table_block(first: |
| 1253 | let mut buf |
| 1254 | // Peek-only lookahead via a cloned iterator: never consumes `lines`. |
| 1255 | // The caller drains the rows after validation so an invalid table doesn't |
| 1256 | // lose lines and a valid table can render every row. |
| 1257 | let mut clone |
| 1258 | while let Some |
| 1259 | if block_parse::is_pipe_line( |
| 1260 | buf.push('\ |
| 1261 | buf.push_str(next |
| 1262 | if is_blank_line(next) |
| 1263 | break; |
| 1264 | } |
| 1265 | } else { |
| 1266 | break; |
| 1267 | } |
| 1268 | } |
| 1269 | buf |
| 1270 | } |
| 1271 | |
| 1272 | // ─── ANSI stripping (moved from md.rs) ───────────────────────────────────── |
| 1273 | |
| 1274 | /// Strip ANSI escape sequences from a string. Moved here from `md.rs` so the |
| 1275 | /// new renderer owns all ANSI handling. |
| 1276 | pub fn strip_ansi(s |
| 1277 | let mut out |
| 1278 | let mut chars |
| 1279 | while let Some |
| 1280 | if c == ' |
| 1281 | for d in chars |
| 1282 | if d.is_ascii_alphabetic() |
| 1283 | break; |
| 1284 | } |
| 1285 | } |
| 1286 | } else { |
| 1287 | out.push(c |
| 1288 | } |
| 1289 | } |
| 1290 | out |
| 1291 | } |
| 1292 | |
| 1293 | #[cfg(test)] |
| 1294 | mod tests { |
| 1295 | use super::*; |
| 1296 | |
| 1297 | fn render(text: |
| 1298 | render_markdown(text, 60 |
| 1299 | } |
| 1300 | |
| 1301 | #[test] |
| 1302 | fn plain_text_passes_through() { |
| 1303 | let out = |
| 1304 | assert!(out.contains |
| 1305 | } |
| 1306 | |
| 1307 | #[test] |
| 1308 | fn bold_emphasis() { |
| 1309 | let out = |
| 1310 | assert!(out.contains |
| 1311 | } |
| 1312 | |
| 1313 | #[test] |
| 1314 | fn italic_emphasis() { |
| 1315 | let out = |
| 1316 | assert!(out.contains |
| 1317 | } |
| 1318 | |
| 1319 | #[test] |
| 1320 | fn underscore_bold_and_italic() { |
| 1321 | let out = |
| 1322 | assert!(out.contains |
| 1323 | assert!(out.contains |
| 1324 | } |
| 1325 | |
| 1326 | #[test] |
| 1327 | fn inline_code() { |
| 1328 | let out = |
| 1329 | assert!(out.contains |
| 1330 | } |
| 1331 | |
| 1332 | #[test] |
| 1333 | fn strikethrough() { |
| 1334 | let out = |
| 1335 | assert!(out.contains |
| 1336 | } |
| 1337 | |
| 1338 | #[test] |
| 1339 | fn heading_levels() { |
| 1340 | let out = |
| 1341 | assert!(out.contains |
| 1342 | assert!(out.contains |
| 1343 | assert!(out.contains |
| 1344 | } |
| 1345 | |
| 1346 | #[test] |
| 1347 | fn unordered_list() { |
| 1348 | let out = |
| 1349 | // fx: dim-wrapped bullet marker, plain content after. |
| 1350 | assert!(out.contains |
| 1351 | assert!(out.contains |
| 1352 | } |
| 1353 | |
| 1354 | #[test] |
| 1355 | fn ordered_list() { |
| 1356 | let out = |
| 1357 | assert!(out.contains |
| 1358 | assert!(out.contains |
| 1359 | } |
| 1360 | |
| 1361 | #[test] |
| 1362 | fn task_list() { |
| 1363 | let out = |
| 1364 | assert!(out.contains |
| 1365 | assert!(out.contains |
| 1366 | } |
| 1367 | |
| 1368 | #[test] |
| 1369 | fn blockquote() { |
| 1370 | let out = |
| 1371 | // fx: dim vertical rule prefix, then the quote body. |
| 1372 | assert!(out.contains |
| 1373 | } |
| 1374 | |
| 1375 | #[test] |
| 1376 | fn code_fence() { |
| 1377 | let out = |
| 1378 | // Line-based rendering: fence lines fall through to the vertical-rule |
| 1379 | // fallback (the TUI's MarkdownStream builds the full fx panel). |
| 1380 | assert!(out.contains |
| 1381 | assert!(out.contains |
| 1382 | } |
| 1383 | |
| 1384 | #[test] |
| 1385 | fn horizontal_rule() { |
| 1386 | let out = |
| 1387 | assert!(out.contains |
| 1388 | } |
| 1389 | |
| 1390 | #[test] |
| 1391 | fn link() { |
| 1392 | let out = |
| 1393 | assert!(out.contains |
| 1394 | assert!(out.contains |
| 1395 | } |
| 1396 | |
| 1397 | #[test] |
| 1398 | fn bare_url() { |
| 1399 | let out = |
| 1400 | assert!(out.contains |
| 1401 | } |
| 1402 | |
| 1403 | #[test] |
| 1404 | fn angle_autolink() { |
| 1405 | let out = |
| 1406 | assert!(out.contains |
| 1407 | } |
| 1408 | |
| 1409 | #[test] |
| 1410 | fn strips_ansi() { |
| 1411 | assert_eq!(strip_ansi( |
| 1412 | } |
| 1413 | } |