Skip to content

Instantly share code, notes, and snippets.

@renatocarvalho
Last active May 9, 2017 03:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatocarvalho/afda8238f57f57b94ae5 to your computer and use it in GitHub Desktop.
Save renatocarvalho/afda8238f57f57b94ae5 to your computer and use it in GitHub Desktop.
Middleman email obfuscate helper
# ====================================
# Obfuscate email
# Adapted from:
# http://stackoverflow.com/questions/483212/effective-method-to-hide-email-from-spam-bots2
# Usage:
# = mailto('hi@email.com', 'Get in touch', 'btn btn--nav')
# ====================================
MAIL_TO = 'mailto:'
AT = '@'
DOT = '.'
def mailto email="user@example.com", string="contact me", classes
comp = email.split("@")
# process string, if it is an email address
if string.include?("@") then
string.gsub!("@", AT + "‌").gsub!(".", DOT)
end
return "<a class=\"#{classes}\" href='javascript:void(0)' rel='nofollow' onclick='str1=\"#{comp[0]}\";str2=\"#{comp[1]}\";this.href=\"#{MAIL_TO}\" + str1 + \"@\" + str2'>#{string}</a>"
end
@Ross-Hunter
Copy link

Seems legit, but https://github.com/amsardesai/middleman-protect-emails worked for me (for future generations who come here looking for this functionality).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment