Open links in a new tab.

This commit is contained in:
2024-01-19 21:07:24 -08:00
parent b1ea44963d
commit 304819275d
3 changed files with 336 additions and 26 deletions

View File

@@ -23,6 +23,7 @@ rocket_cors = "0.6.0"
memmap = "0.7.0"
mailparse = "0.14.0"
ammonia = "3.3.0"
lol_html = "1.2.0"
[dependencies.rocket_contrib]
version = "0.4.11"

View File

@@ -11,6 +11,7 @@ use async_graphql::{
SimpleObject, Union,
};
use log::{error, info, warn};
use lol_html::{element, errors::RewritingError, rewrite_str, RewriteStrSettings};
use mailparse::{parse_mail, MailHeader, MailHeaderMap, ParsedMail};
use memmap::MmapOptions;
use notmuch::Notmuch;
@@ -189,6 +190,25 @@ struct Tag {
bg_color: String,
unread: usize,
}
fn sanitize_html(html: &str) -> Result<String, RewritingError> {
let element_content_handlers = vec![
// Open links in new tab
element!("a[href]", |el| {
el.set_attribute("target", "_blank").unwrap();
Ok(())
}),
];
Ok(rewrite_str(
// TODO(wathiede): replace ammonia with more lol-html rules.
&ammonia::clean(&html),
RewriteStrSettings {
element_content_handlers,
..RewriteStrSettings::default()
},
)?)
}
#[Object]
impl QueryRoot {
@@ -354,7 +374,7 @@ impl QueryRoot {
},
}),
Body::Html(Html { html, content_tree }) => Body::Html(Html {
html: ammonia::clean(&html),
html: sanitize_html(&html)?,
content_tree: if debug_content_tree {
render_content_type_tree(&m)
} else {