use std::fmt::Write;
use dom_query::Selection;
use crate::options::MarkdownOptions;
use crate::utils::get_tag_name;
use super::inline::convert_inline_content_impl;
pub(crate) fn convert_list(
sel: &Selection,
output: &mut String,
options: &MarkdownOptions,
ordered: bool,
depth: usize,
) {
let indent_str = options.list_indent();
let indent = indent_str.repeat(depth);
let mut index = 1;
for node in sel.children().nodes() {
let child = Selection::from(*node);
let tag = get_tag_name(&child);
if tag == "li" {
output.push_str(&indent);
if ordered {
let _ = write!(output, "{index}. ");
index += 1;
} else {
output.push_str("- ");
}
convert_li_content(&child, output, options, depth);
output.push('\n');
for nested in child.children().nodes() {
let nested_sel = Selection::from(*nested);
let nested_tag = get_tag_name(&nested_sel);
if nested_tag == "ul" || nested_tag == "ol" {
convert_list(&nested_sel, output, options, nested_tag == "ol", depth + 1);
}
}
}
}
}
fn convert_li_content(
li: &Selection,
output: &mut String,
options: &MarkdownOptions,
depth: usize,
) {
convert_inline_content_impl(li, output, options, true, depth);
}