| 1 | use teams_core::{Draft, MentionInput}; |
| 2 | |
| 3 | use crate::render::{Block, layout_blocks}; |
| 4 | |
| 5 | const BODY_PREVIEW_CHARACTERS: usize = 1200; |
| 6 | |
| 7 | fn body_preview(text: &str) -> String { |
| 8 | let text = text.trim(); |
| 9 | let cut: String = text.chars().take(BODY_PREVIEW_CHARACTERS).collect(); |
| 10 | if cut.len() == text.len() { |
| 11 | return cut; |
| 12 | } |
| 13 | match cut.rfind('\n') { |
| 14 | Some(newline) => cut[..newline].to_owned(), |
| 15 | None => cut, |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | pub fn body_blocks(text: &str, mentions: &[mcp::ProposalMention]) -> Vec<Block> { |
| 20 | let inputs: Vec<MentionInput> = mentions |
| 21 | .iter() |
| 22 | .map(|mention| MentionInput::user(&mention.user_id, &mention.name)) |
| 23 | .collect(); |
| 24 | let preview = body_preview(text); |
| 25 | let (html, _) = teams_core::apply_mentions(&Draft::from_markdown(&preview).to_html(), &inputs); |
| 26 | layout_blocks(&teams_core::html_to_spans(&html)) |
| 27 | } |
| 28 | |
| 29 | #[cfg(test)] |
| 30 | mod tests { |
| 31 | use super::{BODY_PREVIEW_CHARACTERS, body_blocks, body_preview}; |
| 32 | use crate::render::Block; |
| 33 | |
| 34 | #[test] |
| 35 | fn the_card_body_renders_markdown_like_a_sent_message() { |
| 36 | let blocks = body_blocks("- item\n- `code`", &[]); |
| 37 | let [Block::List { items, ordered, .. }] = blocks.as_slice() else { |
| 38 | panic!("{blocks:?}"); |
| 39 | }; |
| 40 | assert!(!ordered); |
| 41 | assert_eq!(items.len(), 2); |
| 42 | let Block::Paragraph(inline) = &items[1][0] else { |
| 43 | panic!("{items:?}"); |
| 44 | }; |
| 45 | assert_eq!(inline.text, "code"); |
| 46 | assert!(inline.segments.iter().any(|segment| segment.style.code)); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn long_bodies_are_cut_at_the_last_line_break() { |
| 51 | let text = format!("{}\n{}", "a".repeat(1000), "b".repeat(500)); |
| 52 | assert_eq!(body_preview(&text), "a".repeat(1000)); |
| 53 | assert_eq!(body_preview("short"), "short"); |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn long_bodies_are_cut_before_layout() { |
| 58 | let long = "word ".repeat(2000); |
| 59 | let blocks = body_blocks(&long, &[]); |
| 60 | let Block::Paragraph(inline) = &blocks[0] else { |
| 61 | panic!("{blocks:?}"); |
| 62 | }; |
| 63 | assert!(inline.text.chars().count() <= BODY_PREVIEW_CHARACTERS); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn mentions_and_links_render_as_styled_segments() { |
| 68 | let blocks = body_blocks( |
| 69 | "**Hi** @Ada Example, see https://example.com/x", |
| 70 | &[mcp::ProposalMention { |
| 71 | user_id: "u1".to_owned(), |
| 72 | name: "Ada Example".to_owned(), |
| 73 | }], |
| 74 | ); |
| 75 | let Block::Paragraph(inline) = &blocks[0] else { |
| 76 | panic!("{blocks:?}"); |
| 77 | }; |
| 78 | assert!(inline.segments.iter().any(|segment| segment.style.bold)); |
| 79 | assert!(inline.segments.iter().any(|segment| segment.style.mention)); |
| 80 | assert!(inline.segments.iter().any(|segment| segment.link.is_some())); |
| 81 | assert!(!inline.text.contains("**")); |
| 82 | } |
| 83 | } |