Commit 8fbda3bf authored by Sean McGivern's avatar Sean McGivern

Merge branch '12910-personal-snippet-prep-2' into 'master'

Support preview_markdown action for personal_snippets

See merge request !10810
parents 8c687299 87327c58
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
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."
......
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
......
......@@ -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
......
......@@ -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)),
......
---
title: Support Markdown previews for personal snippets
merge_request: 10810
author:
......@@ -3,6 +3,7 @@ resources :snippets, concerns: :awardable do
get 'raw'
get 'download'
post :mark_as_spam
post :preview_markdown
end
end
......
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
......@@ -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
......@@ -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
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