Commit 10cab5dc authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'group-wikis-markdown' into 'master'

Use wiki instead of project_wiki in Banzai

See merge request gitlab-org/gitlab!32675
parents ca7d8a03 b9c25013
......@@ -32,7 +32,7 @@ module PreviewMarkdown
def markdown_context_params
case controller_name
when 'wikis' then { pipeline: :wiki, project_wiki: @project_wiki, page_slug: params[:id] }
when 'wikis' then { pipeline: :wiki, wiki: @project_wiki, page_slug: params[:id] }
when 'snippets' then { skip_project_check: true }
when 'groups' then { group: group }
when 'projects' then projects_filter_params
......
......@@ -129,7 +129,7 @@ module MarkupHelper
context.merge!(
pipeline: :wiki,
project: @project,
project_wiki: @project_wiki,
wiki: @project_wiki,
repository: @project_wiki.repository,
page_slug: wiki_page.slug,
issuable_state_filter_enabled: true
......
......@@ -30,7 +30,7 @@ module Banzai
# Note: the table of contents tag is now handled by TableOfContentsTagFilter
#
# Context options:
# :project_wiki (required) - Current project wiki.
# :wiki [Wiki] (required) - Current wiki instance.
#
class GollumTagsFilter < HTML::Pipeline::Filter
include ActionView::Helpers::TagHelper
......@@ -100,8 +100,8 @@ module Banzai
if url?(content)
path = content
elsif file = project_wiki.find_file(content)
path = ::File.join project_wiki_base_path, file.path
elsif file = wiki.find_file(content)
path = ::File.join(wiki_base_path, file.path)
end
if path
......@@ -134,25 +134,25 @@ module Banzai
if url?(reference)
reference
else
::File.join(project_wiki_base_path, reference)
::File.join(wiki_base_path, reference)
end
content_tag(:a, name || reference, href: href, class: 'gfm')
end
def project_wiki
context[:project_wiki]
def wiki
context[:wiki]
end
def project_wiki_base_path
project_wiki && project_wiki.wiki_base_path
def wiki_base_path
wiki&.wiki_base_path
end
# Ensure that a :project_wiki key exists in context
# Ensure that a :wiki key exists in context
#
# Note that while the key might exist, its value could be nil!
def validate
needs :project_wiki
needs :wiki
end
end
end
......
......@@ -10,7 +10,7 @@ module Banzai
# :commit
# :current_user
# :project
# :project_wiki
# :wiki
# :ref
# :requested_path
# :system_note
......@@ -53,7 +53,7 @@ module Banzai
def linkable_files?
strong_memoize(:linkable_files) do
context[:project_wiki].nil? && repository.try(:exists?) && !repository.empty?
context[:wiki].nil? && repository.try(:exists?) && !repository.empty?
end
end
......
......@@ -6,12 +6,12 @@ module Banzai
# Rewrite rules are documented in the `WikiPipeline` spec.
#
# Context options:
# :project_wiki
# :wiki
class WikiLinkFilter < HTML::Pipeline::Filter
include Gitlab::Utils::SanitizeNodeLink
def call
return doc unless project_wiki?
return doc unless wiki?
doc.search('a:not(.gfm)').each { |el| process_link(el.attribute('href'), el) }
......@@ -33,8 +33,8 @@ module Banzai
remove_unsafe_links({ node: node }, remove_invalid_links: false)
end
def project_wiki?
!context[:project_wiki].nil?
def wiki?
!context[:wiki].nil?
end
def process_link_attr(html_attr)
......@@ -46,7 +46,7 @@ module Banzai
end
def apply_rewrite_rules(link_string)
Rewriter.new(link_string, wiki: context[:project_wiki], slug: context[:page_slug]).apply_rules
Rewriter.new(link_string, wiki: context[:wiki], slug: context[:page_slug]).apply_rules
end
end
end
......
......@@ -157,7 +157,7 @@ describe 'Copy as GFM', :js do
GFM
pipeline: :wiki,
project_wiki: @project.wiki
wiki: @project.wiki
)
verify(
......
......@@ -269,15 +269,15 @@ describe 'GitLab Markdown', :aggregate_failures do
context 'wiki pipeline' do
before do
@project_wiki = @feat.project_wiki
@project_wiki_page = @feat.project_wiki_page
@wiki = @feat.wiki
@wiki_page = @feat.wiki_page
path = 'images/example.jpg'
gitaly_wiki_file = Gitlab::GitalyClient::WikiFile.new(path: path)
expect(@project_wiki).to receive(:find_file).with(path).and_return(Gitlab::Git::WikiFile.new(gitaly_wiki_file))
allow(@project_wiki).to receive(:wiki_base_path) { '/namespace1/gitlabhq/wikis' }
expect(@wiki).to receive(:find_file).with(path).and_return(Gitlab::Git::WikiFile.new(gitaly_wiki_file))
allow(@wiki).to receive(:wiki_base_path) { '/namespace1/gitlabhq/wikis' }
@html = markdown(@feat.raw_markdown, { pipeline: :wiki, project_wiki: @project_wiki, page_slug: @project_wiki_page.slug })
@html = markdown(@feat.raw_markdown, { pipeline: :wiki, wiki: @wiki, page_slug: @wiki_page.slug })
end
it_behaves_like 'all pipelines'
......
......@@ -317,7 +317,7 @@ describe MarkupHelper do
let(:wiki_repository) { double('Repository') }
let(:context) do
{
pipeline: :wiki, project: project, project_wiki: wiki,
pipeline: :wiki, project: project, wiki: wiki,
page_slug: 'nested/page', issuable_state_filter_enabled: true,
repository: wiki_repository
}
......
......@@ -7,11 +7,11 @@ describe Banzai::Filter::GollumTagsFilter do
let(:project) { create(:project) }
let(:user) { double }
let(:project_wiki) { ProjectWiki.new(project, user) }
let(:wiki) { ProjectWiki.new(project, user) }
describe 'validation' do
it 'ensure that a :project_wiki key exists in context' do
expect { filter("See [[images/image.jpg]]", {}) }.to raise_error ArgumentError, "Missing context keys for Banzai::Filter::GollumTagsFilter: :project_wiki"
it 'ensure that a :wiki key exists in context' do
expect { filter("See [[images/image.jpg]]", {}) }.to raise_error ArgumentError, "Missing context keys for Banzai::Filter::GollumTagsFilter: :wiki"
end
end
......@@ -23,19 +23,19 @@ describe Banzai::Filter::GollumTagsFilter do
path: 'images/image.jpg',
raw_data: '')
wiki_file = Gitlab::Git::WikiFile.new(gollum_file_double)
expect(project_wiki).to receive(:find_file).with('images/image.jpg').and_return(wiki_file)
expect(wiki).to receive(:find_file).with('images/image.jpg').and_return(wiki_file)
tag = '[[images/image.jpg]]'
doc = filter("See #{tag}", project_wiki: project_wiki)
doc = filter("See #{tag}", wiki: wiki)
expect(doc.at_css('img')['data-src']).to eq "#{project_wiki.wiki_base_path}/images/image.jpg"
expect(doc.at_css('img')['data-src']).to eq "#{wiki.wiki_base_path}/images/image.jpg"
end
it 'does not creates img tag if image does not exist' do
expect(project_wiki).to receive(:find_file).with('images/image.jpg').and_return(nil)
expect(wiki).to receive(:find_file).with('images/image.jpg').and_return(nil)
tag = '[[images/image.jpg]]'
doc = filter("See #{tag}", project_wiki: project_wiki)
doc = filter("See #{tag}", wiki: wiki)
expect(doc.css('img').size).to eq 0
end
......@@ -44,14 +44,14 @@ describe Banzai::Filter::GollumTagsFilter do
context 'linking external images' do
it 'creates img tag for valid URL' do
tag = '[[http://example.com/image.jpg]]'
doc = filter("See #{tag}", project_wiki: project_wiki)
doc = filter("See #{tag}", wiki: wiki)
expect(doc.at_css('img')['data-src']).to eq "http://example.com/image.jpg"
end
it 'does not creates img tag for invalid URL' do
tag = '[[http://example.com/image.pdf]]'
doc = filter("See #{tag}", project_wiki: project_wiki)
doc = filter("See #{tag}", wiki: wiki)
expect(doc.css('img').size).to eq 0
end
......@@ -60,7 +60,7 @@ describe Banzai::Filter::GollumTagsFilter do
context 'linking external resources' do
it "the created link's text will be equal to the resource's text" do
tag = '[[http://example.com]]'
doc = filter("See #{tag}", project_wiki: project_wiki)
doc = filter("See #{tag}", wiki: wiki)
expect(doc.at_css('a').text).to eq 'http://example.com'
expect(doc.at_css('a')['href']).to eq 'http://example.com'
......@@ -68,7 +68,7 @@ describe Banzai::Filter::GollumTagsFilter do
it "the created link's text will be link-text" do
tag = '[[link-text|http://example.com/pdfs/gollum.pdf]]'
doc = filter("See #{tag}", project_wiki: project_wiki)
doc = filter("See #{tag}", wiki: wiki)
expect(doc.at_css('a').text).to eq 'link-text'
expect(doc.at_css('a')['href']).to eq 'http://example.com/pdfs/gollum.pdf'
......@@ -78,8 +78,8 @@ describe Banzai::Filter::GollumTagsFilter do
context 'linking internal resources' do
it "the created link's text includes the resource's text and wiki base path" do
tag = '[[wiki-slug]]'
doc = filter("See #{tag}", project_wiki: project_wiki)
expected_path = ::File.join(project_wiki.wiki_base_path, 'wiki-slug')
doc = filter("See #{tag}", wiki: wiki)
expected_path = ::File.join(wiki.wiki_base_path, 'wiki-slug')
expect(doc.at_css('a').text).to eq 'wiki-slug'
expect(doc.at_css('a')['href']).to eq expected_path
......@@ -87,15 +87,15 @@ describe Banzai::Filter::GollumTagsFilter do
it "the created link's text will be link-text" do
tag = '[[link-text|wiki-slug]]'
doc = filter("See #{tag}", project_wiki: project_wiki)
expected_path = ::File.join(project_wiki.wiki_base_path, 'wiki-slug')
doc = filter("See #{tag}", wiki: wiki)
expected_path = ::File.join(wiki.wiki_base_path, 'wiki-slug')
expect(doc.at_css('a').text).to eq 'link-text'
expect(doc.at_css('a')['href']).to eq expected_path
end
it "inside back ticks will be exempt from linkification" do
doc = filter('<code>[[link-in-backticks]]</code>', project_wiki: project_wiki)
doc = filter('<code>[[link-in-backticks]]</code>', wiki: wiki)
expect(doc.at_css('code').text).to eq '[[link-in-backticks]]'
end
......
......@@ -12,7 +12,7 @@ describe Banzai::Filter::RepositoryLinkFilter do
project: project,
current_user: user,
group: group,
project_wiki: project_wiki,
wiki: wiki,
ref: ref,
requested_path: requested_path,
only_path: only_path
......@@ -53,7 +53,7 @@ describe Banzai::Filter::RepositoryLinkFilter do
let(:project_path) { project.full_path }
let(:ref) { 'markdown' }
let(:commit) { project.commit(ref) }
let(:project_wiki) { nil }
let(:wiki) { nil }
let(:requested_path) { '/' }
let(:only_path) { true }
......@@ -94,8 +94,8 @@ describe Banzai::Filter::RepositoryLinkFilter do
end
end
context 'with a project_wiki' do
let(:project_wiki) { double('ProjectWiki') }
context 'with a wiki' do
let(:wiki) { double('ProjectWiki') }
include_examples :preserve_unchanged
end
......
......@@ -12,13 +12,13 @@ describe Banzai::Filter::WikiLinkFilter do
let(:repository_upload_folder) { Wikis::CreateAttachmentService::ATTACHMENT_PATH }
it "doesn't rewrite absolute links" do
filtered_link = filter("<a href='http://example.com:8000/'>Link</a>", project_wiki: wiki).children[0]
filtered_link = filter("<a href='http://example.com:8000/'>Link</a>", wiki: wiki).children[0]
expect(filtered_link.attribute('href').value).to eq('http://example.com:8000/')
end
it "doesn't rewrite links to project uploads" do
filtered_link = filter("<a href='/uploads/a.test'>Link</a>", project_wiki: wiki).children[0]
filtered_link = filter("<a href='/uploads/a.test'>Link</a>", wiki: wiki).children[0]
expect(filtered_link.attribute('href').value).to eq('/uploads/a.test')
end
......@@ -26,7 +26,7 @@ describe Banzai::Filter::WikiLinkFilter do
describe "when links point to the #{Wikis::CreateAttachmentService::ATTACHMENT_PATH} folder" do
context 'with an "a" html tag' do
it 'rewrites links' do
filtered_link = filter("<a href='#{repository_upload_folder}/a.test'>Link</a>", project_wiki: wiki).children[0]
filtered_link = filter("<a href='#{repository_upload_folder}/a.test'>Link</a>", wiki: wiki).children[0]
expect(filtered_link.attribute('href').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.test")
end
......@@ -37,7 +37,7 @@ describe Banzai::Filter::WikiLinkFilter do
context 'inside an "a" html tag' do
it 'rewrites links' do
filtered_elements = filter("<a href='#{repository_upload_folder}/a.jpg'><img src='#{repository_upload_folder}/a.jpg'>example</img></a>", project_wiki: wiki)
filtered_elements = filter("<a href='#{repository_upload_folder}/a.jpg'><img src='#{repository_upload_folder}/a.jpg'>example</img></a>", wiki: wiki)
expect(filtered_elements.search('img').first.attribute('src').value).to eq(path)
expect(filtered_elements.search('a').first.attribute('href').value).to eq(path)
......@@ -46,7 +46,7 @@ describe Banzai::Filter::WikiLinkFilter do
context 'outside an "a" html tag' do
it 'rewrites links' do
filtered_link = filter("<img src='#{repository_upload_folder}/a.jpg'>example</img>", project_wiki: wiki).children[0]
filtered_link = filter("<img src='#{repository_upload_folder}/a.jpg'>example</img>", wiki: wiki).children[0]
expect(filtered_link.attribute('src').value).to eq(path)
end
......@@ -55,7 +55,7 @@ describe Banzai::Filter::WikiLinkFilter do
context 'with "video" html tag' do
it 'rewrites links' do
filtered_link = filter("<video src='#{repository_upload_folder}/a.mp4'></video>", project_wiki: wiki).children[0]
filtered_link = filter("<video src='#{repository_upload_folder}/a.mp4'></video>", wiki: wiki).children[0]
expect(filtered_link.attribute('src').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.mp4")
end
......@@ -63,7 +63,7 @@ describe Banzai::Filter::WikiLinkFilter do
context 'with "audio" html tag' do
it 'rewrites links' do
filtered_link = filter("<audio src='#{repository_upload_folder}/a.wav'></audio>", project_wiki: wiki).children[0]
filtered_link = filter("<audio src='#{repository_upload_folder}/a.wav'></audio>", wiki: wiki).children[0]
expect(filtered_link.attribute('src').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.wav")
end
......@@ -75,7 +75,7 @@ describe Banzai::Filter::WikiLinkFilter do
invalid_links.each do |invalid_link|
it "doesn't rewrite invalid invalid_links like #{invalid_link}" do
filtered_link = filter("<a href='#{invalid_link}'>Link</a>", project_wiki: wiki).children[0]
filtered_link = filter("<a href='#{invalid_link}'>Link</a>", wiki: wiki).children[0]
expect(filtered_link.attribute('href').value).to eq(invalid_link)
end
......
......@@ -36,12 +36,12 @@ class MarkdownFeature
end
end
def project_wiki
@project_wiki ||= ProjectWiki.new(project, user)
def wiki
@wiki ||= ProjectWiki.new(project, user)
end
def project_wiki_page
@project_wiki_page ||= build(:wiki_page, wiki: project_wiki)
def wiki_page
@wiki_page ||= build(:wiki_page, wiki: wiki)
end
def issue
......
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