From 100865c92367217a6cc38c42d17cd166c5efbdad Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sun, 1 Sep 2024 16:08:25 -0700 Subject: [PATCH] server: use same html cleanup idiom in nm as we do in newreader --- server/src/nm.rs | 59 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/server/src/nm.rs b/server/src/nm.rs index 1b440c8..b317461 100644 --- a/server/src/nm.rs +++ b/server/src/nm.rs @@ -17,7 +17,7 @@ use crate::{ Attachment, Body, DispositionType, Email, EmailThread, Header, Html, Message, PlainText, Tag, Thread, ThreadSummary, UnhandledContentType, }, - linkify_html, sanitize_html, + linkify_html, InlineStyle, SanitizeHtml, Transformer, }; const TEXT_PLAIN: &'static str = "text/plain"; @@ -169,17 +169,29 @@ pub async fn thread( }; Body::Html(Html { - html: format!( - r#"

{}

"#, - // Trim newlines to prevent excessive white space at the beginning/end of - // presenation. Leave tabs and spaces incase plain text attempts to center a - // header on the first line. - sanitize_html( - &linkify_html(&text.trim_matches('\n')), - &cid_prefix, - &base_url - )? - ), + html: { + let body_tranformers: Vec> = vec![ + Box::new(InlineStyle), + Box::new(SanitizeHtml { + cid_prefix: &cid_prefix, + base_url: &base_url, + }), + ]; + let mut html = linkify_html(&text.trim_matches('\n')); + for t in body_tranformers.iter() { + if t.should_run(&None, &html) { + html = t.transform(&None, &html).await?; + } + } + + format!( + r#"

{}

"#, + // Trim newlines to prevent excessive white space at the beginning/end of + // presenation. Leave tabs and spaces incase plain text attempts to center a + // header on the first line. + html + ) + }, content_tree: if debug_content_tree { render_content_type_tree(&m) } else { @@ -187,8 +199,27 @@ pub async fn thread( }, }) } - Body::Html(Html { html, content_tree }) => Body::Html(Html { - html: sanitize_html(&html, &cid_prefix, &base_url)?, + Body::Html(Html { + mut html, + content_tree, + }) => Body::Html(Html { + html: { + let body_tranformers: Vec> = vec![ + // TODO: this breaks things like emails from calendar + //Box::new(InlineStyle), + Box::new(SanitizeHtml { + cid_prefix: &cid_prefix, + base_url: &base_url, + }), + ]; + for t in body_tranformers.iter() { + if t.should_run(&None, &html) { + html = t.transform(&None, &html).await?; + } + } + html + }, + content_tree: if debug_content_tree { render_content_type_tree(&m) } else {