Welcome to mirror list, hosted at ThFree Co, Russian Federation.

preview_markdown_service.rb « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a449a5dc3e9833a1a59e0e161728ec93024b7f6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

class PreviewMarkdownService < BaseService
  def execute
    text, commands = explain_quick_actions(params[:text])
    users = find_user_references(text)
    suggestions = find_suggestions(text)

    success(
      text: text,
      users: users,
      suggestions: suggestions,
      commands: commands.join(' '),
      markdown_engine: markdown_engine
    )
  end

  private

  def explain_quick_actions(text)
    return text, [] unless %w(Issue MergeRequest Commit).include?(commands_target_type)

    quick_actions_service = QuickActions::InterpretService.new(project, current_user)
    quick_actions_service.explain(text, find_commands_target)
  end

  def find_user_references(text)
    extractor = Gitlab::ReferenceExtractor.new(project, current_user)
    extractor.analyze(text, author: current_user)
    extractor.users.map(&:username)
  end

  def find_suggestions(text)
    return [] unless params[:preview_suggestions]

    Banzai::SuggestionsParser.parse(text)
  end

  def find_commands_target
    QuickActions::TargetService
      .new(project, current_user)
      .execute(commands_target_type, commands_target_id)
  end

  def commands_target_type
    params[:quick_actions_target_type]
  end

  def commands_target_id
    params[:quick_actions_target_id]
  end

  def markdown_engine
    if params[:legacy_render]
      :redcarpet
    else
      CacheMarkdownField::MarkdownEngine.from_version(params[:markdown_version].to_i)
    end
  end
end