summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/relative_link_filter.rb
diff options
context:
space:
mode:
authorFilipa Lacerda <filipa@gitlab.com>2018-01-02 21:06:06 +0000
committerFilipa Lacerda <filipa@gitlab.com>2018-01-02 21:06:06 +0000
commit6db6856d8dee1810a25f1a4c5a665329c5ad598b (patch)
treef8b12c884b42deb3254873ec5c9c0580fb95bac2 /lib/banzai/filter/relative_link_filter.rb
parent7c636732fba98935922ffaadc49b328417e9036c (diff)
parent2cbb2d0eceaed0f31c92d4eed8932e98f4f74559 (diff)
downloadgitlab-ce-6db6856d8dee1810a25f1a4c5a665329c5ad598b.tar.gz
Merge branch 'master' into 41120-performance-bar-auto-scroll
* master: (120 commits) Update CHANGELOG.md for 10.3.3 Fix user membership destroy relation Use heredoc so it's more clear Put all menus under menu module Introduce common project settings and just put Introduce Factory::Resource::DeployKey Introduce expand_deploy_keys Introduce Menu::Side Add documents for GitLab utilities Clears visual token on second backspace Update prometheus gem to version that adds inf+ bucket in accordance with Prometheus docs. Add breadcrumbs to User Settings sub-views Rename asset sync related AWS variables Allow logged in user to change his password Fix 404 error after a user edits an issue description and solves the reCAPTCHA Fix links to old commits in merge requests Typos correction on the advise of @smcgivern Documenting that resolved JIRA tickets are not automatically transitioned Forking a project to a namespace with lower visibility. Keep typographic hierarchy in User Settings ...
Diffstat (limited to 'lib/banzai/filter/relative_link_filter.rb')
-rw-r--r--lib/banzai/filter/relative_link_filter.rb48
1 files changed, 40 insertions, 8 deletions
diff --git a/lib/banzai/filter/relative_link_filter.rb b/lib/banzai/filter/relative_link_filter.rb
index 758f15c8a67..5c197afd782 100644
--- a/lib/banzai/filter/relative_link_filter.rb
+++ b/lib/banzai/filter/relative_link_filter.rb
@@ -2,19 +2,21 @@ require 'uri'
module Banzai
module Filter
- # HTML filter that "fixes" relative links to files in a repository.
+ # HTML filter that "fixes" relative links to uploads or files in a repository.
#
# Context options:
# :commit
+ # :group
# :project
# :project_wiki
# :ref
# :requested_path
class RelativeLinkFilter < HTML::Pipeline::Filter
- def call
- return doc unless linkable_files?
+ include Gitlab::Utils::StrongMemoize
+ def call
@uri_types = {}
+ clear_memoization(:linkable_files)
doc.search('a:not(.gfm)').each do |el|
process_link_attr el.attribute('href')
@@ -31,18 +33,40 @@ module Banzai
protected
def linkable_files?
- context[:project_wiki].nil? && repository.try(:exists?) && !repository.empty?
+ strong_memoize(:linkable_files) do
+ context[:project_wiki].nil? && repository.try(:exists?) && !repository.empty?
+ end
end
def process_link_attr(html_attr)
return if html_attr.blank?
return if html_attr.value.start_with?('//')
+ if html_attr.value.start_with?('/uploads/')
+ process_link_to_upload_attr(html_attr)
+ elsif linkable_files?
+ process_link_to_repository_attr(html_attr)
+ end
+ end
+
+ def process_link_to_upload_attr(html_attr)
+ uri_parts = [html_attr.value]
+
+ if group
+ uri_parts.unshift(relative_url_root, 'groups', group.full_path, '-')
+ elsif project
+ uri_parts.unshift(relative_url_root, project.full_path)
+ end
+
+ html_attr.value = File.join(*uri_parts)
+ end
+
+ def process_link_to_repository_attr(html_attr)
uri = URI(html_attr.value)
if uri.relative? && uri.path.present?
html_attr.value = rebuild_relative_uri(uri).to_s
end
- rescue URI::Error
+ rescue URI::Error, Addressable::URI::InvalidURIError
# noop
end
@@ -51,7 +75,7 @@ module Banzai
uri.path = [
relative_url_root,
- context[:project].full_path,
+ project.full_path,
uri_type(file_path),
Addressable::URI.escape(ref),
Addressable::URI.escape(file_path)
@@ -123,11 +147,19 @@ module Banzai
end
def ref
- context[:ref] || context[:project].default_branch
+ context[:ref] || project.default_branch
+ end
+
+ def group
+ context[:group]
+ end
+
+ def project
+ context[:project]
end
def repository
- @repository ||= context[:project].try(:repository)
+ @repository ||= project&.repository
end
end
end