Skip to content

Instantly share code, notes, and snippets.

@milushov
Created February 25, 2014 09:05
Show Gist options
  • Save milushov/9205417 to your computer and use it in GitHub Desktop.
Save milushov/9205417 to your computer and use it in GitHub Desktop.
def make_nofollow_links
domains = %w(inmyroom kitchenmag)
if content.present?
json_content = begin
JSON.parse content
rescue
[]
end
json_content.each do |plugin|
if plugin['name'] == 'text_area'
html_with_links = auto_link(plugin['data'], html: {target: '_blank'})
html_with_links.scan(/(\<a href=["'].*?["']\>.*?\<\/a\>)/).flatten.each do |link|
node = Nokogiri::HTML::DocumentFragment.parse(link).children.first
next if node[:href].match /(#{ domains.join('|') })\.(ru|dev|dev:3000)/
html_with_links.gsub! link, "<a href='#{ node[:href] }' rel='nofollow' target='#{ node[:target] }'>#{ node.content }</a>"
end
plugin['data'] = html_with_links
end
end
json_content.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment