Skip to content

Instantly share code, notes, and snippets.

@pablojimeno
Forked from patrickfav/example_jekyll.md
Last active March 16, 2023 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablojimeno/049d43bfe61c2f16c78427c6ac746dda to your computer and use it in GitHub Desktop.
Save pablojimeno/049d43bfe61c2f16c78427c6ac746dda to your computer and use it in GitHub Desktop.
A Liquid Filter for obfuscating an Email Address (can be used with Jekyll aswell)

In Jekyll set a variable for the mail, e.g. in the _config.yml

email: name@mail.com

then use it in your page

Reach me under:	{{ site.email | mailObfuscate }}

which will generate the following HTML

<a href="#" data-contact="bmFtZUBtYWlsLmNvbQ== " target="_blank" onfocus="this.href = 'mailto:' + atob(this.dataset.contact)">    
    <script type="text/javascript">document.write(atob("bmFtZUBtYWlsLmNvbQ== "));</script>
</a>

This uses some simple obfuscation techniques of url encode and base64 encode the mail and use JS to support the link and write it to HTML programmatically. This is certainly not bulletproof, but a good shield and in combination with a good spam filter this will fix your problem with mail crawlers.

These techniques are partly from http://techblog.tilllate.com/2008/07/20/ten-methods-to-obfuscate-e-mail-addresses-compared/

require "base64"
require "uri"
module ObfuscateMailAddress
def mailObfuscate(input)
base64Mail = Base64.strict_encode64(Addressable::URI.encode(input))
# See http://techblog.tilllate.com/2008/07/20/ten-methods-to-obfuscate-e-mail-addresses-compared/
output = "<a href=\"#\" "
output += "data-contact=\"#{base64Mail}\" target=\"_blank\" "
output += "onfocus=\"this.href = 'mailto:' + atob(this.dataset.contact)\">"
output += "<script type=\"text/javascript\">document.write(atob(\"#{base64Mail}\"));</script></a>"
return output
end
end
Liquid::Template.register_filter(ObfuscateMailAddress)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment