BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / render / blocks.rs

blocks.rs

blocks.rs 27.0 KiB Download Raw
1use std::ops::Range;
2
3use teams_core::Span;
4
5pub const IMAGE_PLACEHOLDER: &str = "[image]";
6pub const CODE_PADDING: &str = "\u{2004}";
7const URL_PREFIXES: [&str; 3] = ["https://", "http://", "www."];
8const TRAILING_PUNCTUATION: &str = ".,;:!?'\"";
9
10#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
11pub struct StyleFlags {
12 pub bold: bool,
13 pub italic: bool,
14 pub code: bool,
15 pub mention: bool,
16 pub link: bool,
17 pub strike: bool,
18 pub underline: bool,
19 pub color: Option<u32>,
20 pub background: Option<u32>,
21}
22
23impl StyleFlags {
24 pub fn is_plain(&self) -> bool {
25 *self == StyleFlags::default()
26 }
27}
28
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct Segment {
31 pub range: Range<usize>,
32 pub style: StyleFlags,
33 pub link: Option<String>,
34}
35
36#[derive(Debug, Clone, Default, PartialEq, Eq)]
37pub struct Inline {
38 pub text: String,
39 pub segments: Vec<Segment>,
40}
41
42impl Inline {
43 pub fn plain(text: &str) -> Self {
44 let mut inline = Inline::default();
45 inline.push(text, StyleFlags::default(), None);
46 inline
47 }
48
49 fn push(&mut self, text: &str, style: StyleFlags, link: Option<&str>) {
50 if text.is_empty() {
51 return;
52 }
53 let start = self.text.len();
54 self.text.push_str(text);
55 let end = self.text.len();
56 if let Some(last) = self.segments.last_mut()
57 && last.style == style
58 && last.link.as_deref() == link
59 && last.range.end == start
60 {
61 last.range.end = end;
62 return;
63 }
64 self.segments.push(Segment {
65 range: start..end,
66 style,
67 link: link.map(str::to_owned),
68 });
69 }
70
71 pub fn plain_text(&self) -> String {
72 self.text.replace(CODE_PADDING, "")
73 }
74
75 pub fn with_trailing_room(&self, width_in_spaces: usize) -> Inline {
76 let mut padded = self.clone();
77 padded.push(
78 &"\u{2007}".repeat(width_in_spaces),
79 StyleFlags::default(),
80 None,
81 );
82 padded
83 }
84
85 pub fn is_blank(&self) -> bool {
86 self.text.trim().is_empty()
87 }
88
89 fn trim_end(&mut self) {
90 let trimmed = self.text.trim_end().len();
91 self.text.truncate(trimmed);
92 self.segments.retain_mut(|segment| {
93 segment.range.end = segment.range.end.min(trimmed);
94 segment.range.start < segment.range.end
95 });
96 }
97
98 fn without(&self, needle: &str) -> Inline {
99 let mut stripped = Inline::default();
100 for segment in &self.segments {
101 let text = self.text[segment.range.clone()].replace(needle, "");
102 stripped.push(&text, segment.style, segment.link.as_deref());
103 }
104 stripped.trim_end();
105 let leading = stripped.text.len() - stripped.text.trim_start().len();
106 if leading > 0 {
107 let mut trimmed = Inline::default();
108 for segment in &stripped.segments {
109 let text = stripped.text[segment.range.clone()].to_owned();
110 let skip = leading.saturating_sub(segment.range.start).min(text.len());
111 trimmed.push(&text[skip..], segment.style, segment.link.as_deref());
112 }
113 return trimmed;
114 }
115 stripped
116 }
117
118 pub fn links(&self) -> Vec<(Range<usize>, String)> {
119 self.segments
120 .iter()
121 .filter_map(|segment| Some((segment.range.clone(), segment.link.clone()?)))
122 .collect()
123 }
124}
125
126#[derive(Debug, Clone, PartialEq, Eq)]
127pub enum Block {
128 Paragraph(Inline),
129 Heading {
130 level: u8,
131 inline: Inline,
132 },
133 Code {
134 language: Option<String>,
135 code: String,
136 },
137 Reply(Vec<Block>),
138 Quote(Vec<Block>),
139 List {
140 ordered: bool,
141 start: u32,
142 items: Vec<Vec<Block>>,
143 },
144 Table {
145 header: bool,
146 rows: Vec<Vec<Inline>>,
147 },
148 Rule,
149}
150
151impl Block {
152 pub fn markdown(&self) -> Option<String> {
153 let join = |blocks: &[Block]| {
154 blocks
155 .iter()
156 .filter_map(Block::markdown)
157 .collect::<Vec<_>>()
158 .join("\n")
159 };
160 match self {
161 Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.plain_text()),
162 Block::Code { code, .. } => Some(format!("```\n{code}\n```")),
163 Block::List {
164 ordered,
165 start,
166 items,
167 } => Some(
168 items
169 .iter()
170 .enumerate()
171 .map(|(offset, item)| match ordered {
172 true => format!("{}. {}", *start as usize + offset, join(item)),
173 false => format!("- {}", join(item)),
174 })
175 .collect::<Vec<_>>()
176 .join("\n"),
177 ),
178 Block::Table { rows, .. } => Some(
179 rows.iter()
180 .map(|row| {
181 row.iter()
182 .map(Inline::plain_text)
183 .collect::<Vec<_>>()
184 .join(" | ")
185 })
186 .collect::<Vec<_>>()
187 .join("\n"),
188 ),
189 Block::Quote(children) => Some(join(children)),
190 Block::Reply(_) | Block::Rule => None,
191 }
192 }
193
194 pub fn first_line(&self) -> Option<String> {
195 match self {
196 Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.plain_text()),
197 Block::Code { code, .. } => Some(code.clone()),
198 Block::List { items, .. } => items.first()?.iter().find_map(Block::first_line),
199 Block::Table { rows, .. } => Some(rows.first()?.first()?.plain_text()),
200 Block::Quote(children) => children.iter().find_map(Block::first_line),
201 Block::Reply(_) | Block::Rule => None,
202 }
203 }
204
205 pub fn last_inline_mut(&mut self) -> Option<&mut Inline> {
206 match self {
207 Block::Paragraph(inline) => Some(inline),
208 Block::List { items, .. } => items.last_mut()?.last_mut()?.last_inline_mut(),
209 _ => None,
210 }
211 }
212}
213
214pub fn strip_image_placeholders(blocks: Vec<Block>) -> Vec<Block> {
215 blocks
216 .into_iter()
217 .filter_map(|block| match block {
218 Block::Paragraph(inline) => {
219 let stripped = inline.without(IMAGE_PLACEHOLDER);
220 (!stripped.is_blank()).then_some(Block::Paragraph(stripped))
221 }
222 Block::Reply(children) => {
223 let children = strip_image_placeholders(children);
224 (!children.is_empty()).then_some(Block::Reply(children))
225 }
226 Block::Quote(children) => {
227 let children = strip_image_placeholders(children);
228 (!children.is_empty()).then_some(Block::Quote(children))
229 }
230 Block::List {
231 ordered,
232 start,
233 items,
234 } => {
235 let items: Vec<Vec<Block>> = items
236 .into_iter()
237 .map(strip_image_placeholders)
238 .filter(|item| !item.is_empty())
239 .collect();
240 (!items.is_empty()).then_some(Block::List {
241 ordered,
242 start,
243 items,
244 })
245 }
246 other => Some(other),
247 })
248 .collect()
249}
250
251pub fn layout_blocks(spans: &[Span]) -> Vec<Block> {
252 let mut blocks = Vec::new();
253 let mut current = Inline::default();
254 for span in spans {
255 match span {
256 Span::CodeBlock { language, code } => {
257 flush(&mut current, &mut blocks, Block::Paragraph);
258 blocks.push(Block::Code {
259 language: language.clone(),
260 code: code.trim_end_matches('\n').to_owned(),
261 });
262 }
263 Span::Quote(children) | Span::BlockQuote(children) => {
264 flush(&mut current, &mut blocks, Block::Paragraph);
265 let inner = layout_blocks(children);
266 if !inner.is_empty() {
267 blocks.push(match span {
268 Span::Quote(_) => Block::Reply(inner),
269 _ => Block::Quote(inner),
270 });
271 }
272 }
273 Span::List {
274 ordered,
275 start,
276 items,
277 } => {
278 flush(&mut current, &mut blocks, Block::Paragraph);
279 let items: Vec<Vec<Block>> = items
280 .iter()
281 .map(|item| layout_blocks(item))
282 .filter(|item| !item.is_empty())
283 .collect();
284 if !items.is_empty() {
285 blocks.push(Block::List {
286 ordered: *ordered,
287 start: *start,
288 items,
289 });
290 }
291 }
292 Span::Table { header, rows } => {
293 flush(&mut current, &mut blocks, Block::Paragraph);
294 let rows: Vec<Vec<Inline>> = rows
295 .iter()
296 .map(|row| row.iter().map(|cell| inline_of(cell)).collect())
297 .collect();
298 if !rows.is_empty() {
299 blocks.push(Block::Table {
300 header: *header,
301 rows,
302 });
303 }
304 }
305 Span::Heading { level, children } => {
306 flush(&mut current, &mut blocks, Block::Paragraph);
307 let inline = inline_of(children);
308 if !inline.is_blank() {
309 blocks.push(Block::Heading {
310 level: *level,
311 inline,
312 });
313 }
314 }
315 Span::Rule => {
316 flush(&mut current, &mut blocks, Block::Paragraph);
317 blocks.push(Block::Rule);
318 }
319 other => append_inline(
320 std::slice::from_ref(other),
321 StyleFlags::default(),
322 None,
323 &mut current,
324 ),
325 }
326 }
327 flush(&mut current, &mut blocks, Block::Paragraph);
328 blocks
329}
330
331fn inline_of(spans: &[Span]) -> Inline {
332 let mut inline = Inline::default();
333 append_inline(spans, StyleFlags::default(), None, &mut inline);
334 inline.trim_end();
335 pad_code(inline)
336}
337
338fn flush(current: &mut Inline, blocks: &mut Vec<Block>, make: fn(Inline) -> Block) {
339 let mut inline = std::mem::take(current);
340 inline.trim_end();
341 let leading = inline.text.len() - inline.text.trim_start_matches('\n').len();
342 if leading > 0 {
343 inline.text.drain(..leading);
344 inline.segments.retain_mut(|segment| {
345 segment.range.start = segment.range.start.saturating_sub(leading);
346 segment.range.end = segment.range.end.saturating_sub(leading);
347 segment.range.start < segment.range.end
348 });
349 }
350 if !inline.is_blank() {
351 blocks.push(make(pad_code(inline)));
352 }
353}
354
355fn pad_code(inline: Inline) -> Inline {
356 if !inline.segments.iter().any(|segment| segment.style.code) {
357 return inline;
358 }
359 let mut padded = Inline::default();
360 for (index, segment) in inline.segments.iter().enumerate() {
361 let text = &inline.text[segment.range.clone()];
362 let touches = |neighbour: Option<&Segment>| {
363 neighbour.is_some_and(|neighbour| !neighbour.style.code && !neighbour.range.is_empty())
364 };
365 let previous = index
366 .checked_sub(1)
367 .and_then(|previous| inline.segments.get(previous));
368 if segment.style.code && touches(previous) {
369 padded.push(CODE_PADDING, StyleFlags::default(), None);
370 }
371 padded.push(text, segment.style, segment.link.as_deref());
372 if segment.style.code && touches(inline.segments.get(index + 1)) {
373 padded.push(CODE_PADDING, StyleFlags::default(), None);
374 }
375 }
376 padded
377}
378
379fn append_inline(spans: &[Span], style: StyleFlags, link: Option<&str>, out: &mut Inline) {
380 for span in spans {
381 match span {
382 Span::Text(text) if link.is_none() && !style.code => {
383 push_linkified(text, style, out);
384 }
385 Span::Text(text) => out.push(text, style, link),
386 Span::Bold(children) => append_inline(
387 children,
388 StyleFlags {
389 bold: true,
390 ..style
391 },
392 link,
393 out,
394 ),
395 Span::Italic(children) => append_inline(
396 children,
397 StyleFlags {
398 italic: true,
399 ..style
400 },
401 link,
402 out,
403 ),
404 Span::Strike(children) => append_inline(
405 children,
406 StyleFlags {
407 strike: true,
408 ..style
409 },
410 link,
411 out,
412 ),
413 Span::Underline(children) => append_inline(
414 children,
415 StyleFlags {
416 underline: true,
417 ..style
418 },
419 link,
420 out,
421 ),
422 Span::Colored {
423 color,
424 background,
425 children,
426 } => append_inline(
427 children,
428 StyleFlags {
429 color: color.or(style.color),
430 background: background.or(style.background),
431 ..style
432 },
433 link,
434 out,
435 ),
436 Span::Heading { children, .. } => {
437 append_inline(
438 children,
439 StyleFlags {
440 bold: true,
441 ..style
442 },
443 link,
444 out,
445 );
446 out.push("\n", style, link);
447 }
448 Span::Code(text) => out.push(
449 text,
450 StyleFlags {
451 code: true,
452 ..style
453 },
454 link,
455 ),
456 Span::CodeBlock { code, .. } => out.push(
457 code.trim_end_matches('\n'),
458 StyleFlags {
459 code: true,
460 ..style
461 },
462 link,
463 ),
464 Span::Link { url, children } => append_inline(
465 children,
466 StyleFlags {
467 link: true,
468 ..style
469 },
470 Some(url.as_str()),
471 out,
472 ),
473 Span::Mention { name, .. } => {
474 let label = format!("@{}", name.trim_start_matches('@'));
475 out.push(
476 &label,
477 StyleFlags {
478 mention: true,
479 ..style
480 },
481 link,
482 );
483 }
484 Span::LineBreak | Span::Rule => out.push("\n", style, link),
485 Span::Quote(children) | Span::BlockQuote(children) => {
486 append_inline(children, style, link, out);
487 out.push("\n", style, link);
488 }
489 Span::List { items, .. } => {
490 for item in items {
491 append_inline(item, style, link, out);
492 out.push("\n", style, link);
493 }
494 }
495 Span::Table { rows, .. } => {
496 for row in rows {
497 for (index, cell) in row.iter().enumerate() {
498 if index > 0 {
499 out.push(" ", style, link);
500 }
501 append_inline(cell, style, link, out);
502 }
503 out.push("\n", style, link);
504 }
505 }
506 Span::Image { .. } => out.push(IMAGE_PLACEHOLDER, style, link),
507 }
508 }
509}
510
511fn push_linkified(text: &str, style: StyleFlags, out: &mut Inline) {
512 let mut rest = text;
513 while let Some((start, end)) = find_url(rest) {
514 out.push(&rest[..start], style, None);
515 let visible = &rest[start..end];
516 let target = if visible.starts_with("www.") {
517 format!("https://{visible}")
518 } else {
519 visible.to_owned()
520 };
521 out.push(
522 visible,
523 StyleFlags {
524 link: true,
525 ..style
526 },
527 Some(&target),
528 );
529 rest = &rest[end..];
530 }
531 out.push(rest, style, None);
532}
533
534fn find_url(text: &str) -> Option<(usize, usize)> {
535 let mut previous_is_boundary = true;
536 for (start, character) in text.char_indices() {
537 if previous_is_boundary
538 && let Some(prefix) = URL_PREFIXES
539 .iter()
540 .find(|prefix| text[start..].starts_with(**prefix))
541 {
542 let length = text[start..]
543 .find(char::is_whitespace)
544 .unwrap_or(text.len() - start);
545 let end = start + trimmed_url_length(&text[start..start + length]);
546 if end > start + prefix.len() {
547 return Some((start, end));
548 }
549 }
550 previous_is_boundary = !character.is_alphanumeric();
551 }
552 None
553}
554
555fn trimmed_url_length(candidate: &str) -> usize {
556 let mut url = candidate;
557 while let Some(last) = url.chars().next_back() {
558 let opener = match last {
559 ')' => Some('('),
560 ']' => Some('['),
561 '}' => Some('{'),
562 _ => None,
563 };
564 let trimmable = match opener {
565 Some(opener) => url.matches(last).count() > url.matches(opener).count(),
566 None => TRAILING_PUNCTUATION.contains(last),
567 };
568 if !trimmable {
569 break;
570 }
571 url = &url[..url.len() - last.len_utf8()];
572 }
573 url.len()
574}
575
576#[cfg(test)]
577mod tests {
578 use super::*;
579
580 fn text(value: &str) -> Span {
581 Span::Text(value.to_owned())
582 }
583
584 #[test]
585 fn plain_text_is_one_paragraph() {
586 let blocks = layout_blocks(&[text("hello ")]);
587 assert_eq!(blocks, vec![Block::Paragraph(Inline::plain("hello"))]);
588 }
589
590 #[test]
591 fn bold_and_italic_nest_into_flags() {
592 let blocks = layout_blocks(&[
593 text("a "),
594 Span::Bold(vec![Span::Italic(vec![text("b")])]),
595 text(" c"),
596 ]);
597 let Block::Paragraph(inline) = &blocks[0] else {
598 panic!("paragraph expected")
599 };
600 assert_eq!(inline.text, "a b c");
601 assert_eq!(inline.segments.len(), 3);
602 assert!(inline.segments[1].style.bold && inline.segments[1].style.italic);
603 assert_eq!(inline.segments[1].range, 2..3);
604 }
605
606 #[test]
607 fn link_keeps_url_and_range() {
608 let blocks = layout_blocks(&[
609 text("see "),
610 Span::Link {
611 url: "https://example.com".into(),
612 children: vec![text("here")],
613 },
614 ]);
615 let Block::Paragraph(inline) = &blocks[0] else {
616 panic!("paragraph expected")
617 };
618 assert_eq!(
619 inline.links(),
620 vec![(4..8, "https://example.com".to_owned())]
621 );
622 }
623
624 fn paragraph_inline(spans: &[Span]) -> Inline {
625 let Block::Paragraph(inline) = layout_blocks(spans).remove(0) else {
626 panic!("paragraph expected")
627 };
628 inline
629 }
630
631 #[test]
632 fn plain_url_at_end_becomes_link() {
633 let inline = paragraph_inline(&[text("see https://example.com/a")]);
634 assert_eq!(
635 inline.links(),
636 vec![(4..25, "https://example.com/a".to_owned())]
637 );
638 assert!(inline.segments[1].style.link);
639 }
640
641 #[test]
642 fn trailing_period_stays_outside_url() {
643 let inline = paragraph_inline(&[text("go to http://example.com. Then")]);
644 assert_eq!(
645 inline.links(),
646 vec![(6..24, "http://example.com".to_owned())]
647 );
648 }
649
650 #[test]
651 fn balanced_parentheses_stay_in_url() {
652 let inline = paragraph_inline(&[text("https://example.com/Rust_(language)")]);
653 assert_eq!(inline.links()[0].1, "https://example.com/Rust_(language)");
654 let wrapped = paragraph_inline(&[text("(see https://example.com)")]);
655 assert_eq!(wrapped.links()[0].1, "https://example.com");
656 }
657
658 #[test]
659 fn www_url_gets_https_target() {
660 let inline = paragraph_inline(&[text("www.example.com!")]);
661 assert_eq!(inline.text, "www.example.com!");
662 assert_eq!(
663 inline.links(),
664 vec![(0..15, "https://www.example.com".to_owned())]
665 );
666 }
667
668 #[test]
669 fn text_without_url_has_no_links() {
670 let inline = paragraph_inline(&[text("no link, just www. and https:// here")]);
671 assert!(inline.links().is_empty());
672 }
673
674 #[test]
675 fn url_inside_link_stays_one_link() {
676 let inline = paragraph_inline(&[Span::Link {
677 url: "https://target.example".into(),
678 children: vec![text("https://shown.example")],
679 }]);
680 assert_eq!(
681 inline.links(),
682 vec![(0..21, "https://target.example".to_owned())]
683 );
684 }
685
686 #[test]
687 fn mention_gets_an_at_sign_once() {
688 let blocks = layout_blocks(&[
689 Span::Mention {
690 name: "Ada".into(),
691 id: None,
692 },
693 text(" "),
694 Span::Mention {
695 name: "@Bob".into(),
696 id: None,
697 },
698 ]);
699 let Block::Paragraph(inline) = &blocks[0] else {
700 panic!("paragraph expected")
701 };
702 assert_eq!(inline.text, "@Ada @Bob");
703 assert!(inline.segments[0].style.mention);
704 }
705
706 #[test]
707 fn code_block_splits_paragraphs() {
708 let blocks = layout_blocks(&[
709 text("before"),
710 Span::CodeBlock {
711 language: None,
712 code: "let x = 1;\n".into(),
713 },
714 text("after"),
715 ]);
716 assert_eq!(
717 blocks,
718 vec![
719 Block::Paragraph(Inline::plain("before")),
720 Block::Code {
721 language: None,
722 code: "let x = 1;".into()
723 },
724 Block::Paragraph(Inline::plain("after")),
725 ]
726 );
727 }
728
729 #[test]
730 fn lists_nest_and_quotes_are_blocks() {
731 let blocks = layout_blocks(&[
732 Span::List {
733 ordered: true,
734 start: 3,
735 items: vec![
736 vec![text("one")],
737 vec![
738 text("two"),
739 Span::List {
740 ordered: false,
741 start: 1,
742 items: vec![vec![text("inner")]],
743 },
744 ],
745 ],
746 },
747 Span::Quote(vec![text("quoted")]),
748 ]);
749 assert_eq!(
750 blocks,
751 vec![
752 Block::List {
753 ordered: true,
754 start: 3,
755 items: vec![
756 vec![Block::Paragraph(Inline::plain("one"))],
757 vec![
758 Block::Paragraph(Inline::plain("two")),
759 Block::List {
760 ordered: false,
761 start: 1,
762 items: vec![vec![Block::Paragraph(Inline::plain("inner"))]],
763 },
764 ],
765 ],
766 },
767 Block::Reply(vec![Block::Paragraph(Inline::plain("quoted"))]),
768 ]
769 );
770 }
771
772 #[test]
773 fn heading_rule_and_table_are_blocks() {
774 let blocks = layout_blocks(&[
775 Span::Heading {
776 level: 2,
777 children: vec![text("Title")],
778 },
779 Span::Rule,
780 Span::Table {
781 header: true,
782 rows: vec![vec![vec![text("a")], vec![text("b")]]],
783 },
784 ]);
785 assert_eq!(
786 blocks,
787 vec![
788 Block::Heading {
789 level: 2,
790 inline: Inline::plain("Title")
791 },
792 Block::Rule,
793 Block::Table {
794 header: true,
795 rows: vec![vec![Inline::plain("a"), Inline::plain("b")]],
796 },
797 ]
798 );
799 }
800
801 #[test]
802 fn strike_underline_and_color_become_flags() {
803 let inline = paragraph_inline(&[
804 Span::Strike(vec![text("old")]),
805 Span::Underline(vec![text("u")]),
806 Span::Colored {
807 color: Some(0xff0000),
808 background: Some(0xffff00),
809 children: vec![text("c")],
810 },
811 ]);
812 assert!(inline.segments[0].style.strike);
813 assert!(inline.segments[1].style.underline);
814 assert_eq!(inline.segments[2].style.color, Some(0xff0000));
815 assert_eq!(inline.segments[2].style.background, Some(0xffff00));
816 }
817
818 #[test]
819 fn inline_code_gets_room_next_to_text() {
820 let inline = paragraph_inline(&[
821 text("run "),
822 Span::Code("x".into()),
823 text(". "),
824 Span::Code("y".into()),
825 ]);
826 assert_eq!(inline.text, "run \u{2004}x\u{2004}. \u{2004}y");
827 assert_eq!(inline.plain_text(), "run x. y");
828 }
829
830 #[test]
831 fn image_becomes_placeholder() {
832 let blocks = layout_blocks(&[Span::Image {
833 hosted_content_url: "u".into(),
834 }]);
835 assert_eq!(
836 blocks,
837 vec![Block::Paragraph(Inline::plain(IMAGE_PLACEHOLDER))]
838 );
839 }
840
841 #[test]
842 fn stripping_placeholders_keeps_the_text_around_them() {
843 let blocks = layout_blocks(&[
844 text("look "),
845 Span::Bold(vec![text("here")]),
846 Span::LineBreak,
847 Span::Image {
848 hosted_content_url: "u".into(),
849 },
850 ]);
851 let stripped = strip_image_placeholders(blocks);
852 let Block::Paragraph(inline) = &stripped[0] else {
853 panic!("paragraph expected")
854 };
855 assert_eq!(inline.text, "look here");
856 assert!(inline.segments.last().unwrap().style.bold);
857 let only_image = layout_blocks(&[Span::Image {
858 hosted_content_url: "u".into(),
859 }]);
860 assert!(strip_image_placeholders(only_image).is_empty());
861 }
862
863 #[test]
864 fn line_break_stays_inside_paragraph() {
865 let blocks = layout_blocks(&[text("a"), Span::LineBreak, text("b")]);
866 assert_eq!(blocks, vec![Block::Paragraph(Inline::plain("a\nb"))]);
867 }
868
869 #[test]
870 fn empty_input_has_no_blocks() {
871 assert!(layout_blocks(&[]).is_empty());
872 assert!(layout_blocks(&[Span::LineBreak, text(" ")]).is_empty());
873 }
874}