diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2018-01-03 23:23:39 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2018-01-03 23:23:39 +0800 |
commit | d790070d1d316fce863116be024ecab8385c8ad3 (patch) | |
tree | e62ad3aaa299e109a15876e0838a916b662d1986 | |
parent | 83d460bd90da1f4f0121a531b5e96086002e31e1 (diff) | |
download | gitlab-ce-1193-fix-wiki-editing-preview.tar.gz |
WIP, making it work again1193-fix-wiki-editing-preview
-rw-r--r-- | app/controllers/concerns/preview_markdown.rb | 14 | ||||
-rw-r--r-- | config/routes/wiki.rb | 1 | ||||
-rw-r--r-- | spec/features/projects/wiki/markdown_preview_spec.rb | 115 | ||||
-rw-r--r-- | spec/features/projects/wiki/preview_spec.rb | 108 |
4 files changed, 101 insertions, 137 deletions
diff --git a/app/controllers/concerns/preview_markdown.rb b/app/controllers/concerns/preview_markdown.rb index 90bb7a87b45..dfd50b1a0d7 100644 --- a/app/controllers/concerns/preview_markdown.rb +++ b/app/controllers/concerns/preview_markdown.rb @@ -13,8 +13,20 @@ module PreviewMarkdown else {} end + format = params[:format] + + formatted_text = + case format + when 'rdoc' + view_context.rdoc(result[:text], markdown_params) + when 'asciidoc' + view_context.asciidoc(result[:text], markdown_params) + else + view_context.markdown(result[:text], markdown_params) + end + render json: { - body: view_context.markdown(result[:text], markdown_params), + body: formatted_text, references: { users: result[:users], commands: view_context.markdown(result[:commands]) diff --git a/config/routes/wiki.rb b/config/routes/wiki.rb index 6f5033f5c3d..c2da84ff6f2 100644 --- a/config/routes/wiki.rb +++ b/config/routes/wiki.rb @@ -9,6 +9,7 @@ scope(controller: :wikis) do scope(path: 'wikis/*id', as: :wiki, format: false) do get :edit get :history + post :preview_markdown get '/', action: :show put '/', action: :update delete '/', action: :destroy diff --git a/spec/features/projects/wiki/markdown_preview_spec.rb b/spec/features/projects/wiki/markdown_preview_spec.rb index 006c15d60c5..356fe73269d 100644 --- a/spec/features/projects/wiki/markdown_preview_spec.rb +++ b/spec/features/projects/wiki/markdown_preview_spec.rb @@ -3,14 +3,29 @@ require 'spec_helper' feature 'Projects > Wiki > User previews markdown changes', :js do let(:user) { create(:user) } let(:project) { create(:project, namespace: user.namespace) } - let(:wiki_content) do - <<-HEREDOC -[regular link](regular) -[relative link 1](../relative) -[relative link 2](./relative) -[relative link 3](./e/f/relative) - HEREDOC - end + + # sample wiki contents for testing + wiki_supported_formats = { + 'Markdown' => IO.read(Rails.root.join("spec", "fixtures", "sample.md")), + 'AsciiDoc' => IO.read(Rails.root.join("spec", "fixtures", "sample.adoc")), + 'RDoc' => IO.read(Rails.root.join("spec", "fixtures", "sample.rdoc")) + } + + # wiki page path types + wiki_slug_types = { + 'no spaces or hyphens' => { + path: 'a/b/c/d', + expectations: [:expect_norm_relative_links] + }, + 'spaces' => { + path: 'a page/b page/c page/d page', + expectations: [:expect_hyphened_relative_links] + }, + 'hyphens' => { + path: 'a-page/b-page/c-page/d-page', + expectations: [:expect_hyphened_relative_links] + } + } background do project.add_master(user) @@ -31,7 +46,7 @@ feature 'Projects > Wiki > User previews markdown changes', :js do end page.within '.wiki-form' do - fill_in :wiki_content, with: wiki_content + fill_in :wiki_content, with: wiki_supported_formats['Markdown'] click_on "Preview" end @@ -53,7 +68,7 @@ feature 'Projects > Wiki > User previews markdown changes', :js do end page.within '.wiki-form' do - fill_in :wiki_content, with: wiki_content + fill_in :wiki_content, with: wiki_supported_formats['Markdown'] click_on "Preview" end @@ -75,7 +90,7 @@ feature 'Projects > Wiki > User previews markdown changes', :js do end page.within '.wiki-form' do - fill_in :wiki_content, with: wiki_content + fill_in :wiki_content, with: wiki_supported_formats['Markdown'] click_on "Preview" end @@ -90,26 +105,12 @@ feature 'Projects > Wiki > User previews markdown changes', :js do end context "while editing a wiki page" do - def create_wiki_page(path) - find('.add-new-wiki').click - - page.within '#modal-new-wiki' do - fill_in :new_wiki_path, with: path - click_button 'Create page' - end - - page.within '.wiki-form' do - fill_in :wiki_content, with: 'content' - click_on "Create page" - end - end - context "when there are no spaces or hyphens in the page name" do it "rewrites relative links as expected" do create_wiki_page 'a/b/c/d' click_link 'Edit' - fill_in :wiki_content, with: wiki_content + fill_in :wiki_content, with: wiki_supported_formats['Markdown'] click_on "Preview" expect(page).to have_content("regular link") @@ -126,7 +127,7 @@ feature 'Projects > Wiki > User previews markdown changes', :js do create_wiki_page 'a page/b page/c page/d page' click_link 'Edit' - fill_in :wiki_content, with: wiki_content + fill_in :wiki_content, with: wiki_supported_formats['Markdown'] click_on "Preview" expect(page).to have_content("regular link") @@ -143,7 +144,7 @@ feature 'Projects > Wiki > User previews markdown changes', :js do create_wiki_page 'a-page/b-page/c-page/d-page' click_link 'Edit' - fill_in :wiki_content, with: wiki_content + fill_in :wiki_content, with: wiki_supported_formats['Markdown'] click_on "Preview" expect(page).to have_content("regular link") @@ -155,4 +156,62 @@ feature 'Projects > Wiki > User previews markdown changes', :js do end end end + + shared_examples 'using wiki' do |edit_wiki| + # tests all supported formats + wiki_supported_formats.each do |w_format, w_content| + context "user selects #{w_format}" do + wiki_slug_types.each do |slug_type, slug_data| + context "when there are #{slug_type} in the page name" do + it 'renders html as expected' do + create_wiki_page(slug_data[:path], w_format, w_content, edit_wiki) + click_on 'Preview' + + expect_common + + slug_data[:expectations].each do |expectation| + method(expectation).call + end + end + end + end + end + end + end + + context 'when creating a new wiki page' do + it_behaves_like 'using wiki', false + end + + context 'when editing a wiki page' do + it_behaves_like 'using wiki', true + end + + def create_wiki_page(path, wiki_format = 'Markdown', content = 'content', edit = false) + find('.add-new-wiki').click + + page.within '#modal-new-wiki' do + fill_in :new_wiki_path, with: path + click_button 'Create page' + end + + page.within '.wiki-form' do + if edit + fill_in :wiki_content, with: 'content' + else + fill_in :wiki_content, with: content + end + + select(wiki_format, from: 'wiki_format') + + click_on "Create page" + end + + if edit + find('[name=commit]').click + click_link 'Edit' + fill_in :wiki_content, with: content + select(wiki_format, from: 'wiki_format') + end + end end diff --git a/spec/features/projects/wiki/preview_spec.rb b/spec/features/projects/wiki/preview_spec.rb deleted file mode 100644 index 4bfe97f0a81..00000000000 --- a/spec/features/projects/wiki/preview_spec.rb +++ /dev/null @@ -1,108 +0,0 @@ -require 'spec_helper' - -feature 'Projects > Wiki > User previews changes', feature: true, js: true do - let(:user) { create(:user) } - let(:project) { create(:project, namespace: user.namespace) } - - # sample wiki contents for testing - wiki_supported_formats = { - 'Markdown' => IO.read(Rails.root.join("spec", "fixtures", "sample.md")), - 'AsciiDoc' => IO.read(Rails.root.join("spec", "fixtures", "sample.adoc")), - 'RDoc' => IO.read(Rails.root.join("spec", "fixtures", "sample.rdoc")) - } - - # wiki page path types - wiki_slug_types = { - 'no spaces or hyphens' => { - path: 'a/b/c/d', - expectations: [:expect_norm_relative_links] - }, - 'spaces' => { - path: 'a page/b page/c page/d page', - expectations: [:expect_hyphened_relative_links] - }, - 'hyphens' => { - path: 'a-page/b-page/c-page/d-page', - expectations: [:expect_hyphened_relative_links] - }, - } - - def init_wiki_page(path, wiki_format, content, edit = false) - click_link 'New Page' - - fill_in :new_wiki_path, with: path - click_button 'Create Page' - - if edit - fill_in :wiki_content, with: 'content' - else - fill_in :wiki_content, with: content - end - - select(wiki_format, from: 'wiki_format') - - if edit - find('[name=commit]').click - click_link 'Edit' - fill_in :wiki_content, with: content - select(wiki_format, from: 'wiki_format') - end - click_on 'Preview' - end - - def expect_common - expect(page).to have_selector('h1', text: 'My Article') - expect(page).to have_selector('h2', text: 'Software') - expect(page).to have_link('Wikipedia', href: 'http://wikipedia.org') - expect(page).to have_link('regular link', href: "/#{project.path_with_namespace}/wikis/regular") - end - - def expect_hyphened_relative_links - expect(page).to have_link('relative link 1', href: "/#{project.path_with_namespace}/wikis/a-page/b-page/relative") - expect(page).to have_link('relative link 2', href: "/#{project.path_with_namespace}/wikis/a-page/b-page/c-page/relative") - expect(page).to have_link('relative link 3', href: "/#{project.path_with_namespace}/wikis/a-page/b-page/c-page/e/f/relative") - end - - def expect_norm_relative_links - expect(page).to have_link('relative link 1', href: "/#{project.path_with_namespace}/wikis/a/b/relative") - expect(page).to have_link('relative link 2', href: "/#{project.path_with_namespace}/wikis/a/b/c/relative") - expect(page).to have_link('relative link 3', href: "/#{project.path_with_namespace}/wikis/a/b/c/e/f/relative") - end - - background do - project.team << [user, :master] - login_as(user) - - visit namespace_project_path(project.namespace, project) - click_link 'Wiki' - WikiPages::CreateService.new(project, user, title: 'home', content: 'Home page').execute - end - - shared_context 'using wiki' do |edit_wiki| - # tests all supported formats - wiki_supported_formats.each do |w_format, w_content| - context "user selects #{w_format}" do - wiki_slug_types.each do |slug_type, slug_data| - context "when there are #{slug_type} in the page name" do - it 'renders html as expected' do - init_wiki_page(slug_data[:path], w_format, w_content, edit_wiki) - expect_common - - slug_data[:expectations].each do |expectation| - method(expectation).call - end - end - end - end - end - end - end - - context 'when creating a new wiki page' do - include_context 'using wiki', false - end - - context 'when editing a wiki page' do - include_context 'using wiki', true - end -end |