linkify URLs in plaintext emails.
This commit is contained in:
@@ -16,7 +16,7 @@ use memmap::MmapOptions;
|
||||
use notmuch::Notmuch;
|
||||
use rocket::time::Instant;
|
||||
|
||||
use crate::sanitize_html;
|
||||
use crate::{linkify_html, sanitize_html};
|
||||
|
||||
pub struct QueryRoot;
|
||||
|
||||
@@ -347,8 +347,11 @@ impl QueryRoot {
|
||||
.get_first_value("date")
|
||||
.and_then(|d| mailparse::dateparse(&d).ok());
|
||||
let body = match extract_body(&m)? {
|
||||
Body::PlainText(PlainText { text, content_tree }) => Body::PlainText(PlainText {
|
||||
text,
|
||||
Body::PlainText(PlainText { text, content_tree }) => Body::Html(Html {
|
||||
html: format!(
|
||||
r#"<p class="view-part-text-plain">{}</p>"#,
|
||||
linkify_html(&text)
|
||||
),
|
||||
content_tree: if debug_content_tree {
|
||||
render_content_type_tree(&m)
|
||||
} else {
|
||||
|
||||
@@ -3,6 +3,7 @@ pub mod graphql;
|
||||
pub mod nm;
|
||||
|
||||
use css_inline::{CSSInliner, InlineError, InlineOptions};
|
||||
use linkify::{LinkFinder, LinkKind};
|
||||
use log::error;
|
||||
use lol_html::{element, errors::RewritingError, rewrite_str, RewriteStrSettings};
|
||||
use maplit::{hashmap, hashset};
|
||||
@@ -16,6 +17,22 @@ pub enum SanitizeError {
|
||||
InlineError(#[from] InlineError),
|
||||
}
|
||||
|
||||
pub fn linkify_html(text: &str) -> String {
|
||||
let finder = LinkFinder::new();
|
||||
let mut parts = Vec::new();
|
||||
for span in finder.spans(text) {
|
||||
// TODO(wathiede): use Cow<str>?
|
||||
match span.kind() {
|
||||
// Text as-is
|
||||
None => parts.push(span.as_str().to_string()),
|
||||
// Wrap in anchor tag
|
||||
Some(LinkKind::Url) => parts.push(format!(r#"<a href="{0}">{0}</a>"#, span.as_str())),
|
||||
_ => todo!("unhandled kind: {:?}", span.kind().unwrap()),
|
||||
}
|
||||
}
|
||||
parts.join("")
|
||||
}
|
||||
|
||||
pub fn sanitize_html(html: &str) -> Result<String, SanitizeError> {
|
||||
let element_content_handlers = vec![
|
||||
// Open links in new tab
|
||||
@@ -204,8 +221,6 @@ pub fn sanitize_html(html: &str) -> Result<String, SanitizeError> {
|
||||
//let clean_html = inlined_html;
|
||||
|
||||
Ok(rewrite_str(
|
||||
// TODO(wathiede): replace ammonia with more lol-html rules.
|
||||
// &ammonia::clean(&html),
|
||||
&clean_html,
|
||||
RewriteStrSettings {
|
||||
element_content_handlers,
|
||||
|
||||
Reference in New Issue
Block a user