require 'html/pipeline' require 'html/pipeline/gitlab' module Gitlab # Custom parser for GitLab-flavored Markdown # # It replaces references in the text with links to the appropriate items in # GitLab. # # Supported reference formats are: # * @foo for team members # * #123 for issues # * #JIRA-123 for Jira issues # * !123 for merge requests # * $123 for snippets # * 123456 for commits # * 123456...7890123 for commit ranges (comparisons) # # It also parses Emoji codes to insert images. See # http://www.emoji-cheat-sheet.com/ for a list of the supported icons. # # Examples # # >> gfm("Hey @david, can you fix this?") # => "Hey @david, can you fix this?" # # >> gfm("Commit 35d5f7c closes #1234") # => "Commit 35d5f7c closes #1234" # # >> gfm(":trollface:") # => "\":trollface:\" module Markdown include IssuesHelper attr_reader :html_options # Public: Parse the provided text with GitLab-Flavored Markdown # # text - the source text # project - extra options for the reference links as given to link_to # html_options - extra options for the reference links as given to link_to def gfm(text, project = @project, html_options = {}) gfm_with_options(text, {}, project, html_options) end # Public: Parse the provided text with GitLab-Flavored Markdown # # text - the source text # options - parse_tasks: true - render tasks # - xhtml: true - output XHTML instead of HTML # project - extra options for the reference links as given to link_to # html_options - extra options for the reference links as given to link_to def gfm_with_options(text, options = {}, project = @project, html_options = {}) return text if text.nil? # Duplicate the string so we don't alter the original, then call to_str # to cast it back to a String instead of a SafeBuffer. This is required # for gsub calls to work as we need them to. text = text.dup.to_str @html_options = html_options # Extract pre blocks so they are not altered # from http://github.github.com/github-flavored-markdown/ text.gsub!(%r{
.*?
|.*?}m) { |match| extract_piece(match) } # Extract links with probably parsable hrefs text.gsub!(%r{.*?}m) { |match| extract_piece(match) } # Extract images with probably parsable src text.gsub!(%r{}m) { |match| extract_piece(match) } # TODO: add popups with additional information text = parse(text, project) # Insert pre block extractions text.gsub!(/\{gfm-extraction-(\h{32})\}/) do insert_piece($1) end # Used markdown pipelines in GitLab: # GitlabEmojiFilter - performs emoji replacement. # # see https://gitlab.com/gitlab-org/html-pipeline-gitlab for more filters filters = [ HTML::Pipeline::Gitlab::GitlabEmojiFilter ] markdown_context = { asset_root: Gitlab.config.gitlab.url, asset_host: Gitlab::Application.config.asset_host } markdown_pipeline = HTML::Pipeline::Gitlab.new(filters).pipeline result = markdown_pipeline.call(text, markdown_context) saveoptions = 0 if options[:xhtml] saveoptions |= Nokogiri::XML::Node::SaveOptions::AS_XHTML end text = result[:output].to_html(save_with: saveoptions) allowed_attributes = ActionView::Base.sanitized_allowed_attributes allowed_tags = ActionView::Base.sanitized_allowed_tags text = sanitize text.html_safe, attributes: allowed_attributes + %w(id class style), tags: allowed_tags + %w(table tr td th) if options[:parse_tasks] text = parse_tasks(text) end text end private def extract_piece(text) @extractions ||= {} md5 = Digest::MD5.hexdigest(text) @extractions[md5] = text "{gfm-extraction-#{md5}}" end def insert_piece(id) @extractions[id] end # Private: Parses text for references and emoji # # text - Text to parse # # Returns parsed text def parse(text, project = @project) parse_references(text, project) if project text end NAME_STR = '[a-zA-Z0-9_][a-zA-Z0-9_\-\.]*' PROJ_STR = "(?#{NAME_STR}/#{NAME_STR})" REFERENCE_PATTERN = %r{ (?\W)? # Prefix ( # Reference @(?#{NAME_STR}) # User name |~(?