systemprompt_content/
list_items_renderer.rs1use std::sync::Arc;
2
3use anyhow::Result;
4use async_trait::async_trait;
5use serde_json::Value;
6use systemprompt_provider_contracts::{ComponentContext, ComponentRenderer, RenderedComponent};
7
8const PLACEHOLDER_IMAGE_SVG: &str = r#"<div class="card-image card-image--placeholder">
9 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
10 <rect x="3" y="3" width="18" height="18" rx="2" ry="2"/>
11 <circle cx="8.5" cy="8.5" r="1.5"/>
12 <polyline points="21 15 16 10 5 21"/>
13 </svg>
14 </div>"#;
15
16#[derive(Debug, Clone, Copy, Default)]
17pub struct ListItemsCardRenderer;
18
19#[async_trait]
20#[allow(clippy::unnecessary_literal_bound)]
21impl ComponentRenderer for ListItemsCardRenderer {
22 fn component_id(&self) -> &str {
23 "list-items-cards"
24 }
25
26 fn variable_name(&self) -> &str {
27 "ITEMS"
28 }
29
30 fn applies_to(&self) -> Vec<String> {
31 vec!["blog-list".into(), "news-list".into(), "pages-list".into()]
32 }
33
34 async fn render(&self, ctx: &ComponentContext<'_>) -> Result<RenderedComponent> {
35 let items = ctx.all_items.unwrap_or(&[]);
36 let url_prefix = extract_url_prefix(ctx);
37
38 let cards_html: Vec<String> = items
39 .iter()
40 .filter_map(|item| render_card_html(item, &url_prefix))
41 .collect();
42
43 Ok(RenderedComponent::new(
44 self.variable_name(),
45 cards_html.join("\n"),
46 ))
47 }
48
49 fn priority(&self) -> u32 {
50 100
51 }
52}
53
54fn extract_url_prefix(ctx: &ComponentContext<'_>) -> String {
55 ctx.all_items
56 .and_then(|items| items.first())
57 .and_then(|item| item.get("content_type"))
58 .and_then(Value::as_str)
59 .map_or_else(String::new, |ct| {
60 format!("/{}", ct.strip_suffix("-list").unwrap_or(ct))
61 })
62}
63
64fn render_card_html(item: &Value, url_prefix: &str) -> Option<String> {
65 let title = item.get("title")?.as_str()?;
66 let slug = item.get("slug")?.as_str()?;
67 let description = item
68 .get("description")
69 .and_then(Value::as_str)
70 .unwrap_or("");
71 let image = item.get("image").and_then(Value::as_str);
72 let date = format_published_date(item);
73
74 let image_html = render_image_html(image, title);
75
76 Some(format!(
77 r#"<a href="{url_prefix}/{slug}" class="content-card-link">
78 <article class="content-card">
79 {image_html}
80 <div class="card-content">
81 <h2 class="card-title">{title}</h2>
82 <p class="card-excerpt">{description}</p>
83 <div class="card-meta">
84 <time class="card-date">{date}</time>
85 </div>
86 </div>
87 </article>
88</a>"#
89 ))
90}
91
92fn format_published_date(item: &Value) -> String {
93 item.get("published_at")
94 .and_then(Value::as_str)
95 .and_then(|d| chrono::DateTime::parse_from_rfc3339(d).ok())
96 .map_or_else(String::new, |dt| dt.format("%B %d, %Y").to_string())
97}
98
99fn render_image_html(image: Option<&str>, alt: &str) -> String {
100 image.filter(|s| !s.is_empty()).map_or_else(
101 || PLACEHOLDER_IMAGE_SVG.to_string(),
102 |img| {
103 format!(
104 r#"<div class="card-image">
105 <img src="{img}" alt="{alt}" loading="lazy" />
106 </div>"#
107 )
108 },
109 )
110}
111
112pub fn default_list_items_renderer() -> Arc<dyn ComponentRenderer> {
113 Arc::new(ListItemsCardRenderer)
114}