summaryrefslogtreecommitdiff
path: root/app/services/preview_markdown_service.rb
diff options
context:
space:
mode:
authorRares Sfirlogea <rrr.junior@gmail.com>2016-11-16 12:09:09 +0100
committerAdam Niedzielski <adamsunday@gmail.com>2017-05-04 17:02:25 +0200
commit45e4c665653cd511b0d96119d3973652c43238bb (patch)
treeb614bb895b59dfb2f27d1408d0ed7594c4182c73 /app/services/preview_markdown_service.rb
parent2d43f8a2f4b1352067755609c9e3110d382d06c6 (diff)
downloadgitlab-ce-45e4c665653cd511b0d96119d3973652c43238bb.tar.gz
Display slash commands outcome when previewing Markdownadam-separate-slash-commands
Remove slash commands from Markdown preview and display their outcome next to the text field. Introduce new "explanation" block to our slash commands DSL. Introduce optional "parse_params" block to slash commands DSL that allows to process a parameter before it is passed to "explanation" or "command" blocks. Pass path for previewing Markdown as "data" attribute instead of setting a variable on "window".
Diffstat (limited to 'app/services/preview_markdown_service.rb')
-rw-r--r--app/services/preview_markdown_service.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/app/services/preview_markdown_service.rb b/app/services/preview_markdown_service.rb
new file mode 100644
index 00000000000..10d45bbf73c
--- /dev/null
+++ b/app/services/preview_markdown_service.rb
@@ -0,0 +1,45 @@
+class PreviewMarkdownService < BaseService
+ def execute
+ text, commands = explain_slash_commands(params[:text])
+ users = find_user_references(text)
+
+ success(
+ text: text,
+ users: users,
+ commands: commands.join(' ')
+ )
+ end
+
+ private
+
+ def explain_slash_commands(text)
+ return text, [] unless %w(Issue MergeRequest).include?(commands_target_type)
+
+ slash_commands_service = SlashCommands::InterpretService.new(project, current_user)
+ slash_commands_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_commands_target
+ if commands_target_id.present?
+ finder = commands_target_type == 'Issue' ? IssuesFinder : MergeRequestsFinder
+ finder.new(current_user, project_id: project.id).find(commands_target_id)
+ else
+ collection = commands_target_type == 'Issue' ? project.issues : project.merge_requests
+ collection.build
+ end
+ end
+
+ def commands_target_type
+ params[:slash_commands_target_type]
+ end
+
+ def commands_target_id
+ params[:slash_commands_target_id]
+ end
+end