Class: Kramdown::Converter::SlodownHtml

Inherits:
Html
  • Object
show all
Defined in:
lib/kramdown/converter/slodown_html.rb

Overview

This is the custom kramdown converter class we will be using to render HTML from Markdown. It's essentially a handy way to hook into various elements and add our own logic (like supporting oEmbed embeds in Markdown image elements.)

Instance Method Summary collapse

Instance Method Details

#convert_figure(el, indent) ⇒ Object



33
34
35
# File 'lib/kramdown/converter/slodown_html.rb', line 33

def convert_figure(el, indent)
  "#{' '*indent}<figure><img#{html_attributes(el.attr)} />#{(el.attr['title'] ? "<figcaption>#{el.attr['title']}</figcaption>" : "")}</figure>\n"
end

#convert_p(el, indent) ⇒ Object

In Slodown, you can use block-level image attributes for oEmbed-based embeds. For this, we're hooking into #convert_p to find single block-level images.

If we can't use OEmbed, we'll assume the image is an actual image, and convert it into a

element (with optional
.)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kramdown/converter/slodown_html.rb', line 14

def convert_p(el, indent)
  if el.options[:transparent]
    inner(el, indent)
  elsif !el.children.nil? && el.children.count == 1 && el.children.first.type == :img
    # Try to handle the embedded object through OEmbed; if this fails,
    # handle it as an image instead and create a <figure>.
    child = el.children.first

    begin
      oembed = OEmbed::Providers.get(child.attr['src'])
      %q(<div class="embedded %s %s">%s</div>) % [oembed.type, oembed.provider_name.downcase.gsub(/\W+/, '-'), oembed.html]
    rescue OEmbed::NotFound => e
      convert_figure(child, indent)
    end
  else
    super
  end
end