Open links in a new tab.
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user