gitlab-org--gitlab-foss/lib/banzai/filter/wiki_link_filter.rb

43 lines
977 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-04-01 03:17:37 +00:00
require 'uri'
module Banzai
module Filter
# HTML filter that "fixes" links to pages/files in a wiki.
# Rewrite rules are documented in the `WikiPipeline` spec.
2016-04-01 03:17:37 +00:00
#
# Context options:
# :project_wiki
class WikiLinkFilter < HTML::Pipeline::Filter
def call
return doc unless project_wiki?
doc.search('a:not(.gfm)').each do |el|
process_link_attr el.attribute('href')
end
doc
end
protected
def project_wiki?
!context[:project_wiki].nil?
end
def process_link_attr(html_attr)
return if html_attr.blank?
2016-04-01 03:17:37 +00:00
html_attr.value = apply_rewrite_rules(html_attr.value)
rescue URI::Error, Addressable::URI::InvalidURIError
2016-04-01 03:17:37 +00:00
# noop
end
def apply_rewrite_rules(link_string)
Rewriter.new(link_string, wiki: context[:project_wiki], slug: context[:page_slug]).apply_rules
2016-04-01 03:17:37 +00:00
end
end
end
end