Class: Jekyll::MD_TOC::Generator
- Inherits:
-
Generator
- Object
- Generator
- Jekyll::MD_TOC::Generator
- Defined in:
- jekyll/_plugins/md-toc.rb
Instance Method Summary collapse
Instance Method Details
#generate(site) ⇒ Object
55 56 57 58 59 60 |
# File 'jekyll/_plugins/md-toc.rb', line 55 def generate(site) code_pages = site.pages.select {|page| page.name.end_with? ".js.md" } code_pages.each do | page| page.content = make_toc(page.content) end end |
#make_toc(text) ⇒ Object
23 24 25 26 27 28 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 |
# File 'jekyll/_plugins/md-toc.rb', line 23 def make_toc(text) new_text = text[/\#\s.+\n/] new_text.concat("\n\n## Table of Contents {#toc}\n") text_with_ids = text.gsub(/(?<hashes>\#{2,})\s(?<title>.+)\s*\n/) { |_| match = Regexp.last_match #slugify the subheading id_slug = match[2].downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') # indent if more than 2 x # depth = match[1].length - 2; while depth > 0 new_text.concat(" ") depth = depth - 1 end # add the list item to table of contents new_text.concat("- [#{match[2]}](##{id_slug})\n" ) # replace subheading with anchor # fat arrow ⇧ #(id:#{id_slug}) "#{match[1]} <a id='#{id_slug}'>#{match[2]}</a> [↑](#toc)\n" } new_text.concat("\n\n---\n\n") new_text.concat(text_with_ids) end |