Fix bug in Gollum tags filter

The gollums tag filter is in the wrong place because when it gerates
the image, we have already run the `ImageLazyLoad` filter. This results
in an empty image because it has the wrong attributes.
parent 8cbd3ebd
---
title: Fix bug in Gollum Tags filter
merge_request: 56638
author:
type: fixed
......@@ -98,14 +98,15 @@ module Banzai
return unless image?(content)
path =
if url?(content)
path = content
content
elsif file = wiki.find_file(content, load_content: false)
path = ::File.join(wiki_base_path, file.path)
file.path
end
if path
content_tag(:img, nil, data: { src: path }, class: 'gfm')
content_tag(:img, nil, src: path, class: 'gfm')
end
end
......
......@@ -5,7 +5,7 @@ module Banzai
class WikiPipeline < FullPipeline
def self.filters
@filters ||= begin
super.insert_after(Filter::TableOfContentsFilter, Filter::GollumTagsFilter)
super.insert_before(Filter::ImageLazyLoadFilter, Filter::GollumTagsFilter)
.insert_before(Filter::TaskListFilter, Filter::WikiLinkFilter)
end
end
......
......@@ -27,7 +27,7 @@ RSpec.describe Banzai::Filter::GollumTagsFilter do
tag = '[[images/image.jpg]]'
doc = filter("See #{tag}", wiki: wiki)
expect(doc.at_css('img')['data-src']).to eq "#{wiki.wiki_base_path}/images/image.jpg"
expect(doc.at_css('img')['src']).to eq 'images/image.jpg'
end
it 'does not creates img tag if image does not exist' do
......@@ -45,7 +45,7 @@ RSpec.describe Banzai::Filter::GollumTagsFilter do
tag = '[[http://example.com/image.jpg]]'
doc = filter("See #{tag}", wiki: wiki)
expect(doc.at_css('img')['data-src']).to eq "http://example.com/image.jpg"
expect(doc.at_css('img')['src']).to eq "http://example.com/image.jpg"
end
it 'does not creates img tag for invalid URL' do
......
......@@ -289,4 +289,29 @@ RSpec.describe Banzai::Pipeline::WikiPipeline do
expect(output).to include('<audio src="/wiki_link_ns/wiki_link_project/-/wikis/nested/twice/audio%20file%20name.wav"')
end
end
describe 'gollum tag filters' do
context 'when local image file exists' do
it 'sets the proper attributes for the image' do
gollum_file_double = double('Gollum::File',
mime_type: 'image/jpeg',
name: 'images/image.jpg',
path: 'images/image.jpg',
raw_data: '')
wiki_file = Gitlab::Git::WikiFile.new(gollum_file_double)
markdown = "[[#{wiki_file.path}]]"
expect(wiki).to receive(:find_file).with(wiki_file.path, load_content: false).and_return(wiki_file)
output = described_class.to_html(markdown, project: project, wiki: wiki, page_slug: page.slug)
doc = Nokogiri::HTML::DocumentFragment.parse(output)
full_path = "/wiki_link_ns/wiki_link_project/-/wikis/nested/twice/#{wiki_file.path}"
expect(doc.css('a')[0].attr('href')).to eq(full_path)
expect(doc.css('img')[0].attr('class')).to eq('gfm lazy')
expect(doc.css('img')[0].attr('data-src')).to eq(full_path)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment