preview_markdown.rb 1.07 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
module PreviewMarkdown
  extend ActiveSupport::Concern

6
  # rubocop:disable Gitlab/ModuleWithInstanceVariables
7 8 9 10 11 12 13
  def preview_markdown
    result = PreviewMarkdownService.new(@project, current_user, params).execute

    markdown_params =
      case controller_name
      when 'wikis'    then { pipeline: :wiki, project_wiki: @project_wiki, page_slug: params[:id] }
      when 'snippets' then { skip_project_check: true }
14
      when 'groups'   then { group: group }
15
      when 'projects' then projects_filter_params
16 17 18 19 20 21 22
      else {}
      end

    render json: {
      body: view_context.markdown(result[:text], markdown_params),
      references: {
        users: result[:users],
Oswaldo Ferreira's avatar
Oswaldo Ferreira committed
23
        suggestions: SuggestionSerializer.new.represent_diff(result[:suggestions]),
24 25 26 27
        commands: view_context.markdown(result[:commands])
      }
    }
  end
28 29 30 31 32 33 34

  def projects_filter_params
    {
      issuable_state_filter_enabled: true,
      suggestions_filter_enabled: params[:preview_suggestions].present?
    }
  end
35
  # rubocop:enable Gitlab/ModuleWithInstanceVariables
36
end