From e919b5a4e9dd5e09628daf28b7b96424b764863b Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 1 Feb 2016 11:07:59 -0500 Subject: [PATCH] Fix relative links in other markup formats - Apply the RelativeLinkFilter filter to other formats, e.g., reStructuredText so links from the Files view or the Project view work - Remove the AsciidocPipeline pipeline Fixes #3533. --- CHANGELOG | 1 + app/helpers/application_helper.rb | 3 +-- app/helpers/gitlab_markdown_helper.rb | 15 +++++++++++++++ lib/banzai/pipeline/asciidoc_pipeline.rb | 11 ----------- lib/gitlab/asciidoc.rb | 4 +--- lib/gitlab/other_markup.rb | 24 ++++++++++++++++++++++++ spec/helpers/application_helper_spec.rb | 4 ++++ spec/lib/gitlab/asciidoc_spec.rb | 16 ---------------- 8 files changed, 46 insertions(+), 32 deletions(-) delete mode 100644 lib/banzai/pipeline/asciidoc_pipeline.rb create mode 100644 lib/gitlab/other_markup.rb diff --git a/CHANGELOG b/CHANGELOG index ea84cf0fd79..78b34b83700 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -16,6 +16,7 @@ v 8.5.0 (unreleased) set it up - API: Added "merge_requests/:merge_request_id/closes_issues" (Gal Schlezinger) - Fix diff comments loaded by AJAX to load comment with diff in discussion tab + - Fix relative links in other markup formats (Ben Boeckel) - Whitelist raw "abbr" elements when parsing Markdown (Benedict Etzel) - Fix label links for a merge request pointing to issues list - Don't vendor minified JS diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 622cbfe3cc4..02357e2f23e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -212,8 +212,7 @@ def render_markup(file_name, file_content) file_content end else - GitHub::Markup.render(file_name, file_content). - force_encoding(file_content.encoding).html_safe + other_markup(file_name, file_content) end rescue RuntimeError simple_format(file_content) diff --git a/app/helpers/gitlab_markdown_helper.rb b/app/helpers/gitlab_markdown_helper.rb index 1a226252251..89d2a648494 100644 --- a/app/helpers/gitlab_markdown_helper.rb +++ b/app/helpers/gitlab_markdown_helper.rb @@ -78,6 +78,21 @@ def asciidoc(text) ) end + def other_markup(file_name, text) + Gitlab::OtherMarkup.render( + file_name, + text, + project: @project, + current_user: (current_user if defined?(current_user)), + + # RelativeLinkFilter + project_wiki: @project_wiki, + requested_path: @path, + ref: @ref, + commit: @commit + ) + end + # Return the first line of +text+, up to +max_chars+, after parsing the line # as Markdown. HTML tags in the parsed output are not counted toward the # +max_chars+ limit. If the length limit falls within a tag's contents, then diff --git a/lib/banzai/pipeline/asciidoc_pipeline.rb b/lib/banzai/pipeline/asciidoc_pipeline.rb deleted file mode 100644 index f1331c0ebf9..00000000000 --- a/lib/banzai/pipeline/asciidoc_pipeline.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Banzai - module Pipeline - class AsciidocPipeline < BasePipeline - def self.filters - [ - Filter::RelativeLinkFilter - ] - end - end - end -end diff --git a/lib/gitlab/asciidoc.rb b/lib/gitlab/asciidoc.rb index b203b9d70e4..0b9c2e730f9 100644 --- a/lib/gitlab/asciidoc.rb +++ b/lib/gitlab/asciidoc.rb @@ -31,9 +31,7 @@ def self.render(input, context, asciidoc_opts = {}) html = ::Asciidoctor.convert(input, asciidoc_opts) - if context[:project] - html = Banzai.render(html, context.merge(pipeline: :asciidoc)) - end + html = Banzai.post_process(html, context) html.html_safe end diff --git a/lib/gitlab/other_markup.rb b/lib/gitlab/other_markup.rb new file mode 100644 index 00000000000..746ec283330 --- /dev/null +++ b/lib/gitlab/other_markup.rb @@ -0,0 +1,24 @@ +module Gitlab + # Parser/renderer for markups without other special support code. + module OtherMarkup + + # Public: Converts the provided markup into HTML. + # + # input - the source text in a markup format + # context - a Hash with the template context: + # :commit + # :project + # :project_wiki + # :requested_path + # :ref + # + def self.render(file_name, input, context) + html = GitHub::Markup.render(file_name, input). + force_encoding(input.encoding) + + html = Banzai.post_process(html, context) + + html.html_safe + end + end +end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 30e353148a8..f6c1005d265 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -293,6 +293,10 @@ def element(*arguments) describe 'render_markup' do let(:content) { 'Noël' } + let(:user) { create(:user) } + before do + allow(helper).to receive(:current_user).and_return(user) + end it 'should preserve encoding' do expect(content.encoding.name).to eq('UTF-8') diff --git a/spec/lib/gitlab/asciidoc_spec.rb b/spec/lib/gitlab/asciidoc_spec.rb index 6beb21c6d2b..736bf787208 100644 --- a/spec/lib/gitlab/asciidoc_spec.rb +++ b/spec/lib/gitlab/asciidoc_spec.rb @@ -42,22 +42,6 @@ module Gitlab end end - context "with project in context" do - - let(:context) { { project: create(:project) } } - - it "should filter converted input via HTML pipeline and return result" do - filtered_html = 'ASCII' - - allow(Asciidoctor).to receive(:convert).and_return(html) - expect(Banzai).to receive(:render) - .with(html, context.merge(pipeline: :asciidoc)) - .and_return(filtered_html) - - expect( render('foo', context) ).to eql filtered_html - end - end - def render(*args) described_class.render(*args) end -- GitLab