debian-mirror-gitlab/lib/banzai/filter/external_link_filter.rb
2016-06-16 23:09:34 +05:30

31 lines
689 B
Ruby

module Banzai
module Filter
# HTML Filter to modify the attributes of external links
class ExternalLinkFilter < HTML::Pipeline::Filter
def call
doc.search('a').each do |node|
link = node.attr('href')
next unless link
# Skip non-HTTP(S) links
next unless link.start_with?('http')
# Skip internal links
next if link.start_with?(internal_url)
node.set_attribute('rel', 'nofollow noreferrer')
node.set_attribute('target', '_blank')
end
doc
end
private
def internal_url
@internal_url ||= Gitlab.config.gitlab.url
end
end
end
end