class Danger::Toc::MarkdownFile
Attributes
exists[R]
filename[R]
headers[R]
toc[R]
Public Class Methods
new(filename = 'README.md')
click to toggle source
# File lib/toc/markdown_file.rb, line 14 def initialize(filename = 'README.md') @filename = filename @exists = File.exist?(filename) if @exists parse! reduce! validate! end end
Public Instance Methods
bad?()
click to toggle source
# File lib/toc/markdown_file.rb, line 28 def bad? !good? end
exists?()
click to toggle source
# File lib/toc/markdown_file.rb, line 24 def exists? !!@exists end
good?()
click to toggle source
# File lib/toc/markdown_file.rb, line 32 def good? !!@good end
has_toc?()
click to toggle source
# File lib/toc/markdown_file.rb, line 36 def has_toc? !!@has_toc end
toc_from_headers()
click to toggle source
# File lib/toc/markdown_file.rb, line 40 def toc_from_headers headers.map do |header| [ ' ' * header[:depth] * 2, "- [#{header[:text]}]", "(##{header[:id]})" ].compact.join end end
Private Instance Methods
parse!()
click to toggle source
Parse markdown file for TOC.
# File lib/toc/markdown_file.rb, line 53 def parse! md = File.read(filename) doc = Kramdown::Document.new(md, input: 'GFM') # extract toc toc_start, toc_end = Danger::Toc::Extractor.convert(doc.root).first @has_toc = toc_start && toc_end @toc = md.split("\n")[toc_start, toc_end - toc_start - 1].reject(&:empty?) if @has_toc # construct toc @headers = Danger::Toc::Constructors.current.convert(doc.root).first end
reduce!()
click to toggle source
# File lib/toc/markdown_file.rb, line 66 def reduce! min_depth = nil headers.each do |header| min_depth = header[:depth] unless min_depth && min_depth < header[:depth] end if min_depth headers.each do |header| header[:depth] -= min_depth end end end
validate!()
click to toggle source
# File lib/toc/markdown_file.rb, line 78 def validate! @good = (toc_from_headers == toc) end