From deb09c2a58c51f2ca2b3802c4f27402153847ae3 Mon Sep 17 00:00:00 2001 From: Nando Vieira Date: Sat, 20 Jan 2024 17:18:01 -0800 Subject: [PATCH] Support arbitrary note types. --- lib/kitabu/markdown.rb | 16 ++++++++++------ spec/kitabu/markdown_spec.rb | 20 +++++++++++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/kitabu/markdown.rb b/lib/kitabu/markdown.rb index f2b98cd..3775f18 100644 --- a/lib/kitabu/markdown.rb +++ b/lib/kitabu/markdown.rb @@ -6,7 +6,8 @@ class Renderer < Redcarpet::Render::HTML include Redcarpet::Render::SmartyPants include Rouge::Plugins::Redcarpet - ALERT_MARK = /^\[!(?NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]$/ + # Be more flexible than github and support any arbitrary name. + ALERT_MARK = /^\[!(?[A-Z]+)\]$/ # Support alert boxes just like github. # https://github.com/orgs/community/discussions/16925 @@ -20,15 +21,18 @@ def block_quote(quote) element.remove + type = matches[:type].downcase + type = "info" if type == "note" + title = I18n.t( - matches[:type], - scope: :alerts, - default: matches[:type].titleize + type, + scope: :notes, + default: type.titleize ) <<~HTML.strip_heredoc -
-

#{title}

+
+

#{title}

#{html.css('body').inner_html}
HTML diff --git a/spec/kitabu/markdown_spec.rb b/spec/kitabu/markdown_spec.rb index bf4fa2d..f2c41cf 100644 --- a/spec/kitabu/markdown_spec.rb +++ b/spec/kitabu/markdown_spec.rb @@ -53,12 +53,26 @@ class User TEXT html = Nokogiri::HTML(html) - selector = "div.alert.alert--note > .alert--title" + selector = "div.note.info > .note--title" - expect(html.css(selector).text).to eql("Note") + expect(html.css(selector).text).to eql("Info") expect(html.css("#{selector} + p").text).to eql("This is just a note") end + it "renders arbitrary alert boxes using block quotes" do + html = Kitabu::Markdown.render <<-TEXT.strip_heredoc + > [!ALERT] + > + > This is just an alert + TEXT + + html = Nokogiri::HTML(html) + selector = "div.note.alert > .note--title" + + expect(html.css(selector).text).to eql("Alert") + expect(html.css("#{selector} + p").text).to eql("This is just an alert") + end + it "renders regular block quotes" do html = Kitabu::Markdown.render <<-TEXT.strip_heredoc > This is just a quote @@ -66,7 +80,7 @@ class User html = Nokogiri::HTML(html) - expect(html.css(".alert").count).to eql(0) + expect(html.css(".note").count).to eql(0) expect(html.css("blockquote").text.chomp).to eql("This is just a quote") end end