Class: Danger::DangerToc

Inherits:
Plugin
  • Object
show all
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 filenames and remind the requester to update TOCs when necessary.


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

See Also:

  • dblock/danger-toc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dangerfile) ⇒ DangerToc

Returns a new instance of DangerToc.



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

def initialize(dangerfile)
  @filenames = ['README.md']
  super
end

Instance Attribute Details

#filenamesArray

The toc filenames, defaults to ‘[README.md]`.

Returns:

  • (Array)


19
20
21
# File 'lib/toc/plugin.rb', line 19

def filenames
  @filenames
end

Instance Method Details

#checkvoid

This method returns an undefined value.

Run all checks.



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

def check
  is_toc_correct?
end

#is_toc_correct?boolean

Is the TOC format correct?

Returns:

  • (boolean)


40
41
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
# File 'lib/toc/plugin.rb', line 40

def is_toc_correct?
  filenames.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 <<-MARKDOWN
Here's the expected TOC for #{filename}:

```markdown
# #{Danger::Toc.config.header}

#{toc_file.toc_from_headers.join("\n")}
```
MARKDOWN
      if toc_file.has_toc?
        messaging.fail("The TOC found in #{filename} doesn't match the sections of the file.", sticky: false)
      else
        messaging.fail("The #{filename} file is missing a TOC.", sticky: false)
      end
      false
    end
  end
end

#toc_changes?boolean

Has the README file been modified?

Returns:

  • (boolean)


34
35
36
# File 'lib/toc/plugin.rb', line 34

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