summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/base_relative_link_filter.rb
blob: 3f775abb18595476af960ce6c1e8c46b0de3ac30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

require 'uri'

module Banzai
  module Filter
    class BaseRelativeLinkFilter < HTML::Pipeline::Filter
      include Gitlab::Utils::StrongMemoize

      CSS   = 'a:not(.gfm), img:not(.gfm), video:not(.gfm), audio:not(.gfm)'
      XPATH = Gitlab::Utils::Nokogiri.css_to_xpath(CSS).freeze

      protected

      def linkable_attributes
        if Feature.enabled?(:optimize_linkable_attributes, project, default_enabled: :yaml)
          # Nokorigi Nodeset#search performs badly for documents with many nodes
          #
          # Here we store fetched attributes in the shared variable "result"
          # This variable is passed through the chain of filters and can be
          # accessed by them
          result[:linkable_attributes] ||= fetch_linkable_attributes
        else
          strong_memoize(:linkable_attributes) do
            fetch_linkable_attributes
          end
        end
      end

      def relative_url_root
        Gitlab.config.gitlab.relative_url_root.presence || '/'
      end

      def project
        context[:project]
      end

      private

      def unescape_and_scrub_uri(uri)
        Addressable::URI.unescape(uri).scrub.delete("\0")
      end

      def fetch_linkable_attributes
        attrs = []

        attrs += doc.xpath(XPATH).flat_map do |el|
          [el.attribute('href'), el.attribute('src'), el.attribute('data-src')]
        end

        attrs.reject { |attr| attr.blank? || attr.value.start_with?('//') }
      end
    end
  end
end