From bece8ef1478bce882e72913fa2fdb26f2f48fa7f Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 2 Jun 2024 18:19:58 +0200 Subject: [PATCH] don't lose data when obfuscating mails --- src/misc/markdown.rs | 73 ++++++++++++++++++++++++++--------- src/routes/index.rs | 4 +- static/js/mail_obfuscation.js | 19 +++++---- templates/index.html | 7 +++- 4 files changed, 73 insertions(+), 30 deletions(-) diff --git a/src/misc/markdown.rs b/src/misc/markdown.rs index 3d5409f..1ed58b0 100644 --- a/src/misc/markdown.rs +++ b/src/misc/markdown.rs @@ -7,6 +7,7 @@ use lol_html::html_content::ContentType; use lol_html::{element, rewrite_str, HtmlRewriter, RewriteStrSettings, Settings}; use ramhorns::Content; use serde::{Deserialize, Deserializer}; +use std::fmt::Debug; use std::fs; use std::path::Path; use std::sync::atomic::{AtomicBool, Ordering}; @@ -426,28 +427,62 @@ fn hljs_replace<'a>(root: &'a AstNode<'a>, mermaid_str: &str) { /// Obfuscate email if email found fn mail_obfuscation(html: &str) -> (String, bool) { let modified = Arc::new(AtomicBool::new(false)); - ( - rewrite_str( - html, - RewriteStrSettings { - element_content_handlers: vec![element!("a[href^='mailto:']", |el| { - modified.store(true, Ordering::SeqCst); - let link = el.get_attribute("href").unwrap(); - let (uri, mail) = &link.split_at(7); - let (before, after) = mail.split_once('@').unwrap(); + // Modify HTML for mails + let new_html = rewrite_str( + html, + RewriteStrSettings { + element_content_handlers: vec![element!("a[href^='mailto:']", |el| { + modified.store(true, Ordering::SeqCst); - let modified_mail = format!("{before}(at){after}"); + // Get mail address + let link = el.get_attribute("href").unwrap(); + let (uri, mail) = &link.split_at(7); + let (before, after) = mail.split_once('@').unwrap(); - el.set_inner_content(&modified_mail, ContentType::Html); + // Preserve old data and add obfuscated mail address + el.prepend("{before}(at){after}"); + el.append(&modified_mail, ContentType::Html); - // Change href - Ok(el.set_attribute("href", &format!("{uri}{before} at {after}"))?) - })], - ..RewriteStrSettings::default() - }, - ) - .unwrap(), - modified.load(Ordering::SeqCst), + // Change href + Ok(el.set_attribute("href", &format!("{uri}{before} at {after}"))?) + })], + ..RewriteStrSettings::default() + }, ) + .unwrap(); + + let is_modified = modified.load(Ordering::SeqCst); + + if is_modified { + // Remove old data email if exists + ( + rewrite_str( + &new_html, + RewriteStrSettings { + element_content_handlers: vec![element!( + "a[href^='mailto:'] > span[data]", + |el| { + Ok(el.set_attribute( + "data", + // Remove mails + &el.get_attribute("data") + .unwrap() + .split_whitespace() + .filter(|word| !word.contains('@')) + .collect::>() + .join(" "), + )?) + } + )], + ..RewriteStrSettings::default() + }, + ) + .unwrap(), + is_modified, + ) + } else { + (new_html, is_modified) + } } diff --git a/src/routes/index.rs b/src/routes/index.rs index 601977e..f9e7d5b 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -21,7 +21,7 @@ struct IndexTemplate { navbar: NavBar, name: String, pronouns: Option, - content: Option, + file: Option, avatar: String, avatar_caption: String, avatar_style: StyleAvatar, @@ -77,7 +77,7 @@ fn build_page(config: Config) -> String { index: true, ..NavBar::default() }, - content: file, + file, name, pronouns, avatar, diff --git a/static/js/mail_obfuscation.js b/static/js/mail_obfuscation.js index 2dcde65..9a81f1e 100644 --- a/static/js/mail_obfuscation.js +++ b/static/js/mail_obfuscation.js @@ -1,13 +1,18 @@ window.addEventListener("load", () => { Array.from(document.getElementsByClassName("at")).forEach((elem) => { - elem.textContent = "@"; + const span = elem.parentElement; + + // Replace (at) by @ + elem.outerHTML = "@"; + + // Remove useless span + const a = span.parentElement; + + // Correct text + const data = span.getAttribute("data"); + a.innerHTML = data.length > 0 ? data : span.textContent; // Change link - const a = elem.parentElement; - const href = a.getAttribute("href"); - elem.parentElement.setAttribute( - "href", - href.replace(" at ", elem.textContent) - ); + a.setAttribute("href", a.getAttribute("href").replace(" at ", "@")); }); }); diff --git a/templates/index.html b/templates/index.html index 2df0219..63b99f2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -33,13 +33,16 @@

- {{#content}} {{&content}} {{/content}} {{^content}} + {{#file}} {{&content}} {{/file}} {{^file}}

Welcome to EWP, create a index.md file inside your data/ directory to get started.

- {{/content}} {{/data}} + {{/file}} + {{#file}} {{#metadata}} + {{#mail_obfsucated}}{{>libs/mail_obfuscater.html}}{{/mail_obfsucated}} + {{/metadata}} {{/file}} {{/data}}