Class: Kitabu::Markdown::Renderer

Inherits:
Redcarpet::Render::HTML
  • Object
show all
Includes:
Redcarpet::Render::SmartyPants, Rouge::Plugins::Redcarpet
Defined in:
lib/kitabu/markdown.rb

Constant Summary collapse

ALERT_MARK =

Be more flexible than github and support any arbitrary name.

/^\[!(?<type>[A-Z]+)\]$/

Instance Method Summary collapse

Methods included from Rouge::Plugins::Redcarpet

#rouge_formatter

Instance Method Details

#block_quote(quote) ⇒ Object

Support alert boxes just like github. github.com/orgs/community/discussions/16925



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
# File 'lib/kitabu/markdown.rb', line 14

def block_quote(quote)
  html = Nokogiri::HTML(quote)
  element = html.css("body > :first-child").first

  matches = element.text.match(ALERT_MARK)

  return "<blockquote>#{quote}</blockquote>" unless matches

  element.remove

  type = matches[:type].downcase
  type = "info" if type == "note"

  title = I18n.t(
    type,
    scope: :notes,
    default: type.titleize
  )

  <<~HTML.strip_heredoc
    <div class="note #{type}">
      <p class="note--title">#{title}</p>
      #{html.css('body').inner_html}
    </div>
  HTML
end