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

54 lines
1.4 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
module Banzai
module Filter
# HTML filter that "fixes" links to pages/files in a wiki.
# Rewrite rules are documented in the `WikiPipeline` spec.
2016-06-02 11:05:42 +05:30
#
# Context options:
# :project_wiki
class WikiLinkFilter < HTML::Pipeline::Filter
2019-07-31 22:56:46 +05:30
include Gitlab::Utils::SanitizeNodeLink
2016-06-02 11:05:42 +05:30
def call
return doc unless project_wiki?
2019-07-31 22:56:46 +05:30
doc.search('a:not(.gfm)').each { |el| process_link(el.attribute('href'), el) }
2019-12-21 20:55:43 +05:30
doc.search('video, audio').each { |el| process_link(el.attribute('src'), el) }
2019-07-31 22:56:46 +05:30
2018-11-20 20:47:30 +05:30
doc.search('img').each do |el|
attr = el.attribute('data-src') || el.attribute('src')
2019-07-31 22:56:46 +05:30
process_link(attr, el)
2016-06-02 11:05:42 +05:30
end
doc
end
protected
2019-07-31 22:56:46 +05:30
def process_link(link_attr, node)
process_link_attr(link_attr)
remove_unsafe_links({ node: node }, remove_invalid_links: false)
end
2016-06-02 11:05:42 +05:30
def project_wiki?
!context[:project_wiki].nil?
end
def process_link_attr(html_attr)
return if html_attr.blank?
2016-06-02 11:05:42 +05:30
html_attr.value = apply_rewrite_rules(html_attr.value)
2016-08-24 12:49:21 +05:30
rescue URI::Error, Addressable::URI::InvalidURIError
2016-06-02 11:05:42 +05:30
# noop
end
def apply_rewrite_rules(link_string)
Rewriter.new(link_string, wiki: context[:project_wiki], slug: context[:page_slug]).apply_rules
2016-06-02 11:05:42 +05:30
end
end
end
end