don't lose data when obfuscating mails
All checks were successful
ci/woodpecker/push/publish Pipeline was successful
All checks were successful
ci/woodpecker/push/publish Pipeline was successful
This commit is contained in:
parent
4146f2ea45
commit
bece8ef147
4 changed files with 73 additions and 30 deletions
|
@ -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}<span class='at'>(at)</span>{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("<span data='", ContentType::Html);
|
||||
let modified_mail = format!("' />{before}<span class='at'>(at)</span>{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::<Vec<&str>>()
|
||||
.join(" "),
|
||||
)?)
|
||||
}
|
||||
)],
|
||||
..RewriteStrSettings::default()
|
||||
},
|
||||
)
|
||||
.unwrap(),
|
||||
is_modified,
|
||||
)
|
||||
} else {
|
||||
(new_html, is_modified)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ struct IndexTemplate {
|
|||
navbar: NavBar,
|
||||
name: String,
|
||||
pronouns: Option<String>,
|
||||
content: Option<File>,
|
||||
file: Option<File>,
|
||||
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,
|
||||
|
|
|
@ -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 ", "@"));
|
||||
});
|
||||
});
|
||||
|
|
|
@ -33,13 +33,16 @@
|
|||
</div>
|
||||
<p id="subname"></p>
|
||||
|
||||
{{#content}} {{&content}} {{/content}} {{^content}}
|
||||
{{#file}} {{&content}} {{/file}} {{^file}}
|
||||
<p>
|
||||
<b>Welcome to EWP</b>, create a <code>index.md</code> file inside your
|
||||
<code>data/</code> directory to get started.
|
||||
</p>
|
||||
{{/content}} {{/data}}
|
||||
{{/file}}
|
||||
</main>
|
||||
<script src="/js/index.js"></script>
|
||||
{{#file}} {{#metadata}}
|
||||
{{#mail_obfsucated}}{{>libs/mail_obfuscater.html}}{{/mail_obfsucated}}
|
||||
{{/metadata}} {{/file}} {{/data}}
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue