Class: Danger::DangerToc

Inherits:
Plugin
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/toc/plugin.rb

Overview

Check whether the TOC in .md file(s) has been updated.

Examples:

Run all checks on the default README.md.


toc.check!

Customize files and remind the requester to update TOCs when necessary.


toc.files = ['README.md']
toc.is_toc_correct?

See Also:

  • dblock/danger-toc

Instance Method Summary collapse

Instance Method Details

#checkvoid

This method returns an undefined value.

Run all checks.



29
30
31
32
# File 'lib/toc/plugin.rb', line 29

def check
  warn '[DEPRECATION] `check` is deprecated. Please use `check!` instead.'
  check!
end

#check!void

This method returns an undefined value.

Run all checks.



23
24
25
# File 'lib/toc/plugin.rb', line 23

def check!
  is_toc_correct?
end

#is_toc_correct?boolean

Is the TOC format correct?

Returns:

  • (boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/toc/plugin.rb', line 42

def is_toc_correct?
  files.all? do |filename|
    toc_file = Danger::Toc::MarkdownFile.new(filename)
    if !toc_file.exists?
      messaging.fail("The #{filename} file does not exist.", sticky: false)
      false
    elsif toc_file.good?
      true
    else
      markdown "Here's the expected TOC for \#{filename}:\n\n```markdown\n# \#{Danger::Toc.config.header}\n\n\#{toc_file.toc_from_headers.join(\"\\n\")}\n```\n      MARKDOWN\n      if toc_file.has_toc?\n        messaging.fail(\"The TOC found in \#{filename} doesn't match the sections of the file.\", sticky: false)\n      else\n        messaging.fail(\"The \#{filename} file is missing a TOC.\", sticky: false)\n      end\n      false\n    end\n  end\nend\n"

#toc_changes?boolean

Has the README file been modified?

Returns:

  • (boolean)


36
37
38
# File 'lib/toc/plugin.rb', line 36

def toc_changes?
  (git.modified_files & files).any? || (git.added_files & files).any?
end