Module: GitlabMarkdownHelper

Defined in:
app/helpers/gitlab_markdown_helper.rb

Instance Method Summary collapse

Instance Method Details

#asciidoc(text) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/gitlab_markdown_helper.rb', line 69

def asciidoc(text)
  Gitlab::Asciidoc.render(
    text,
    project:      @project,
    current_user: (current_user if defined?(current_user)),

    # RelativeLinkFilter
    project_wiki:   @project_wiki,
    requested_path: @path,
    ref:            @ref,
    commit:         @commit
  )
end

#first_line_in_markdown(text, max_chars = nil, options = {}) ⇒ Object

Return the first line of text, up to max_chars, after parsing the line as Markdown. HTML tags in the parsed output are not counted toward the max_chars limit. If the length limit falls within a tag's contents, then the tag contents are truncated without removing the closing tag.


102
103
104
105
106
# File 'app/helpers/gitlab_markdown_helper.rb', line 102

def first_line_in_markdown(text, max_chars = nil, options = {})
  md = markdown(text, options).strip

  truncate_visible(md, max_chars || md.length) if md.present?
end

Use this in places where you would normally use link_to(gfm(…), …).

It solves a problem occurring with nested links (i.e. “<a>outer text <a>gfm ref</a> more outer text</a>”). This will not be interpreted as intended. Browsers will parse something like “<a>outer text </a><a>gfm ref</a> more outer text” (notice the last part is not linked any more). link_to_gfm corrects that. It wraps all parts to explicitly produce the correct linking behavior (i.e. “<a>outer text </a><a>gfm ref</a><a> more outer text</a>”).


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
# File 'app/helpers/gitlab_markdown_helper.rb', line 13

def link_to_gfm(body, url, html_options = {})
  return "" if body.blank?

  escaped_body = if body =~ /\A\<img/
                   body
                 else
                   escape_once(body)
                 end

  user = current_user if defined?(current_user)
  gfm_body = Banzai.render(escaped_body, project: @project, current_user: user, pipeline: :single_line)

  fragment = Nokogiri::HTML::DocumentFragment.parse(gfm_body)
  if fragment.children.size == 1 && fragment.children[0].name == 'a'
    # Fragment has only one node, and it's a link generated by `gfm`.
    # Replace it with our requested link.
    text = fragment.children[0].text
    fragment.children[0].replace(link_to(text, url, html_options))
  else
    # Traverse the fragment's first generation of children looking for pure
    # text, wrapping anything found in the requested link
    fragment.children.each do |node|
      next unless node.text?
      node.replace(link_to(node.text, url, html_options))
    end
  end

  # Add any custom CSS classes to the GFM-generated reference links
  if html_options[:class]
    fragment.css('a.gfm').add_class(html_options[:class])
  end

  fragment.to_html.html_safe
end

#markdown(text, context = {}) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/helpers/gitlab_markdown_helper.rb', line 48

def markdown(text, context = {})
  return "" unless text.present?

  context[:project] ||= @project

  text = Banzai.pre_process(text, context)

  html = Banzai.render(text, context)

  context.merge!(
    current_user:   (current_user if defined?(current_user)),

    # RelativeLinkFilter
    requested_path: @path,
    project_wiki:   @project_wiki,
    ref:            @ref
  )

  Banzai.post_process(html, context)
end

#other_markup(file_name, text) ⇒ Object


83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/helpers/gitlab_markdown_helper.rb', line 83

def other_markup(file_name, text)
  Gitlab::OtherMarkup.render(
    file_name,
    text,
    project:      @project,
    current_user: (current_user if defined?(current_user)),

    # RelativeLinkFilter
    project_wiki:   @project_wiki,
    requested_path: @path,
    ref:            @ref,
    commit:         @commit
  )
end

#render_wiki_content(wiki_page) ⇒ Object


108
109
110
111
112
113
114
115
116
117
# File 'app/helpers/gitlab_markdown_helper.rb', line 108

def render_wiki_content(wiki_page)
  case wiki_page.format
  when :markdown
    markdown(wiki_page.content, pipeline: :wiki, project_wiki: @project_wiki)
  when :asciidoc
    asciidoc(wiki_page.content)
  else
    wiki_page.formatted_content.html_safe
  end
end