Module: MiddlemanMdocs::TOC

Included in:
Controller
Defined in:
lib/middleman-mdocs/toc.rb

Defined Under Namespace

Classes: Item

Instance Method Summary collapse

Instance Method Details

#table_of_contents(page_or_source, input: 'markdown') ⇒ Object

Generate TOC object tree for current page or source file



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/middleman-mdocs/toc.rb', line 52

def table_of_contents(page_or_source, input: 'markdown')
  # puts "#{Process.pid} =>>>> GENERATE TOC: #{page_or_source.try(:page_id) || page_or_source}"
  # byebug if page_or_source.page_id.force_encoding('UTF-8') == 'docs/services/gis-gmp/import-refunds'

  begin
    return nil if page_or_source.[:ignore]

    content = page_or_source.text
    toc = Kramdown::Document.new(content, app.config[:markdown].merge(input: 'html')).to_toc

    return Item.parse_kramdown_toc(toc)
  rescue StandardError => e
    # puts "WARNING: #{e}"
    # puts e.backtrace
  end

  content = ::File.read(page_or_source.try(:source_file) || page_or_source)
  # remove YAML frontmatter
  content = content.gsub(/^(---\s*\n.*?\n?)^(---\s*$\n?)/m, '')

  toc = if input == 'markdown'
          Kramdown::Document.new(content, app.config[:markdown]).to_toc
        else
          Kramdown::Document.new(content, input: input).to_toc
        end

  Item.parse_kramdown_toc(toc)
end