adf2html/nodes/block/
heading.rs

1use serde::{Deserialize, Serialize};
2
3use crate::nodes::inline::inline_node::InlineNode;
4
5#[derive(Clone, Debug, Serialize, Deserialize)]
6#[serde(rename_all = "camelCase")]
7pub struct Heading {
8    pub content: Option<Vec<InlineNode>>,
9    #[serde(rename = "attrs")]
10    pub attributes: Attributes,
11}
12
13#[derive(Clone, Debug, Serialize, Deserialize)]
14#[serde(rename_all = "camelCase")]
15pub struct Attributes {
16    pub level: i8,
17    pub local_id: Option<String>,
18}
19
20impl Heading {
21    pub fn to_html(&self, issue_or_comment_link: &String) -> String {
22        let tag = format!("h{}", self.attributes.level);
23        let id = self.attributes.local_id.as_ref().map(|id| format!(" id = {id}")).unwrap_or_default();
24
25        let mut html = String::new();
26
27        if let Some(content) = &self.content {
28            html = content
29                .iter()
30                .map(|n| n.to_html(issue_or_comment_link))
31                .collect();
32        }
33
34        format!(r#"<{tag}{id} style = "padding: 4px;">{html}</{tag}>"#)
35    }
36}