debian-mirror-gitlab/lib/banzai/filter/base_relative_link_filter.rb

43 lines
983 B
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
require 'uri'
module Banzai
module Filter
class BaseRelativeLinkFilter < HTML::Pipeline::Filter
include Gitlab::Utils::StrongMemoize
2021-06-02 17:11:27 +05:30
CSS = 'a:not(.gfm), img:not(.gfm), video:not(.gfm), audio:not(.gfm)'
XPATH = Gitlab::Utils::Nokogiri.css_to_xpath(CSS).freeze
2020-03-13 15:44:24 +05:30
protected
def linkable_attributes
strong_memoize(:linkable_attributes) do
attrs = []
2021-06-02 17:11:27 +05:30
attrs += doc.xpath(XPATH).flat_map do |el|
[el.attribute('href'), el.attribute('src'), el.attribute('data-src')]
2020-03-13 15:44:24 +05:30
end
2021-06-02 17:11:27 +05:30
attrs.reject { |attr| attr.blank? || attr.value.start_with?('//') }
2020-03-13 15:44:24 +05:30
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)
2020-07-02 01:45:43 +05:30
Addressable::URI.unescape(uri).scrub.delete("\0")
2020-03-13 15:44:24 +05:30
end
end
end
end