summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/external_link_filter.rb
blob: 0a29c547a4de70ec177eed8fc1112f0882654e4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Banzai
  module Filter
    # HTML Filter to modify the attributes of external links
    class ExternalLinkFilter < HTML::Pipeline::Filter
      def call
        # Skip non-HTTP(S) links and internal links
        doc.xpath("descendant-or-self::a[starts-with(@href, 'http') and not(starts-with(@href, '#{internal_url}'))]").each do |node|
          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