Module: Troy::Markdown::Rouge
Overview
Create a new Redcarpet renderer, that prepares the code block to use rouge syntax.
Instance Method Summary collapse
Instance Method Details
#header(text, level) ⇒ Object
[View source]
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/troy/markdown.rb', line 24 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
[View source]
54 55 56 |
# File 'lib/troy/markdown.rb', line 54 def heading_counter @heading_counter ||= Hash.new {|h, k| h[k] = 0} end |
#permalink(text) ⇒ Object
[View source]
44 45 46 47 48 49 50 51 52 |
# File 'lib/troy/markdown.rb', line 44 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 |