linkify URLs in plaintext emails.
This commit is contained in:
@@ -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