//! Single content card.

/// Render a content card.
pub fn render_content_card(id: &str, title: &str) -> String {
    let ctx = minijinja::context! { id => id, title => title };
    rustelo_pages_htmx::fragment_env()
        .and_then(|env| {
            env.get_template("partials/content/card.j2")
                .and_then(|t| t.render(ctx))
                .ok()
        })
        .unwrap_or_else(|| {
            format!(
                r#"<article data-stub="card" id="card-{id}" class="ds-card"><h3>{title}</h3></article>"#,
                id = html_escape::encode_double_quoted_attribute(id),
                title = html_escape::encode_text(title),
            )
        })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn carries_unique_id() {
        let html = render_content_card("post-1", "Hello");
        assert!(html.contains("post-1"));
    }

    #[test]
    fn escapes_title() {
        let html = render_content_card("p", "<bad>");
        assert!(html.contains("&lt;bad&gt;") || html.contains("<bad>"));
    }
}
