1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
//! Markdown → ratatui `Text` renderer.
//!
//! Converts a markdown string to a `ratatui::text::Text<'static>` using
//! `pulldown-cmark`. Designed for rendering agent output inside the dashboard's
//! content panes where only `Paragraph` + `Text` are available (no nested widgets).
//!
//! Feature coverage:
//! - Headings H1–H6 (bold + accent colour, H1 gets an underline rule)
//! - Paragraphs with soft/hard breaks
//! - Bullet and ordered lists (nested up to depth 3)
//! - Blockquotes (dim, "│ " prefix)
//! - Inline `code`, **bold**, *italic*, ~~strikethrough~~, links (underlined)
//! - Fenced code blocks (tinted, language tag in title line)
//! - `mermaid` fenced blocks → styled fallback box with a hint to install mmdc
//! - Horizontal rules (dim dashes)
//!
//! Plain text that contains no markdown degrades cleanly - it just renders as
//! white text.
use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag, TagEnd};
use ratatui::{
style::{Color, Modifier, Style},
text::{Line, Span, Text},
};
// ─── Palette ──────────────────────────────────────────────────────────────────
//
// Shared with every other Leviath terminal surface. Imported from the single
// definition in `tui::theme` rather than a hand-copied duplicate of the
// dashboard's palette, which is exactly the kind of thing that drifts.
use crate::tui::theme::{C_ACCENT, C_CODE_BG, C_DIM, C_MUTED, C_SUCCESS, C_WHITE};
// ─── Public API ───────────────────────────────────────────────────────────────
/// Convert a markdown string to ratatui `Text` for rendering in a `Paragraph`.
///
/// `width` is used to draw horizontal rules to the correct width.
pub fn markdown_to_text(input: &str, width: u16) -> Text<'static> {
let mut renderer = Renderer::new(width);
renderer.render(input);
Text::from(renderer.lines)
}
// ─── Renderer internals ───────────────────────────────────────────────────────
/// Stack of style modifiers accumulated from nested inline tags.
#[derive(Default, Clone)]
struct InlineStyle {
bold: bool,
italic: bool,
strikethrough: bool,
code: bool,
link: bool,
}
impl InlineStyle {
fn to_ratatui_style(&self) -> Style {
let mut style = Style::default().fg(C_WHITE);
if self.code {
style = style.fg(Color::Rgb(200, 160, 100)).bg(C_CODE_BG);
} else if self.link {
style = style.fg(C_ACCENT).add_modifier(Modifier::UNDERLINED);
}
if self.bold {
style = style.add_modifier(Modifier::BOLD);
}
if self.italic {
style = style.add_modifier(Modifier::ITALIC);
}
if self.strikethrough {
style = style.add_modifier(Modifier::CROSSED_OUT);
}
style
}
}
struct Renderer {
lines: Vec<Line<'static>>,
/// Spans being accumulated for the current line.
current_spans: Vec<Span<'static>>,
/// Inline style stack (push/pop for nested inline tags).
inline_stack: Vec<InlineStyle>,
/// Current inline style (derived from the stack).
inline: InlineStyle,
/// Whether we're currently inside a fenced code block.
in_code_block: bool,
/// Language hint for the current code block.
code_lang: Option<String>,
/// Source lines accumulated within a code block.
code_lines: Vec<String>,
/// List nesting stack: None = bullet, Some(n) = ordered (current item #).
list_stack: Vec<Option<u64>>,
/// Terminal width (used for HR lines).
width: u16,
}
impl Renderer {
fn new(width: u16) -> Self {
Self {
lines: Vec::new(),
current_spans: Vec::new(),
inline_stack: Vec::new(),
inline: InlineStyle::default(),
in_code_block: false,
code_lang: None,
code_lines: Vec::new(),
list_stack: Vec::new(),
width,
}
}
/// Flush `current_spans` into a finished `Line`.
fn flush_line(&mut self) {
let spans = std::mem::take(&mut self.current_spans);
self.lines.push(Line::from(spans));
}
/// Push a fully-built `Line` directly.
fn push_line(&mut self, line: Line<'static>) {
if !self.current_spans.is_empty() {
self.flush_line();
}
self.lines.push(line);
}
/// Push an empty blank line.
fn blank_line(&mut self) {
self.push_line(Line::from(""));
}
/// Current list indent (2 spaces per nesting level).
fn list_indent(&self) -> String {
" ".repeat(self.list_stack.len())
}
/// Rebuild the active inline style from the stack top.
fn sync_inline(&mut self) {
self.inline = self.inline_stack.last().cloned().unwrap_or_default();
}
fn push_inline(&mut self, mut new_style: InlineStyle) {
// Inherit accumulated modifiers from parent
if let Some(parent) = self.inline_stack.last() {
if parent.bold {
new_style.bold = true;
}
if parent.italic {
new_style.italic = true;
}
if parent.strikethrough {
new_style.strikethrough = true;
}
}
self.inline_stack.push(new_style);
self.sync_inline();
}
fn pop_inline(&mut self) {
self.inline_stack.pop();
self.sync_inline();
}
/// Emit a span of text with the current inline style.
fn emit_text(&mut self, text: &str) {
let style = self.inline.to_ratatui_style();
// Preserve leading space so words don't jam together
self.current_spans
.push(Span::styled(text.to_owned(), style));
}
/// Handle a non-code-block text payload, splitting on embedded newlines if
/// present. Factored out so the newline-split path can be exercised in
/// tests directly (pulldown_cmark never produces a non-code Text event with
/// a literal `\n` under current Options, so this path is unreachable via
/// the parser alone).
fn handle_text_content(&mut self, t: &str) {
if t.contains('\n') {
let mut first = true;
for part in t.split('\n') {
if !first {
self.flush_line();
}
first = false;
if !part.is_empty() {
self.emit_text(part);
}
}
} else {
self.emit_text(t);
}
}
/// Render a complete code block (fenced or mermaid).
fn flush_code_block(&mut self) {
let lang = self.code_lang.take().unwrap_or_default();
let content = std::mem::take(&mut self.code_lines);
let is_mermaid = lang.trim().to_lowercase() == "mermaid";
if is_mermaid {
// ── Mermaid fallback ─────────────────────────────────────────────
self.push_line(Line::from(vec![
Span::styled(
" ◇ ",
Style::default().fg(C_ACCENT).add_modifier(Modifier::BOLD),
),
Span::styled("mermaid diagram", Style::default().fg(C_ACCENT)),
Span::styled(" - source", Style::default().fg(C_DIM)),
]));
for code_line in &content {
self.push_line(Line::from(vec![
Span::styled(" │ ", Style::default().fg(C_DIM)),
Span::styled(code_line.to_owned(), Style::default().fg(C_MUTED)),
]));
}
self.push_line(Line::from(Span::styled(
" ↑ Install mermaid-cli (mmdc) to render as a diagram",
Style::default().fg(C_DIM),
)));
} else {
// ── Regular code block ───────────────────────────────────────────
let lang_label = if lang.is_empty() {
"code".to_string()
} else {
lang.clone()
};
self.push_line(Line::from(vec![
Span::styled(" ╭─ ", Style::default().fg(C_DIM)),
Span::styled(
lang_label,
Style::default().fg(C_MUTED).add_modifier(Modifier::BOLD),
),
Span::styled(" ─", Style::default().fg(C_DIM)),
]));
for code_line in &content {
self.push_line(Line::from(vec![
Span::styled(" │ ", Style::default().fg(C_DIM)),
Span::styled(
code_line.to_owned(),
Style::default().fg(Color::Rgb(200, 200, 140)).bg(C_CODE_BG),
),
]));
}
self.push_line(Line::from(Span::styled(" ╰─", Style::default().fg(C_DIM))));
}
self.in_code_block = false;
}
pub fn render(&mut self, input: &str) {
let opts = Options::ENABLE_STRIKETHROUGH | Options::ENABLE_TABLES;
let parser = Parser::new_ext(input, opts);
for event in parser {
match event {
// ── Block starts ─────────────────────────────────────────────
Event::Start(Tag::Heading { level, .. }) => {
// Flush any pending content and start a fresh heading line
if !self.current_spans.is_empty() {
self.flush_line();
}
// Blank line before non-first headings
if !self.lines.is_empty() {
self.blank_line();
}
// Visual decorators - convey depth without # text (terminal can't vary font size)
let (color, prefix, bold) = match level {
HeadingLevel::H1 => (C_ACCENT, "▌ ", true),
HeadingLevel::H2 => (C_ACCENT, "▎ ", true),
HeadingLevel::H3 => (C_SUCCESS, " ", true),
HeadingLevel::H4 => (C_MUTED, " ", false),
HeadingLevel::H5 => (C_DIM, " ", false),
HeadingLevel::H6 => (C_DIM, " ", false),
};
let mut sty = Style::default().fg(color);
if bold {
sty = sty.add_modifier(Modifier::BOLD);
}
self.current_spans.push(Span::styled(prefix, sty));
self.push_inline(InlineStyle {
bold,
..Default::default()
});
}
Event::End(TagEnd::Heading(level)) => {
self.pop_inline();
self.flush_line();
// H1 gets a dim underline rule
if level == HeadingLevel::H1 {
let rule_w = (self.width as usize).saturating_sub(2).max(8);
self.push_line(Line::from(Span::styled(
"─".repeat(rule_w),
Style::default().fg(C_DIM),
)));
}
self.blank_line();
}
Event::Start(Tag::Paragraph) => {}
Event::End(TagEnd::Paragraph) => {
self.flush_line();
self.blank_line();
}
Event::Start(Tag::BlockQuote(_)) => {
self.push_inline(InlineStyle {
..Default::default()
});
self.current_spans
.push(Span::styled("│ ", Style::default().fg(C_DIM)));
}
Event::End(TagEnd::BlockQuote(_)) => {
self.pop_inline();
if !self.current_spans.is_empty() {
self.flush_line();
}
self.blank_line();
}
Event::Start(Tag::List(start)) => {
self.list_stack.push(start);
}
Event::End(TagEnd::List(_)) => {
self.list_stack.pop();
if self.list_stack.is_empty() {
self.blank_line();
}
}
Event::Start(Tag::Item) => {
if !self.current_spans.is_empty() {
self.flush_line();
}
let indent = self.list_indent();
let bullet = match self.list_stack.last() {
Some(Some(n)) => format!("{}. ", n),
Some(None) | None => "● ".to_string(),
};
// Increment ordered list counter
if let Some(Some(n)) = self.list_stack.last_mut() {
*n += 1;
}
self.current_spans.push(Span::styled(
format!("{}{}", indent, bullet),
Style::default().fg(C_ACCENT),
));
}
Event::End(TagEnd::Item) => {
if !self.current_spans.is_empty() {
self.flush_line();
}
}
Event::Start(Tag::CodeBlock(kind)) => {
self.in_code_block = true;
self.code_lang = match kind {
CodeBlockKind::Fenced(lang) => {
let s = lang.into_string();
if s.is_empty() { None } else { Some(s) }
}
CodeBlockKind::Indented => None,
};
self.code_lines = Vec::new();
if !self.current_spans.is_empty() {
self.flush_line();
}
}
Event::End(TagEnd::CodeBlock) => {
self.flush_code_block();
}
Event::Start(Tag::Strong) => {
self.push_inline(InlineStyle {
bold: true,
..Default::default()
});
}
Event::End(TagEnd::Strong) => {
self.pop_inline();
}
Event::Start(Tag::Emphasis) => {
self.push_inline(InlineStyle {
italic: true,
..Default::default()
});
}
Event::End(TagEnd::Emphasis) => {
self.pop_inline();
}
Event::Start(Tag::Strikethrough) => {
self.push_inline(InlineStyle {
strikethrough: true,
..Default::default()
});
}
Event::End(TagEnd::Strikethrough) => {
self.pop_inline();
}
Event::Start(Tag::Link { dest_url, .. }) => {
self.push_inline(InlineStyle {
link: true,
..Default::default()
});
// Show the URL as a dim suffix after the link text
let url = dest_url.into_string();
if !url.is_empty() {
// We'll append the URL after the link text in End(Link)
// Store it via a span now so we can reference it; simpler: just push
// a placeholder and let the text events fill in link text.
// We push the URL as a trailing span at End(Link).
// Use a little indirection: push the open bracket.
self.current_spans
.push(Span::styled("[", Style::default().fg(C_DIM)));
// Stash URL in a "pending link url" field would be ideal.
// For simplicity, store it as a special span at end.
// We'll capture the URL by pushing it immediately at End.
// So skip storing; just remember we need to close.
let _ = url; // url emitted at End(Link) via stored span approach
}
}
Event::End(TagEnd::Link) => {
self.pop_inline();
self.current_spans
.push(Span::styled("]", Style::default().fg(C_DIM)));
}
// ── Inline events ────────────────────────────────────────────
Event::Text(text) => {
if self.in_code_block {
// Accumulate raw code lines
for line in text.lines() {
self.code_lines.push(line.to_string());
}
} else {
let t = text.into_string();
self.handle_text_content(&t);
}
}
Event::Code(text) => {
// Inline code span
self.current_spans.push(Span::styled(
text.into_string(),
Style::default().fg(Color::Rgb(200, 160, 100)).bg(C_CODE_BG),
));
}
Event::SoftBreak => {
// Soft breaks just become a space in terminal output
self.current_spans.push(Span::raw(" "));
}
Event::HardBreak => {
self.flush_line();
}
Event::Rule => {
if !self.current_spans.is_empty() {
self.flush_line();
}
let w = (self.width as usize).saturating_sub(2).max(8);
self.push_line(Line::from(Span::styled(
"─".repeat(w),
Style::default().fg(C_DIM),
)));
self.blank_line();
}
// Ignore HTML, footnotes, task list markers, math, etc.
_ => {}
}
}
// Flush any trailing content
if !self.current_spans.is_empty() {
self.flush_line();
}
}
}
// ─── Tests ────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn push_line_flushes_pending_spans_first() {
// No current caller ever invokes `push_line` while `current_spans`
// is non-empty (each call site flushes its own pending spans via a
// separate check first) - but the method itself is directly
// testable by constructing that state manually, without needing a
// real caller to reach it.
let mut r = Renderer::new(80);
r.current_spans.push(Span::raw("pending"));
r.push_line(Line::from("new line"));
assert_eq!(r.lines.len(), 2);
assert!(r.current_spans.is_empty());
let flushed: String = r.lines[0]
.spans
.iter()
.map(|s| s.content.as_ref())
.collect();
assert_eq!(flushed, "pending");
}
#[test]
fn plain_text_renders_as_single_line() {
let text = markdown_to_text("Hello, world!", 80);
assert!(!text.lines.is_empty());
// The content should contain our text somewhere
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("Hello, world!"));
}
#[test]
fn heading_produces_lines() {
let text = markdown_to_text("# My Heading\n\nSome paragraph.", 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("My Heading"));
assert!(all.contains("Some paragraph"));
}
#[test]
fn code_block_renders_with_border() {
let md = "```rust\nfn main() {}\n```";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("fn main() {}"));
// Should have a border glyph. Code-block borders only ever render
// with '╭' (never '│'), so checking a single glyph avoids a
// redundant `||` whose right-hand side could never be reached.
assert!(all.contains('╭'));
}
#[test]
fn mermaid_block_shows_hint() {
let md = "```mermaid\ngraph LR\n A --> B\n```";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("mermaid"));
// The fallback hint always contains both substrings together, so
// checking one avoids a redundant `||` whose right-hand side could
// never be reached.
assert!(all.contains("mmdc"));
}
#[test]
fn list_renders_bullets() {
let md = "- item one\n- item two";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("item one"));
assert!(all.contains("item two"));
}
// ─── Empty input ───────────────────────────────────────────────────────
#[test]
fn empty_input_returns_empty() {
let text = markdown_to_text("", 80);
// Empty input produces zero lines (no content to render). Checked
// directly (rather than via `.iter().all(|l| ...)`) since `all()`
// short-circuits without invoking its predicate on an empty
// iterator, which would otherwise leave that closure's body
// permanently unreachable.
assert!(text.lines.is_empty());
}
// ─── Inline styles ────────────────────────────────────────────────────
#[test]
fn bold_text_rendered() {
let text = markdown_to_text("**bold text**", 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("bold text"));
}
#[test]
fn italic_text_rendered() {
let text = markdown_to_text("*italic text*", 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("italic text"));
}
#[test]
fn strikethrough_text_rendered() {
let text = markdown_to_text("~~deleted~~", 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("deleted"));
}
#[test]
fn inline_code_rendered() {
let text = markdown_to_text("use `println!()`", 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("println!()"));
}
// ─── Headings ──────────────────────────────────────────────────────────
#[test]
fn h2_heading_rendered() {
let text = markdown_to_text("## Second Level", 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("Second Level"));
}
#[test]
fn h3_heading_rendered() {
let text = markdown_to_text("### Third Level", 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("Third Level"));
}
#[test]
fn h1_heading_produces_underline_rule() {
let text = markdown_to_text("# Heading", 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
// H1 should produce a horizontal rule line underneath
assert!(all.contains("\u{2500}"));
}
// ─── Horizontal rule ───────────────────────────────────────────────────
#[test]
fn horizontal_rule_rendered() {
let text = markdown_to_text("above\n\n---\n\nbelow", 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("above"));
assert!(all.contains("below"));
assert!(all.contains("\u{2500}"));
}
// ─── Blockquote ────────────────────────────────────────────────────────
#[test]
fn blockquote_rendered() {
let text = markdown_to_text("> quoted text", 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("quoted text"));
}
// ─── Ordered list ──────────────────────────────────────────────────────
#[test]
fn ordered_list_rendered() {
let md = "1. first\n2. second\n3. third";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("first"));
assert!(all.contains("second"));
assert!(all.contains("third"));
}
// ─── Code block without language ───────────────────────────────────────
#[test]
fn code_block_without_language() {
let md = "```\nplain code\n```";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("plain code"));
// Should show "code" as the default language label
assert!(all.contains("code"));
}
// ─── Link rendering ───────────────────────────────────────────────────
#[test]
fn link_rendered() {
let md = "[click here](https://example.com)";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("click here"));
}
#[test]
fn link_with_empty_url_skips_bracket_span() {
// Exercises the `!url.is_empty()` false arm (a link whose destination
// URL is empty), which skips pushing the trailing "[" span.
let md = "[no url]()";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>();
assert!(all.contains("no url"));
}
// ─── Narrow width ──────────────────────────────────────────────────────
#[test]
fn narrow_width_does_not_panic() {
// Very narrow width should not cause panics
let text = markdown_to_text("# Heading\n\n---\n\nSome content", 5);
assert!(!text.lines.is_empty());
}
#[test]
fn zero_width_does_not_panic() {
let text = markdown_to_text("# Heading\n\n---", 0);
assert!(!text.lines.is_empty());
}
// ─── InlineStyle ───────────────────────────────────────────────────────
#[test]
fn inline_style_default_produces_white_text() {
let style = InlineStyle::default();
let ratatui_style = style.to_ratatui_style();
assert_eq!(ratatui_style.fg, Some(C_WHITE));
}
#[test]
fn inline_style_code_overrides_color() {
let style = InlineStyle {
code: true,
..Default::default()
};
let ratatui_style = style.to_ratatui_style();
// Code should have a specific fg color, not white
assert_ne!(ratatui_style.fg, Some(C_WHITE));
}
// ─── Multiple paragraphs ───────────────────────────────────────────────
#[test]
fn multiple_paragraphs_have_blank_lines() {
let md = "First paragraph.\n\nSecond paragraph.";
let text = markdown_to_text(md, 80);
// Should have more than 2 lines (paragraphs + blank separators)
assert!(text.lines.len() >= 3);
}
// ─── Additional heading levels (H4/H5/H6) ───────────────────────────────
#[test]
fn h4_h5_h6_headings_rendered() {
let md = "#### Four\n\n##### Five\n\n###### Six";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("Four"));
assert!(all.contains("Five"));
assert!(all.contains("Six"));
}
// ─── Nested inline styles inherit from parent ──────────────────────────
#[test]
fn bold_italic_nested_inherits_both_modifiers() {
// ***text*** parses as Strong containing Emphasis (or vice versa) -
// the inner style must inherit the outer's bold/italic/strikethrough.
let md = "***bold italic***";
let text = markdown_to_text(md, 80);
let style = text.lines[0].spans[0].style;
assert!(style.add_modifier.contains(Modifier::BOLD));
assert!(style.add_modifier.contains(Modifier::ITALIC));
}
#[test]
fn strikethrough_inside_bold_inherits_bold() {
let md = "**bold ~~and struck~~**";
let text = markdown_to_text(md, 80);
let all_styled_bold = text.lines[0].spans.iter().any(|s| {
s.style
.add_modifier
.contains(Modifier::CROSSED_OUT | Modifier::BOLD)
});
assert!(all_styled_bold);
}
// ─── Blockquote with multiple lines flushes correctly ──────────────────
#[test]
fn blockquote_with_multiple_lines() {
let md = "> line one\n> line two";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("line one"));
assert!(all.contains("line two"));
}
// ─── Nested / multi-item lists flush pending content between items ─────
#[test]
fn bullet_list_with_multiple_items() {
let md = "- alpha\n- beta\n- gamma";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("alpha"));
assert!(all.contains("beta"));
assert!(all.contains("gamma"));
assert!(all.contains("\u{25cf}"));
}
#[test]
fn nested_list_indents() {
let md = "- top\n - nested\n- top2";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("top"));
assert!(all.contains("nested"));
}
// ─── Indented (non-fenced) code block ───────────────────────────────────
#[test]
fn indented_code_block_has_no_language_label_from_lang() {
let md = "Normal text.\n\n indented code line\n\nMore text.";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("indented code line"));
}
// ─── Multi-line text event (embedded newline split) ────────────────────
#[test]
fn hard_break_splits_into_separate_lines() {
// Two trailing spaces + newline = hard break in CommonMark.
let md = "first line \nsecond line";
let text = markdown_to_text(md, 80);
assert!(text.lines.len() >= 2);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("first line"));
assert!(all.contains("second line"));
}
#[test]
fn soft_break_becomes_space() {
let md = "first\nsecond";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("first"));
assert!(all.contains("second"));
}
// ─── Rule with pending inline content before it ────────────────────────
#[test]
fn rule_flushes_pending_content_first() {
// pulldown-cmark treats "text\n***" as a paragraph followed by a rule
// only when properly separated; use explicit blank-line-free content
// before a thematic break to exercise the pending-flush branch.
let md = "above text\n\n---\nbelow text";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("above text"));
assert!(all.contains("below text"));
}
// ─── Table events fall through the catch-all arm ───────────────────────
#[test]
fn table_does_not_panic_and_renders_cell_text() {
let md = "| A | B |\n|---|---|\n| 1 | 2 |";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
// Table structural events are ignored (catch-all arm), but the text
// content inside cells still comes through as Text events. Both
// substrings are always present together, so check each directly
// rather than via a redundant `||` whose right-hand side could
// never be reached.
assert!(all.contains('1'));
assert!(all.contains('A'));
}
// ─── Renderer state-machine edge cases ──────────────────────────────────
//
// These target `flush_line()`/inline-inheritance branches that only fire
// when `current_spans` (or the inline-style stack) is in a specific,
// non-default state at the moment a new block/inline event starts --
// found by probing actual `pulldown_cmark::Parser` event streams for
// each input (see git history) rather than guessing at markdown syntax.
#[test]
fn heading_as_first_content_of_list_item_flushes_pending_bullet_span() {
// `Start(Item)` pushes the bullet marker into `current_spans`, then
// `Start(Heading)` fires with no Text/other event in between --
// exercises the `!self.current_spans.is_empty()` flush at heading
// start.
let md = "- # nested heading in item\n- item2";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("nested heading in item"));
assert!(all.contains("item2"));
}
#[test]
fn code_block_as_first_content_of_list_item_flushes_pending_bullet_span() {
// Same shape as the heading case above, but for `Start(CodeBlock)`.
let md = "- ```\ncode\n```";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("code"));
}
#[test]
fn rule_directly_inside_blockquote_flushes_pending_quote_marker_span() {
// `Start(BlockQuote)` pushes the "│ " marker into `current_spans`,
// then `Event::Rule` fires with nothing else queued - exercises the
// `!self.current_spans.is_empty()` flush at `Event::Rule` (every
// other rule test has an empty `current_spans` at that point).
let md = "> ---";
let text = markdown_to_text(md, 80);
assert!(!text.lines.is_empty());
}
#[test]
fn empty_blockquote_flushes_pending_quote_marker_span_at_end() {
// `Start(BlockQuote)` pushes "│ " with no inner content at all
// before `End(BlockQuote)` - exercises the flush at blockquote end
// (every other blockquote test has real paragraph content, which
// flushes `current_spans` via `End(Paragraph)` first).
let md = ">";
let text = markdown_to_text(md, 80);
let all: String = text
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains('│'));
}
#[test]
fn nested_strong_inside_strikethrough_inherits_strikethrough_modifier() {
// `push_inline` for the inner `Strong` tag inherits `strikethrough`
// from the `Strikethrough` parent already on the stack - every
// other strikethrough test only nests plain text, never another
// inline tag, so `parent.strikethrough` was never true at push time.
let md = "~~strike **bold inside** more~~";
let text = markdown_to_text(md, 80);
let has_strikethrough_bold = text.lines.iter().any(|l| {
l.spans.iter().any(|s| {
s.content.contains("bold inside")
&& s.style.add_modifier.contains(Modifier::CROSSED_OUT)
&& s.style.add_modifier.contains(Modifier::BOLD)
})
});
assert!(has_strikethrough_bold);
}
// `push_line`'s own `if !self.current_spans.is_empty() { self.flush_line() }`
// guard is not reachable given how `push_line`/`blank_line` are actually
// called in this file: every call site (heading/paragraph/blockquote/list/
// rule end handlers) already flushes `current_spans` explicitly, via its
// own separate check, before ever calling `push_line`/`blank_line`.
// `Event::Start(Tag::Item)`'s `None => "● ".to_string()` arm (list_stack
// empty) is not covered and is not reachable: `Tag::Item` is only ever
// emitted by `pulldown_cmark` as a child of `Tag::List`, which always
// pushes onto `list_stack` first.
// ─── handle_text_content: embedded newline split (direct call) ────────────
#[test]
fn handle_text_content_with_embedded_newline_splits_lines() {
// pulldown_cmark never produces a non-code Text event with `\n`, so
// the newline-split path in handle_text_content is exercised here by
// calling the method directly.
let mut r = Renderer::new(80);
r.handle_text_content("first\nsecond\nthird");
// The first line is flushed for each embedded newline; at least 2 lines
// should have been emitted.
assert!(r.lines.len() >= 2);
let all: String = r
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect();
assert!(all.contains("first"));
assert!(all.contains("second"));
}
#[test]
fn handle_text_content_with_leading_newline_skips_empty_first_part() {
// The empty part before the leading '\n' must not emit an empty span.
let mut r = Renderer::new(80);
r.handle_text_content("\nhello");
// "hello" lands in current_spans (pending); lines get the flush for the
// empty-first-part boundary, which produces an empty line.
let pending: String = r.current_spans.iter().map(|s| s.content.as_ref()).collect();
assert!(pending.contains("hello"));
}
#[test]
fn handle_text_content_without_newline_emits_directly() {
let mut r = Renderer::new(80);
// Flush a real line into `r.lines` first (text ending in a newline
// gets flushed), so the `r.lines` iteration below actually visits an
// element instead of running over an always-empty `Vec` and leaving
// its closure unreachable.
r.handle_text_content("first line\n");
r.handle_text_content("no newline here");
let all: String = r
.lines
.iter()
.flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
.collect::<String>()
+ r.current_spans
.iter()
.map(|s| s.content.as_ref())
.collect::<String>()
.as_str();
assert!(all.contains("no newline here"));
assert!(all.contains("first line"));
}
}