wip: mail obfuscation (#51)
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending approval

This commit is contained in:
Mylloon 2024-05-03 12:49:50 +02:00
parent ab5ce11037
commit d352206e29
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
6 changed files with 44 additions and 0 deletions

View file

@ -100,6 +100,7 @@ pub struct Metadata {
pub math: bool,
pub mermaid: bool,
pub syntax_highlight: bool,
pub mail_obfsucated: bool,
}
impl Metadata {
@ -212,6 +213,7 @@ fn fix_images_and_integration(path: &str, html: String) -> (String, Metadata) {
math: false,
mermaid: false,
syntax_highlight: false,
mail_obfsucated: false,
};
(
@ -284,14 +286,17 @@ pub fn read_md(
let mut html_content = String::from_utf8(html).unwrap();
let children_metadata;
let mail_obfsucated;
(html_content, children_metadata) = fix_images_and_integration(path, html_content);
html_content = custom_img_size(html_content);
(html_content, mail_obfsucated) = mail_obfuscation(html_content);
let mut final_metadata = Metadata {
info: metadata,
mermaid: check_mermaid(root, mermaid_name),
syntax_highlight: check_code(root, &[mermaid_name.into()]),
math: check_math(&html_content),
mail_obfsucated,
};
final_metadata.merge(children_metadata);
@ -424,3 +429,32 @@ fn hljs_replace<'a>(root: &'a AstNode<'a>, mermaid_str: &str) {
}
});
}
/// Obfuscate email if email found
fn mail_obfuscation(html: String) -> (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();
let modified_mail = format!("{}<span class='at'>(at)</span>{}", before, after);
el.set_inner_content(&modified_mail, ContentType::Html);
// TODO: Change href
Ok(el.set_attribute("href", &link)?)
})],
..RewriteStrSettings::default()
},
)
.unwrap(),
modified.load(Ordering::SeqCst),
)
}

View file

@ -78,6 +78,7 @@ fn read_pdf(data: Vec<u8>) -> File {
mermaid: false,
syntax_highlight: false,
math: false,
mail_obfsucated: false,
},
content: format!(
r#"<embed

View file

@ -0,0 +1,6 @@
window.addEventListener("load", () => {
Array.from(document.getElementsByClassName("at")).forEach((elem) => {
// TODO: Change link
elem.textContent = "@";
});
});

View file

@ -36,6 +36,7 @@
{{#mermaid}}{{>libs/mermaid_footer.html}}{{/mermaid}}
{{#math}}{{>libs/katex_footer.html}}{{/math}}
{{#syntax_highlight}}{{>libs/hljs_footer.html}}{{/syntax_highlight}}
{{#mail_obfsucated}}{{>libs/mail_obfuscater.html}}{{/mail_obfsucated}}
{{/metadata}} {{/post}} {{/data}}
</body>
</html>

View file

@ -24,6 +24,7 @@
{{#metadata}} {{#mermaid}}{{>libs/mermaid_footer.html}}{{/mermaid}}
{{#math}}{{>libs/katex_footer.html}}{{/math}}
{{#syntax_highlight}}{{>libs/hljs_footer.html}}{{/syntax_highlight}}
{{#mail_obfsucated}}{{>libs/mail_obfuscater.html}}{{/mail_obfsucated}}
{{/metadata}} {{/content}} {{/data}}
<script src="/js/cours.js"></script>
</body>

View file

@ -0,0 +1 @@
<script src="/js/mail_obfuscation.js"></script>