Module: Troy::Markdown::Rouge
Overview
Create a new Redcarpet renderer, that prepares the code block to use rouge syntax.
Constant Summary collapse
- ALERT_MARK =
Be more flexible than github and support any arbitrary name.
/^\[!(?<type>[A-Z]+)\](?<title>.*?)?$/
Instance Method Summary collapse
-
#block_quote(quote) ⇒ Object
Support alert boxes just like github.
- #header(text, level) ⇒ Object
- #heading_counter ⇒ Object
- #permalink(text) ⇒ Object
Instance Method Details
#block_quote(quote) ⇒ Object
Support alert boxes just like github. github.com/orgs/community/discussions/16925
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/troy/markdown.rb', line 29 def block_quote(quote) html = Nokogiri::HTML.fragment(quote) element = html.children.first matches = element.text.to_s.match(ALERT_MARK) if element return "<blockquote>#{quote}</blockquote>" unless matches element.remove type = matches[:type].downcase title = matches[:title].to_s.strip title = I18n.t(type, scope: :alerts, default: title) html = Nokogiri::HTML.fragment <<~HTML <div class="alert-message #{type}"> <p class="alert-message--title"></p> #{html} </div> HTML if title.empty? html.css(".alert-message--title").first.remove else html.css(".alert-message--title").first.content = title end html.to_s end |
#header(text, level) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/troy/markdown.rb', line 57 def header(text, level) matches = text.strip.match(HEADING_ID) title = matches[:text].strip html = Nokogiri::HTML.fragment("<h#{level}>#{title}</h#{level}>") heading = html.first_element_child title = heading.text id = matches[:id] id ||= permalink(title) heading_counter[id] += 1 id = "#{id}-#{heading_counter[id]}" if heading_counter[id] > 1 heading.add_child %[<a class="anchor" href="##{id}" aria-hidden="true" tabindex="-1"></a>] # rubocop:disable Style/LineLength heading.set_attribute :tabindex, "-1" heading.set_attribute(:id, id) heading.to_s end |
#heading_counter ⇒ Object
87 88 89 |
# File 'lib/troy/markdown.rb', line 87 def heading_counter @heading_counter ||= Hash.new {|h, k| h[k] = 0 } end |
#permalink(text) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/troy/markdown.rb', line 77 def permalink(text) str = text.dup.unicode_normalize(:nfkd) str = str.gsub(/[^\x00-\x7F]/, "").to_s str.gsub!(/[^-\w]+/xim, "-") str.gsub!(/-+/xm, "-") str.gsub!(/^-?(.*?)-?$/, '\1') str.downcase! str end |