2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
class WikiLinkFilter < HTML::Pipeline::Filter
|
|
|
|
class Rewriter
|
|
|
|
def initialize(link_string, wiki:, slug:)
|
|
|
|
@uri = Addressable::URI.parse(link_string)
|
|
|
|
@wiki_base_path = wiki && wiki.wiki_base_path
|
|
|
|
@slug = slug
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_rules
|
2018-03-17 18:26:18 +05:30
|
|
|
# Special case: relative URLs beginning with `/uploads/` refer to
|
2018-11-20 20:47:30 +05:30
|
|
|
# user-uploaded files will be handled elsewhere.
|
|
|
|
return @uri.to_s if public_upload?
|
|
|
|
|
|
|
|
# Special case: relative URLs beginning with Wikis::CreateAttachmentService::ATTACHMENT_PATH
|
|
|
|
# refer to user-uploaded files to the wiki repository.
|
|
|
|
unless repository_upload?
|
|
|
|
apply_file_link_rules!
|
|
|
|
apply_hierarchical_link_rules!
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
apply_relative_link_rules!
|
|
|
|
@uri.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Of the form 'file.md'
|
|
|
|
def apply_file_link_rules!
|
|
|
|
@uri = Addressable::URI.join(@slug, @uri) if @uri.extname.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Of the form `./link`, `../link`, or similar
|
|
|
|
def apply_hierarchical_link_rules!
|
|
|
|
@uri = Addressable::URI.join(@slug, @uri) if @uri.to_s[0] == '.'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Any link _not_ of the form `http://example.com/`
|
|
|
|
def apply_relative_link_rules!
|
|
|
|
if @uri.relative? && @uri.path.present?
|
|
|
|
link = ::File.join(@wiki_base_path, @uri.path)
|
2016-09-29 09:46:39 +05:30
|
|
|
link = "#{link}##{@uri.fragment}" if @uri.fragment
|
2016-06-16 23:09:34 +05:30
|
|
|
@uri = Addressable::URI.parse(link)
|
|
|
|
end
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
def public_upload?
|
|
|
|
@uri.relative? && @uri.path.starts_with?('/uploads/')
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository_upload?
|
|
|
|
@uri.relative? && @uri.path.starts_with?(Wikis::CreateAttachmentService::ATTACHMENT_PATH)
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|