Class: Danger::Toc::Constructors::KramdownConstructor

Inherits:
Kramdown::Converter::Toc
  • Object
show all
Defined in:
lib/toc/constructors/kramdown_constructor.rb

Direct Known Subclasses

GithubConstructor

Instance Method Summary collapse

Instance Method Details

#convert(el) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/toc/constructors/kramdown_constructor.rb', line 25

def convert(el)
  toc = flatten(super(el))
  has_toc = false
  headers = []
  toc.each do |line|
    if !has_toc && line[:text] == Danger::Toc.config.header
      headers = [] # drop any headers prior to TOC
      has_toc = true
    else
      headers << line
    end
  end
  headers
end

#flatten(el) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/toc/constructors/kramdown_constructor.rb', line 7

def flatten(el)
  return [] unless el.type == :toc
  result = []
  if el.value
    result << {
      id: el.attr[:id],
      text: el.value.options[:raw_text],
      depth: el.value.options[:level]
    }
  end
  if el.children
    el.children.each do |child|
      result.concat(flatten(child))
    end
  end
  result
end