diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2017-04-26 12:36:25 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2017-04-26 12:36:25 +0000 |
commit | 8fbda3bf152b6b15f30754cbc68c7466e86b8ee9 (patch) | |
tree | ff32284427ba72a335f6e6057a79c4652c43bd43 | |
parent | 8c68729964d709ef8c372e41614a742cea8b6633 (diff) | |
parent | 87327c5845a7feec92e546dc0da42282ac27de40 (diff) | |
download | gitlab-ce-8fbda3bf152b6b15f30754cbc68c7466e86b8ee9.tar.gz |
Merge branch '12910-personal-snippet-prep-2' into 'master'
Support preview_markdown action for personal_snippets
See merge request !10810
-rw-r--r-- | app/controllers/concerns/markdown_preview.rb | 19 | ||||
-rw-r--r-- | app/controllers/projects/wikis_controller.rb | 21 | ||||
-rw-r--r-- | app/controllers/projects_controller.rb | 19 | ||||
-rw-r--r-- | app/controllers/snippets_controller.rb | 5 | ||||
-rw-r--r-- | app/helpers/gitlab_markdown_helper.rb | 2 | ||||
-rw-r--r-- | changelogs/unreleased/12910-personal-snippet-prep-2.yml | 4 | ||||
-rw-r--r-- | config/routes/snippets.rb | 1 | ||||
-rw-r--r-- | spec/controllers/projects/wikis_controller_spec.rb | 16 | ||||
-rw-r--r-- | spec/controllers/projects_controller_spec.rb | 10 | ||||
-rw-r--r-- | spec/controllers/snippets_controller_spec.rb | 12 |
10 files changed, 80 insertions, 29 deletions
diff --git a/app/controllers/concerns/markdown_preview.rb b/app/controllers/concerns/markdown_preview.rb new file mode 100644 index 00000000000..40eff267348 --- /dev/null +++ b/app/controllers/concerns/markdown_preview.rb @@ -0,0 +1,19 @@ +module MarkdownPreview + private + + def render_markdown_preview(text, markdown_context = {}) + render json: { + body: view_context.markdown(text, markdown_context), + references: { + users: preview_referenced_users(text) + } + } + end + + def preview_referenced_users(text) + extractor = Gitlab::ReferenceExtractor.new(@project, current_user) + extractor.analyze(text, author: current_user) + + extractor.users.map(&:username) + end +end diff --git a/app/controllers/projects/wikis_controller.rb b/app/controllers/projects/wikis_controller.rb index c5e24b9e365..96125684da0 100644 --- a/app/controllers/projects/wikis_controller.rb +++ b/app/controllers/projects/wikis_controller.rb @@ -1,4 +1,6 @@ class Projects::WikisController < Projects::ApplicationController + include MarkdownPreview + before_action :authorize_read_wiki! before_action :authorize_create_wiki!, only: [:edit, :create, :history] before_action :authorize_admin_wiki!, only: :destroy @@ -91,21 +93,13 @@ class Projects::WikisController < Projects::ApplicationController ) end - def preview_markdown - text = params[:text] - - ext = Gitlab::ReferenceExtractor.new(@project, current_user) - ext.analyze(text, author: current_user) - - render json: { - body: view_context.markdown(text, pipeline: :wiki, project_wiki: @project_wiki, page_slug: params[:id]), - references: { - users: ext.users.map(&:username) - } - } + def git_access end - def git_access + def preview_markdown + context = { pipeline: :wiki, project_wiki: @project_wiki, page_slug: params[:id] } + + render_markdown_preview(params[:text], context) end private @@ -115,7 +109,6 @@ class Projects::WikisController < Projects::ApplicationController # Call #wiki to make sure the Wiki Repo is initialized @project_wiki.wiki - @sidebar_wiki_entries = WikiPage.group_by_directory(@project_wiki.pages.first(15)) rescue ProjectWiki::CouldNotCreateWikiError flash[:notice] = "Could not create Wiki Repository at this time. Please try again later." diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 6807c37f972..9f6ee4826e6 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,6 +1,7 @@ class ProjectsController < Projects::ApplicationController include IssuableCollections include ExtractsPath + include MarkdownPreview before_action :authenticate_user!, except: [:index, :show, :activity, :refs] before_action :project, except: [:index, :new, :create] @@ -216,20 +217,6 @@ class ProjectsController < Projects::ApplicationController } end - def preview_markdown - text = params[:text] - - ext = Gitlab::ReferenceExtractor.new(@project, current_user) - ext.analyze(text, author: current_user) - - render json: { - body: view_context.markdown(text), - references: { - users: ext.users.map(&:username) - } - } - end - def refs branches = BranchesFinder.new(@repository, params).execute.map(&:name) @@ -252,6 +239,10 @@ class ProjectsController < Projects::ApplicationController render json: options.to_json end + def preview_markdown + render_markdown_preview(params[:text]) + end + private # Render project landing depending of which features are available diff --git a/app/controllers/snippets_controller.rb b/app/controllers/snippets_controller.rb index f3fd3da8b20..056910fad67 100644 --- a/app/controllers/snippets_controller.rb +++ b/app/controllers/snippets_controller.rb @@ -2,6 +2,7 @@ class SnippetsController < ApplicationController include ToggleAwardEmoji include SpammableActions include SnippetsActions + include MarkdownPreview before_action :snippet, only: [:show, :edit, :destroy, :update, :raw, :download] @@ -77,6 +78,10 @@ class SnippetsController < ApplicationController ) end + def preview_markdown + render_markdown_preview(params[:text], skip_project_check: true) + end + protected def snippet diff --git a/app/helpers/gitlab_markdown_helper.rb b/app/helpers/gitlab_markdown_helper.rb index cd442237086..106feb87398 100644 --- a/app/helpers/gitlab_markdown_helper.rb +++ b/app/helpers/gitlab_markdown_helper.rb @@ -196,7 +196,7 @@ module GitlabMarkdownHelper end # Calls Banzai.post_process with some common context options - def banzai_postprocess(html, context) + def banzai_postprocess(html, context = {}) context.merge!( current_user: (current_user if defined?(current_user)), diff --git a/changelogs/unreleased/12910-personal-snippet-prep-2.yml b/changelogs/unreleased/12910-personal-snippet-prep-2.yml new file mode 100644 index 00000000000..bd9527c30c8 --- /dev/null +++ b/changelogs/unreleased/12910-personal-snippet-prep-2.yml @@ -0,0 +1,4 @@ +--- +title: Support Markdown previews for personal snippets +merge_request: 10810 +author: diff --git a/config/routes/snippets.rb b/config/routes/snippets.rb index ce0d1314292..56534f677be 100644 --- a/config/routes/snippets.rb +++ b/config/routes/snippets.rb @@ -3,6 +3,7 @@ resources :snippets, concerns: :awardable do get 'raw' get 'download' post :mark_as_spam + post :preview_markdown end end diff --git a/spec/controllers/projects/wikis_controller_spec.rb b/spec/controllers/projects/wikis_controller_spec.rb new file mode 100644 index 00000000000..92addf30307 --- /dev/null +++ b/spec/controllers/projects/wikis_controller_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Projects::WikisController do + let(:project) { create(:project_empty_repo, :public) } + let(:user) { create(:user) } + + describe 'POST #preview_markdown' do + it 'renders json in a correct format' do + sign_in(user) + + post :preview_markdown, namespace_id: project.namespace, project_id: project, id: 'page/path', text: '*Markdown* text' + + expect(JSON.parse(response.body).keys).to match_array(%w(body references)) + end + end +end diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb index a88ffc1ea6a..eafc2154568 100644 --- a/spec/controllers/projects_controller_spec.rb +++ b/spec/controllers/projects_controller_spec.rb @@ -398,4 +398,14 @@ describe ProjectsController do expect(parsed_body["Commits"]).to include("123456") end end + + describe 'POST #preview_markdown' do + it 'renders json in a correct format' do + sign_in(user) + + post :preview_markdown, namespace_id: public_project.namespace, id: public_project, text: '*Markdown* text' + + expect(JSON.parse(response.body).keys).to match_array(%w(body references)) + end + end end diff --git a/spec/controllers/snippets_controller_spec.rb b/spec/controllers/snippets_controller_spec.rb index 5de3b9890ef..234f3edd3d8 100644 --- a/spec/controllers/snippets_controller_spec.rb +++ b/spec/controllers/snippets_controller_spec.rb @@ -521,4 +521,16 @@ describe SnippetsController do end end end + + describe 'POST #preview_markdown' do + let(:snippet) { create(:personal_snippet, :public) } + + it 'renders json in a correct format' do + sign_in(user) + + post :preview_markdown, id: snippet, text: '*Markdown* text' + + expect(JSON.parse(response.body).keys).to match_array(%w(body references)) + end + end end |