# Module Gitlab::Markdown

- lib/gitlab/markdown.rb

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
    * !123 for merge requests
    * $123 for snippets
    * 123456 for commits

It also parses Emoji codes to insert images. See www.emoji-cheat-sheet.com/ for a list of the supported icons.

Examples

    >> gfm("Hey @david, can you fix this?")
    => "Hey <a href="/gitlab/team_members/1">@david</a>, can you fix this?"

    >> gfm("Commit 35d5f7c closes #1234")
    => "Commit <a href="/gitlab/commits/35d5f7c">35d5f7c</a> closes <a href="/gitlab/issues/1234">#1234</a>"

    >> gfm(":trollface:")
    => "<img alt=\":trollface:\" class=\"emoji\" src=\"/images/trollface.png" title=\":trollface:\" />

Methods

G
  - gfm

Constants

REFERENCE_PATTERN | = | %r{ (?<prefix>\W)? # Prefix ( # Reference @(?<user>[a-zA-Z][a-zA-Z0-9_\-\.]*) # User name |\#(?<issue>\d+) # Issue ID |!(?<merge_request>\d+) # MR ID |\$(?<snippet>\d+) # Snippet ID |(?<commit>[\h]{6,40}) # Commit ID ) (?<suffix>\W)? # Suffix }x.freeze
TYPES | = | [:user, :issue, :merge_request, :snippet, :commit].freeze
EMOJI_PATTERN | = | %r{(:(\S+):)}.freeze

Attributes

[R] | html_options |

Instance Public methods
gfm(text, html_options = {}) Link

Public: Parse the provided text with GitLab-Flavored Markdown

text - the source text #html_options - extra options for the reference links as given to link_to

Note: reference links will only be generated if @project is set

Source: show

    # File lib/gitlab/markdown.rb, line 52
    def gfm(text, 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/
      extractions = {}
      text.gsub!(%r{<pre>.*?</pre>|<code>.*?</code>}) do |match|
        md5 = Digest::MD5.hexdigest(match)
        extractions[md5] = match
        "{gfm-extraction-#{md5}}"
      end

      # TODO: add popups with additional information

      text = parse(text)

      # Insert pre block extractions
      text.gsub!(%r\{gfm-extraction-(\h{32})\}/) do
        extractions[$1]
      end

      sanitize text.html_safe, attributes: ActionView::Base.sanitized_allowed_attributes + %w(id class)
    end