Commit 3b422bcf authored by Aleksei Lipniagov's avatar Aleksei Lipniagov

Merge branch 'fj-refactor-wiki-markups' into 'master'

Refactor Wiki formats handling

See merge request gitlab-org/gitlab!84155
parents ea77de40 d40ceafd
...@@ -139,6 +139,10 @@ module WikiHelper ...@@ -139,6 +139,10 @@ module WikiHelper
api_v4_projects_wikis_path(wiki_page_render_api_endpoint_params(page)) api_v4_projects_wikis_path(wiki_page_render_api_endpoint_params(page))
end end
def wiki_markup_hash_by_name_id
Wiki::VALID_USER_MARKUPS.map { |key, value| { value[:name] => key } }.reduce({}, :merge)
end
private private
def wiki_page_render_api_endpoint_params(page) def wiki_page_render_api_endpoint_params(page)
......
...@@ -10,18 +10,45 @@ class Wiki ...@@ -10,18 +10,45 @@ class Wiki
extend ActiveModel::Naming extend ActiveModel::Naming
MARKUPS = { # rubocop:disable Style/MultilineIfModifier MARKUPS = { # rubocop:disable Style/MultilineIfModifier
'Markdown' => :markdown, markdown: {
'RDoc' => :rdoc, name: 'Markdown',
'AsciiDoc' => :asciidoc, default_extension: :md,
'Org' => :org created_by_user: true
},
rdoc: {
name: 'RDoc',
default_extension: :rdoc,
created_by_user: true
},
asciidoc: {
name: 'AsciiDoc',
default_extension: :asciidoc,
created_by_user: true
},
org: {
name: 'Org',
default_extension: :org,
created_by_user: true
},
textile: {
name: 'Textile',
default_extension: :textile
},
creole: {
name: 'Creole',
default_extension: :creole
},
rest: {
name: 'reStructuredText',
default_extension: :rst
},
mediawiki: {
name: 'MediaWiki',
default_extension: :mediawiki
}
}.freeze unless defined?(MARKUPS) }.freeze unless defined?(MARKUPS)
DEFAULT_MARKUP_EXTENSIONS = { # rubocop:disable Style/MultilineIfModifier VALID_USER_MARKUPS = MARKUPS.select { |_, v| v[:created_by_user] }.freeze unless defined?(VALID_USER_MARKUPS)
markdown: 'md',
rdoc: 'rdoc',
asciidoc: 'asciidoc',
org: 'org'
}.freeze unless defined?(DEFAULT_MARKUP_EXTENSIONS)
CouldNotCreateWikiError = Class.new(StandardError) CouldNotCreateWikiError = Class.new(StandardError)
...@@ -355,13 +382,15 @@ class Wiki ...@@ -355,13 +382,15 @@ class Wiki
end end
def with_valid_format(format, &block) def with_valid_format(format, &block)
unless Wiki::MARKUPS.value?(format.to_sym) default_extension = Wiki::VALID_USER_MARKUPS.dig(format.to_sym, :default_extension).to_s
if default_extension.blank?
@error_message = _('Invalid format selected') @error_message = _('Invalid format selected')
return false return false
end end
yield Wiki::DEFAULT_MARKUP_EXTENSIONS[format.to_sym] yield default_extension
end end
def sluggified_full_path(title, extension) def sluggified_full_path(title, extension)
......
...@@ -185,7 +185,7 @@ class WikiPage ...@@ -185,7 +185,7 @@ class WikiPage
# :content - The raw markup content. # :content - The raw markup content.
# :format - Optional symbol representing the # :format - Optional symbol representing the
# content format. Can be any type # content format. Can be any type
# listed in the Wiki::MARKUPS # listed in the Wiki::VALID_USER_MARKUPS
# Hash. # Hash.
# :message - Optional commit message to set on # :message - Optional commit message to set on
# the new page. # the new page.
...@@ -205,7 +205,7 @@ class WikiPage ...@@ -205,7 +205,7 @@ class WikiPage
# attrs - Hash of attributes to be updated on the page. # attrs - Hash of attributes to be updated on the page.
# :content - The raw markup content to replace the existing. # :content - The raw markup content to replace the existing.
# :format - Optional symbol representing the content format. # :format - Optional symbol representing the content format.
# See Wiki::MARKUPS Hash for available formats. # See Wiki::VALID_USER_MARKUPS Hash for available formats.
# :message - Optional commit message to set on the new version. # :message - Optional commit message to set on the new version.
# :last_commit_sha - Optional last commit sha to validate the page unchanged. # :last_commit_sha - Optional last commit sha to validate the page unchanged.
# :title - The Title (optionally including dir) to replace existing title # :title - The Title (optionally including dir) to replace existing title
......
...@@ -3,4 +3,4 @@ ...@@ -3,4 +3,4 @@
.gl-mt-3 .gl-mt-3
= form_errors(@page, truncate: :title) = form_errors(@page, truncate: :title)
#js-wiki-form{ data: { page_info: page_info.to_json, format_options: Wiki::MARKUPS.to_json } } #js-wiki-form{ data: { page_info: page_info.to_json, format_options: wiki_markup_hash_by_name_id.to_json } }
...@@ -12,7 +12,7 @@ module API ...@@ -12,7 +12,7 @@ module API
params :common_wiki_page_params do params :common_wiki_page_params do
optional :format, optional :format,
type: String, type: String,
values: Wiki::MARKUPS.values.map(&:to_s), values: Wiki::VALID_USER_MARKUPS.keys.map(&:to_s),
default: 'markdown', default: 'markdown',
desc: 'Format of a wiki page. Available formats are markdown, rdoc, asciidoc and org' desc: 'Format of a wiki page. Available formats are markdown, rdoc, asciidoc and org'
end end
......
...@@ -20,6 +20,12 @@ RSpec.shared_examples 'User creates wiki page' do ...@@ -20,6 +20,12 @@ RSpec.shared_examples 'User creates wiki page' do
click_link "Create your first page" click_link "Create your first page"
end end
it 'shows all available formats in the dropdown' do
Wiki::VALID_USER_MARKUPS.each do |key, markup|
expect(page).to have_css("#wiki_format option[value=#{key}]", text: markup[:name])
end
end
it "disables the submit button", :js do it "disables the submit button", :js do
page.within(".wiki-form") do page.within(".wiki-form") do
fill_in(:wiki_content, with: "") fill_in(:wiki_content, with: "")
......
...@@ -11,6 +11,10 @@ RSpec.shared_examples 'wiki model' do ...@@ -11,6 +11,10 @@ RSpec.shared_examples 'wiki model' do
subject { wiki } subject { wiki }
it 'VALID_USER_MARKUPS contains all valid markups' do
expect(described_class::VALID_USER_MARKUPS.keys).to match_array(%i(markdown rdoc asciidoc org))
end
it 'container class includes HasWiki' do it 'container class includes HasWiki' do
# NOTE: This is not enforced at runtime, since we also need to support Geo::DeletedProject # NOTE: This is not enforced at runtime, since we also need to support Geo::DeletedProject
expect(wiki_container).to be_kind_of(HasWiki) expect(wiki_container).to be_kind_of(HasWiki)
...@@ -520,6 +524,15 @@ RSpec.shared_examples 'wiki model' do ...@@ -520,6 +524,15 @@ RSpec.shared_examples 'wiki model' do
end end
end end
context 'when format is not allowed' do
let!(:page) { create(:wiki_page, wiki: subject, title: 'test page') }
it 'returns false and sets error message' do
expect(subject.update_page(page.page, content: 'new content', format: :creole)).to eq false
expect(subject.error_message).to match(/Invalid format selected/)
end
end
context 'when page path does not have a default extension' do context 'when page path does not have a default extension' do
let!(:page) { create(:wiki_page, wiki: subject, title: 'test page') } let!(:page) { create(:wiki_page, wiki: subject, title: 'test page') }
......
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