Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

Ross-Hunter commented May 9, 2017

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