Content: Extract icontext,icon from Node::Tag

This commit is contained in:
asonix 2021-02-08 22:34:46 -06:00
parent 39f3a19189
commit 640419cc84
2 changed files with 75 additions and 19 deletions

View file

@ -54,6 +54,8 @@ fn attribute_filter<'u>(element: &str, attribute: &str, value: &'u str) -> Optio
| ("a", "rel")
| ("a", "title")
| ("a", "href")
| ("a", "class")
| ("img", "align")
| ("img", "src")
| ("img", "title")
| ("img", "alt") => Some(Cow::Borrowed(value)),
@ -85,6 +87,10 @@ static AMMONIA_CONFIG: Lazy<Builder> = Lazy::new(|| {
pre_hs.insert("codeblock");
pre_hs.insert("toolkit-code--pre");
let a_hs = classes.entry("a").or_insert(HashSet::new());
a_hs.insert("icon");
a_hs.insert("icontext");
let mut schemes = HashSet::new();
schemes.insert("http");
schemes.insert("https");

View file

@ -230,14 +230,20 @@ fn render_nodes(nodes: Vec<RenderNode>) -> String {
img,
href,
} => {
format!("<a href=\"{}\" rel=\"noopener noreferer nofollow\">", href)
+ &format!(
"<img src=\"{}\" title=\"@{handle}@{domain}\" alt=\"@{handle}@{domain}\" />",
img,
handle = handle,
domain = domain
) + &format!("@{}@{}", handle, domain)
+ &format!("</a>")
format!(
"<a class=\"icontext\" href=\"{}\" rel=\"noopener noreferer nofollow\">",
href
) + &format!(
"<img src=\"{}\" title=\"@{handle}@{domain}\" alt=\"@{handle}@{domain}\" align=\"middle\" />",
img,
handle = handle,
domain = domain
) + "</a> " + &format!(
"<a class=\"icontext\" href=\"{}\" rel=\"noopener noreferer nofollow\">",
href
)
+ &format!("@{}@{}", handle, domain)
+ "</a>"
}
RenderNode::Icon {
handle,
@ -245,9 +251,11 @@ fn render_nodes(nodes: Vec<RenderNode>) -> String {
img,
href,
} => {
format!("<a href=\"{}\" rel=\"noopener noreferer nofollow\">", href)
+ &format!(
"<img src=\"{}\" title=\"@{handle}@{domain}\" alt=\"@{handle}@{domain}\" />",
format!(
"<a class=\"icon\" href=\"{}\" rel=\"noopener noreferer nofollow\">",
href
) + &format!(
"<img src=\"{}\" title=\"@{handle}@{domain}\" alt=\"@{handle}@{domain}\" align=\"middle\" />",
img,
handle = handle,
domain = domain
@ -364,14 +372,56 @@ where
tag,
attr,
children,
} => to_render(
(f)(NodeView::Tag {
tag,
attr: attr.as_deref().map(Cow::Borrowed),
}),
Some(children),
f,
),
} => match tag {
Tag::IconText => match children.get(0) {
Some(Node::Handle { handle, domain }) => to_render(
(f)(NodeView::IconText {
handle: Cow::Borrowed(&handle),
domain: Cow::Borrowed(&domain),
href: None,
img: None,
}),
None,
f,
),
_ => to_render(
(f)(NodeView::Tag {
tag,
attr: attr.as_deref().map(Cow::Borrowed),
}),
Some(children),
f,
),
},
Tag::Icon => match children.get(0) {
Some(Node::Handle { handle, domain }) => to_render(
(f)(NodeView::Icon {
handle: Cow::Borrowed(&handle),
domain: Cow::Borrowed(&domain),
href: None,
img: None,
}),
None,
f,
),
_ => to_render(
(f)(NodeView::Tag {
tag,
attr: attr.as_deref().map(Cow::Borrowed),
}),
Some(children),
f,
),
},
tag => to_render(
(f)(NodeView::Tag {
tag,
attr: attr.as_deref().map(Cow::Borrowed),
}),
Some(children),
f,
),
},
Node::Url { href } => to_render(
(f)(NodeView::Url {
href: Cow::Borrowed(&href),