diff options
author | Gabriel Mazetto <gabriel@gitlab.com> | 2016-04-01 00:17:37 -0300 |
---|---|---|
committer | Gabriel Mazetto <gabriel@gitlab.com> | 2016-04-01 00:17:37 -0300 |
commit | 28266d9d610123d20333c6b05fcf83d1fb3e336a (patch) | |
tree | 6701842693419aa77c3fa46d37f6ffc6851855a8 /lib | |
parent | 7d56d592cd7d1df718d59981af1ced9d0be8eaac (diff) | |
download | gitlab-ce-28266d9d610123d20333c6b05fcf83d1fb3e336a.tar.gz |
Added WikiLinkFilter
Diffstat (limited to 'lib')
-rw-r--r-- | lib/banzai/filter/wiki_link_filter.rb | 52 | ||||
-rw-r--r-- | lib/banzai/pipeline/wiki_pipeline.rb | 6 |
2 files changed, 56 insertions, 2 deletions
diff --git a/lib/banzai/filter/wiki_link_filter.rb b/lib/banzai/filter/wiki_link_filter.rb new file mode 100644 index 00000000000..7a585eff653 --- /dev/null +++ b/lib/banzai/filter/wiki_link_filter.rb @@ -0,0 +1,52 @@ +require 'uri' + +module Banzai + module Filter + # HTML filter that "fixes" relative links to files in a repository. + # + # 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? + + uri = URI(html_attr.value) + if uri.relative? && uri.path.present? && uri.path + html_attr.value = rebuild_wiki_uri(uri).to_s + end + rescue URI::Error + # noop + end + + def rebuild_wiki_uri(uri) + uri.path = ::File.join(project_wiki_base_path, uri.path) + uri + end + + def project_wiki + context[:project_wiki] + end + + def project_wiki_base_path + project_wiki && project_wiki.wiki_base_path + end + end + end +end diff --git a/lib/banzai/pipeline/wiki_pipeline.rb b/lib/banzai/pipeline/wiki_pipeline.rb index 0b5a9e0b2b8..1cdb3808961 100644 --- a/lib/banzai/pipeline/wiki_pipeline.rb +++ b/lib/banzai/pipeline/wiki_pipeline.rb @@ -2,8 +2,10 @@ module Banzai module Pipeline class WikiPipeline < FullPipeline def self.filters - @filters ||= super.insert_after(Filter::TableOfContentsFilter, - Filter::GollumTagsFilter) + @filters ||= begin + super.insert_after(Filter::TableOfContentsFilter, Filter::GollumTagsFilter) + .insert_after(Filter::GollumTagsFilter, Filter::WikiLinkFilter) + end end end end |