Add target="blank" on urls

This commit is contained in:
asonix 2021-02-13 16:36:13 -06:00
parent c723bb759a
commit dcf5ca3e0a
2 changed files with 4 additions and 2 deletions

View file

@ -52,6 +52,7 @@ fn attribute_filter<'u>(element: &str, attribute: &str, value: &'u str) -> Optio
| ("span", "data-symbol")
| ("blockquote", "data-author")
| ("a", "rel")
| ("a", "target")
| ("a", "title")
| ("a", "href")
| ("a", "class")
@ -121,6 +122,7 @@ static AMMONIA_CONFIG: Lazy<Builder> = Lazy::new(|| {
.url_schemes(schemes)
.link_rel(Some("nofollow noopener noreferer"))
.attribute_filter(attribute_filter)
.add_tag_attributes("a", &["target"])
.add_tag_attributes("span", &["style"])
.add_tag_attributes("div", &["style"]);

View file

@ -211,7 +211,7 @@ fn render_nodes(nodes: Vec<RenderNode>) -> String {
Tag::Br => String::from("<br>"),
Tag::Url if !children.is_empty() => {
if let Some(href) = attr {
format!("<a href=\"{}\" rel=\"noopener noreferer nofollow\">", href)
format!("<a href=\"{}\" target=\"_blank\" rel=\"noopener noreferer nofollow\">", href)
+ &render_nodes(children)
+ "</a>"
} else {
@ -221,7 +221,7 @@ fn render_nodes(nodes: Vec<RenderNode>) -> String {
_ => String::new(),
},
RenderNode::Url { href } => format!(
"<a href=\"{href}\" rel=\"noopener noreferer nofollow\">{href}</a>",
"<a href=\"{href}\" target=\"blank\" rel=\"noopener noreferer nofollow\">{href}</a>",
href = href
),
RenderNode::IconText {