html2md/rewriter/
lists.rs

1use super::counter::Counter;
2use lol_html::html_content::ContentType;
3use lol_html::html_content::Element;
4
5// Function to handle list elements and items
6#[inline]
7pub(crate) fn handle_list_or_item(
8    element: &mut Element,
9    list_type: &mut Option<String>,
10    order_counter: &mut usize,
11) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
12    match element.tag_name().as_str() {
13        "ul" | "menu" => {
14            *list_type = Some("ul".to_string());
15            order_counter.reset(); // Reset the order counter for a new list
16        }
17        "ol" => {
18            *list_type = Some("ol".to_string());
19            order_counter.reset();
20        }
21        "li" => {
22            if list_type.as_deref() == Some("ol") {
23                let order = order_counter.increment();
24                element.before(&format!("\n{}. ", order), ContentType::Text);
25            } else {
26                element.before("\n* ", ContentType::Text);
27            }
28        }
29        _ => (),
30    }
31
32    Ok(())
33}
34
35// Function to handle list elements and items
36#[inline]
37pub(crate) fn handle_list_or_item_send(
38    element: &mut lol_html::send::Element,
39    list_type: &mut Option<String>,
40    order_counter: &mut usize,
41) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
42    match element.tag_name().as_str() {
43        "ul" | "menu" => {
44            *list_type = Some("ul".to_string());
45
46            order_counter.reset();
47        }
48        "ol" => {
49            *list_type = Some("ol".to_string());
50
51            order_counter.reset();
52        }
53        "li" => {
54            let ordered: bool = list_type.as_deref().eq(&Some("ol"));
55
56            if ordered {
57                let order = order_counter.increment();
58
59                element.before(&format!("\n{}. ", order), ContentType::Text);
60            } else {
61                element.before("\n* ", ContentType::Text);
62            }
63        }
64        _ => (),
65    }
66
67    Ok(())
68}