lib/kitabu/markdown.rb in kitabu-3.0.3 vs lib/kitabu/markdown.rb in kitabu-3.1.0

- old
+ new

@@ -3,9 +3,41 @@ module Kitabu module Markdown class Renderer < Redcarpet::Render::HTML include Redcarpet::Render::SmartyPants include Rouge::Plugins::Redcarpet + + # Be more flexible than github and support any arbitrary name. + ALERT_MARK = /^\[!(?<type>[A-Z]+)\]$/ + + # Support alert boxes just like github. + # https://github.com/orgs/community/discussions/16925 + 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 end class << self # Set markdown renderer attr_accessor :processor