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
api_v4_projects_wikis_path(wiki_page_render_api_endpoint_params(page))
end
def wiki_markup_hash_by_name_id
Wiki::VALID_USER_MARKUPS.map { |key, value| { value[:name] => key } }.reduce({}, :merge)
end
private
def wiki_page_render_api_endpoint_params(page)
......
......@@ -10,18 +10,45 @@ class Wiki
extend ActiveModel::Naming
MARKUPS = { # rubocop:disable Style/MultilineIfModifier
'Markdown' => :markdown,
'RDoc' => :rdoc,
'AsciiDoc' => :asciidoc,
'Org' => :org
markdown: {
name: 'Markdown',
default_extension: :md,
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)
DEFAULT_MARKUP_EXTENSIONS = { # rubocop:disable Style/MultilineIfModifier
markdown: 'md',
rdoc: 'rdoc',
asciidoc: 'asciidoc',
org: 'org'
}.freeze unless defined?(DEFAULT_MARKUP_EXTENSIONS)
VALID_USER_MARKUPS = MARKUPS.select { |_, v| v[:created_by_user] }.freeze unless defined?(VALID_USER_MARKUPS)
CouldNotCreateWikiError = Class.new(StandardError)
......@@ -355,13 +382,15 @@ class Wiki
end
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')
return false
end
yield Wiki::DEFAULT_MARKUP_EXTENSIONS[format.to_sym]
yield default_extension
end
def sluggified_full_path(title, extension)
......
......@@ -185,7 +185,7 @@ class WikiPage
# :content - The raw markup content.
# :format - Optional symbol representing the
# content format. Can be any type
# listed in the Wiki::MARKUPS
# listed in the Wiki::VALID_USER_MARKUPS
# Hash.
# :message - Optional commit message to set on
# the new page.
......@@ -205,7 +205,7 @@ class WikiPage
# attrs - Hash of attributes to be updated on the page.
# :content - The raw markup content to replace the existing.
# :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.
# :last_commit_sha - Optional last commit sha to validate the page unchanged.
# :title - The Title (optionally including dir) to replace existing title
......
......@@ -3,4 +3,4 @@
.gl-mt-3
= 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
params :common_wiki_page_params do
optional :format,
type: String,
values: Wiki::MARKUPS.values.map(&:to_s),
values: Wiki::VALID_USER_MARKUPS.keys.map(&:to_s),
default: 'markdown',
desc: 'Format of a wiki page. Available formats are markdown, rdoc, asciidoc and org'
end
......
......@@ -20,6 +20,12 @@ RSpec.shared_examples 'User creates wiki page' do
click_link "Create your first page"
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
page.within(".wiki-form") do
fill_in(:wiki_content, with: "")
......
......@@ -11,6 +11,10 @@ RSpec.shared_examples 'wiki model' do
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
# NOTE: This is not enforced at runtime, since we also need to support Geo::DeletedProject
expect(wiki_container).to be_kind_of(HasWiki)
......@@ -520,6 +524,15 @@ RSpec.shared_examples 'wiki model' do
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
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