Module: Markdowndocs::DocsHelper
- Defined in:
- app/helpers/markdowndocs/docs_helper.rb
Instance Method Summary collapse
- #add_heading_anchors(html) ⇒ Object
- #generate_table_of_contents(html) ⇒ Object
- #markdowndocs_format_breadcrumbs(category, title) ⇒ Object
- #slugify_heading(text) ⇒ Object
Instance Method Details
#add_heading_anchors(html) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/helpers/markdowndocs/docs_helper.rb', line 35 def add_heading_anchors(html) return html if html.blank? doc = Nokogiri::HTML.fragment(html) doc.css("h2, h3").each do |heading| text = heading.text.strip next if text.blank? unless heading["id"].present? slug = slugify_heading(text) heading["id"] = slug end end doc.css("a.anchor").each do |anchor| anchor.remove if anchor.text.strip.empty? end doc.to_html end |
#generate_table_of_contents(html) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/helpers/markdowndocs/docs_helper.rb', line 5 def generate_table_of_contents(html) return [] if html.blank? doc = Nokogiri::HTML.fragment(html) toc_items = [] doc.css("h2, h3").each do |heading| text = heading.text.strip next if text.blank? slug = heading["id"].presence || slugify_heading(text) toc_items << { text: text, slug: slug, level: heading.name[1].to_i } end toc_items end |
#markdowndocs_format_breadcrumbs(category, title) ⇒ Object
57 58 59 60 61 62 63 |
# File 'app/helpers/markdowndocs/docs_helper.rb', line 57 def (category, title) [ {name: "Docs", path: markdowndocs.root_path, current: false}, {name: category, path: nil, current: false}, {name: title, path: nil, current: true} ] end |
#slugify_heading(text) ⇒ Object
27 28 29 30 31 32 33 |
# File 'app/helpers/markdowndocs/docs_helper.rb', line 27 def slugify_heading(text) text.to_s .downcase .gsub(/[^\w\s-]/, "") .gsub(/\s+/, "-").squeeze("-") .gsub(/^-|-$/, "") end |