use crate::error::DocsError;
use ammonia::Builder;
use std::collections::{HashMap, HashSet};
#[must_use]
pub fn sanitize(html: &str) -> String {
builder().clean(html).to_string()
}
pub fn render_untrusted(markdown: &str) -> Result<String, DocsError> {
let html = crate::render::render_inner(markdown, "", false)?;
Ok(sanitize(&html))
}
fn builder() -> Builder<'static> {
let mut b = Builder::default();
let token_attrs: HashSet<&str> = ["class", "mathvariant", "mathsize", "mathcolor"]
.into_iter()
.collect();
let math_attrs: HashSet<&str> = ["class", "display", "xmlns"].into_iter().collect();
let math_tags: HashSet<&str> = [
"mi",
"mn",
"mo",
"ms",
"mtext",
"mrow",
"mfrac",
"msqrt",
"mroot",
"msub",
"msup",
"msubsup",
"munder",
"mover",
"munderover",
"mtable",
"mtr",
"mtd",
"mspace",
"mstyle",
"mmultiscripts",
"mprescripts",
"mphantom",
"menclose",
"merror",
]
.into_iter()
.collect();
b.add_tags(math_tags);
let mut attrs: HashMap<&str, HashSet<&str>> = HashMap::new();
for tag in [
"mi",
"mn",
"mo",
"ms",
"mtext",
"mrow",
"mfrac",
"msqrt",
"mroot",
"msub",
"msup",
"msubsup",
"munder",
"mover",
"munderover",
"mtable",
"mtr",
"mtd",
"mspace",
"mstyle",
"mmultiscripts",
"mprescripts",
"mphantom",
"menclose",
"merror",
] {
attrs.insert(tag, token_attrs.clone());
}
b.add_tags(["math"]);
attrs.insert("math", math_attrs);
b.tag_attributes(attrs);
b
}