use lol_html::html_content::Element;
use tracing::debug;
pub fn standardize_heading(el: &mut Element) {
let tag_name = el.tag_name();
if tag_name == "div" {
if let Some(role) = el.get_attribute("role") {
if role == "heading" {
if let Some(level) = el.get_attribute("aria-level") {
let new_tag = match level.as_str() {
"1" => "h1",
"2" => "h2",
"3" => "h3",
"4" => "h4",
"5" => "h5",
"6" => "h6",
_ => return,
};
debug!("Converting div[role=heading] to {}", new_tag);
}
}
}
}
if matches!(tag_name.as_str(), "h1" | "h2" | "h3" | "h4" | "h5" | "h6") {
let attrs_to_remove: Vec<String> = el
.attributes()
.iter()
.filter(|attr| !matches!(attr.name().as_str(), "id" | "class"))
.map(lol_html::html_content::Attribute::name)
.collect();
for attr in attrs_to_remove {
el.remove_attribute(&attr);
}
}
}
pub fn process_h1_element(el: &mut Element, title: &str) {
if el.tag_name() == "h1" {
if let Some(text_attr) = el.get_attribute("data-text") {
let normalized_element = normalize_text(&text_attr);
let normalized_title = normalize_text(title);
if normalized_element == normalized_title {
debug!("Removing H1 that matches title");
el.remove();
} else {
debug!("Converting H1 to H2");
}
}
}
}
fn normalize_text(text: &str) -> String {
text
.replace('\u{00A0}', " ") .split_whitespace()
.collect::<Vec<_>>()
.join(" ")
.trim()
.to_lowercase()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_normalize_text() {
let text = " Hello\u{00A0}World ";
assert_eq!(normalize_text(text), "hello world");
}
}