use comrak::nodes::{AstNode, NodeValue};
pub(crate) fn unbold_headings<'a>(root: &'a AstNode<'a>) {
let headings: Vec<_> = root
.descendants()
.filter(|node| matches!(node.data.borrow().value, NodeValue::Heading(_)))
.collect();
for node in headings {
let children: Vec<_> = node.children().collect();
if children.len() != 1 {
continue;
}
let child = children[0];
let child_value = child.data.borrow().value.clone();
match child_value {
NodeValue::Strong => {
let grandchildren: Vec<_> = child.children().collect();
for gc in &grandchildren {
gc.detach();
}
child.detach();
for gc in grandchildren {
node.append(gc);
}
}
NodeValue::Emph => {
let emph_children: Vec<_> = child.children().collect();
if emph_children.len() == 1
&& matches!(emph_children[0].data.borrow().value, NodeValue::Strong)
{
let strong = emph_children[0];
let strong_children: Vec<_> = strong.children().collect();
for gc in &strong_children {
gc.detach();
}
strong.detach();
for gc in strong_children {
child.append(gc);
}
}
}
_ => {}
}
}
}
pub(crate) fn doc_cleanups<'a>(root: &'a AstNode<'a>) {
unbold_headings(root);
}