Skip to content

Commit

Permalink
Support arbitrary note types.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Jan 21, 2024
1 parent 31af695 commit deb09c2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
16 changes: 10 additions & 6 deletions lib/kitabu/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Renderer < Redcarpet::Render::HTML
include Redcarpet::Render::SmartyPants
include Rouge::Plugins::Redcarpet

ALERT_MARK = /^\[!(?<type>NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]$/
# 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
Expand All @@ -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
<div class="alert alert--#{matches[:type].downcase}">
<p class="alert--title">#{title}</p>
<div class="note #{type}">
<p class="note--title">#{title}</p>
#{html.css('body').inner_html}
</div>
HTML
Expand Down
20 changes: 17 additions & 3 deletions spec/kitabu/markdown_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,34 @@ 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
TEXT

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

0 comments on commit deb09c2

Please sign in to comment.