don't lose data when obfuscating mails
All checks were successful
ci/woodpecker/push/publish Pipeline was successful

This commit is contained in:
Mylloon 2024-06-02 18:19:58 +02:00
parent 4146f2ea45
commit bece8ef147
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 73 additions and 30 deletions

View file

@ -7,6 +7,7 @@ use lol_html::html_content::ContentType;
use lol_html::{element, rewrite_str, HtmlRewriter, RewriteStrSettings, Settings}; use lol_html::{element, rewrite_str, HtmlRewriter, RewriteStrSettings, Settings};
use ramhorns::Content; use ramhorns::Content;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use std::fmt::Debug;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
@ -426,20 +427,23 @@ fn hljs_replace<'a>(root: &'a AstNode<'a>, mermaid_str: &str) {
/// Obfuscate email if email found /// Obfuscate email if email found
fn mail_obfuscation(html: &str) -> (String, bool) { fn mail_obfuscation(html: &str) -> (String, bool) {
let modified = Arc::new(AtomicBool::new(false)); let modified = Arc::new(AtomicBool::new(false));
(
rewrite_str( // Modify HTML for mails
let new_html = rewrite_str(
html, html,
RewriteStrSettings { RewriteStrSettings {
element_content_handlers: vec![element!("a[href^='mailto:']", |el| { element_content_handlers: vec![element!("a[href^='mailto:']", |el| {
modified.store(true, Ordering::SeqCst); modified.store(true, Ordering::SeqCst);
// Get mail address
let link = el.get_attribute("href").unwrap(); let link = el.get_attribute("href").unwrap();
let (uri, mail) = &link.split_at(7); let (uri, mail) = &link.split_at(7);
let (before, after) = mail.split_once('@').unwrap(); let (before, after) = mail.split_once('@').unwrap();
let modified_mail = format!("{before}<span class='at'>(at)</span>{after}"); // Preserve old data and add obfuscated mail address
el.prepend("<span data='", ContentType::Html);
el.set_inner_content(&modified_mail, ContentType::Html); let modified_mail = format!("' />{before}<span class='at'>(at)</span>{after}");
el.append(&modified_mail, ContentType::Html);
// Change href // Change href
Ok(el.set_attribute("href", &format!("{uri}{before} at {after}"))?) Ok(el.set_attribute("href", &format!("{uri}{before} at {after}"))?)
@ -447,7 +451,38 @@ fn mail_obfuscation(html: &str) -> (String, bool) {
..RewriteStrSettings::default() ..RewriteStrSettings::default()
}, },
) )
.unwrap(), .unwrap();
modified.load(Ordering::SeqCst),
) 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::<Vec<&str>>()
.join(" "),
)?)
}
)],
..RewriteStrSettings::default()
},
)
.unwrap(),
is_modified,
)
} else {
(new_html, is_modified)
}
} }

View file

@ -21,7 +21,7 @@ struct IndexTemplate {
navbar: NavBar, navbar: NavBar,
name: String, name: String,
pronouns: Option<String>, pronouns: Option<String>,
content: Option<File>, file: Option<File>,
avatar: String, avatar: String,
avatar_caption: String, avatar_caption: String,
avatar_style: StyleAvatar, avatar_style: StyleAvatar,
@ -77,7 +77,7 @@ fn build_page(config: Config) -> String {
index: true, index: true,
..NavBar::default() ..NavBar::default()
}, },
content: file, file,
name, name,
pronouns, pronouns,
avatar, avatar,

View file

@ -1,13 +1,18 @@
window.addEventListener("load", () => { window.addEventListener("load", () => {
Array.from(document.getElementsByClassName("at")).forEach((elem) => { 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 // Change link
const a = elem.parentElement; a.setAttribute("href", a.getAttribute("href").replace(" at ", "@"));
const href = a.getAttribute("href");
elem.parentElement.setAttribute(
"href",
href.replace(" at ", elem.textContent)
);
}); });
}); });

View file

@ -33,13 +33,16 @@
</div> </div>
<p id="subname"></p> <p id="subname"></p>
{{#content}} {{&content}} {{/content}} {{^content}} {{#file}} {{&content}} {{/file}} {{^file}}
<p> <p>
<b>Welcome to EWP</b>, create a <code>index.md</code> file inside your <b>Welcome to EWP</b>, create a <code>index.md</code> file inside your
<code>data/</code> directory to get started. <code>data/</code> directory to get started.
</p> </p>
{{/content}} {{/data}} {{/file}}
</main> </main>
<script src="/js/index.js"></script> <script src="/js/index.js"></script>
{{#file}} {{#metadata}}
{{#mail_obfsucated}}{{>libs/mail_obfuscater.html}}{{/mail_obfsucated}}
{{/metadata}} {{/file}} {{/data}}
</body> </body>
</html> </html>