Module: GitlabKramdown::Converter::GitlabHtml

Defined in:
lib/gitlab_kramdown/converter/gitlab_html.rb

Overview

Converts a Kramdown::Document to HTML.

This includes GitLab custom elements from GitLab Flavored Markdown syntax

Instance Method Summary collapse

Instance Method Details

#convert_codeblock(element, opts) ⇒ Object

Codeblock is customized in order to implement a different output to Mermaid

Mermaid requires ‘<div class=“mermaid”></div>` surrounding the content in order to trigger the unobtrusive JS.



31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab_kramdown/converter/gitlab_html.rb', line 31

def convert_codeblock(element, opts)
  case element.options[:lang]
  when 'mermaid'
    %(<div class="mermaid">#{element.value}</div>\n)
  when 'plantuml'
    render_plantuml(element)
  else
    super
  end
end

#convert_img(element, _indent) ⇒ Object

Images can have a link to themselves when configuration allows

We don’t autolink when image is already linked to something-else

Parameters:

  • element (Kramdown::Element)
  • _indent (Integer)


57
58
59
60
61
62
63
64
# File 'lib/gitlab_kramdown/converter/gitlab_html.rb', line 57

def convert_img(element, _indent)
  return super unless @clickable_images
  return super if @stack.last.type == :a

  href = element.attr['src']

  img_with_clickable_link(href, super)
end

#convert_label(element, _indent) ⇒ Object

Label element is generated by the GitlabKramdown parser, representing GitLab labels

It includes the following attributes:

  • href: the URL for the label

  • label: the name of the label without namespace part

  • namespace: the namespace part of the label



72
73
74
75
76
77
78
79
# File 'lib/gitlab_kramdown/converter/gitlab_html.rb', line 72

def convert_label(element, _indent)
  label_attrs = { class: 'badge color-label' }

  label_text = "#{element.attr['label']} in <i>#{element.attr['namespace']}</i>"
  span_tag = format_as_span_html(:span, label_attrs, label_text)

  format_as_span_html(:a, { 'href' => element.attr['href'] }, span_tag)
end


81
82
83
# File 'lib/gitlab_kramdown/converter/gitlab_html.rb', line 81

def img_with_clickable_link(href, img_html)
  %(<a class="no-attachment-icon" href="#{href}" target="_blank" rel="noopener noreferrer">#{img_html}</a>)
end

#initialize(root, options) ⇒ Object

Initializes a GitLab custom HTML converter with a given document

It accepts all existing options for HTML converter with our custom ones:

  • clickable_images (default: true) - whether images will have a link to itself

Parameters:

  • root (Kramdown::Document)
  • options (Hash)


21
22
23
24
25
# File 'lib/gitlab_kramdown/converter/gitlab_html.rb', line 21

def initialize(root, options)
  super(root, options)

  @clickable_images = options[:clickable_images] != false
end

#plantuml_setupObject



42
43
44
45
46
47
48
49
# File 'lib/gitlab_kramdown/converter/gitlab_html.rb', line 42

def plantuml_setup
  Asciidoctor::PlantUml.configure do |conf|
    conf.url = 'https://plantuml.gitlab-static.net'
    conf.png_enable = true
    conf.svg_enable = false
    conf.txt_enable = false
  end
end

#render_plantuml(element) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gitlab_kramdown/converter/gitlab_html.rb', line 85

def render_plantuml(element)
  plantuml_setup

  img_tag = Nokogiri::HTML::DocumentFragment.parse(
    Asciidoctor::PlantUml::Processor.plantuml_content(element.value, {})
  )

  if @clickable_images
    href = img_tag.at('img').attr('src')

    "#{img_with_clickable_link(href, img_tag)}\n"
  else
    %(#{img_tag}\n)
  end
end