Skip to content

Commit

Permalink
Merge pull request #53 from bandzoogle/master
Browse files Browse the repository at this point in the history
Add an `ignore_within` option
  • Loading branch information
toshimaru authored Oct 9, 2018
2 parents 4b1d91d + 6607e7b commit 933ebdf
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ toc:

The default level range is `<h1>` to `<h6>`.

### Ignore within

It can be configured to ignore elements within a selector:

```yml
toc:
ignore_within: .exclude
```

```html
<h1>h1</h1>
<div class="exclude">
<h2>h2</h2>
<h3>h3</h3>
</div>
<h4>h4</h4>
```

Which would result in only the `<h1>` & `<h4>` within the example being included in the TOC.

### CSS Styling

The toc can be modified with CSS. The sample CSS is the following.
Expand Down
7 changes: 6 additions & 1 deletion lib/table_of_contents/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Parser
def initialize(html, options = {})
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
options = generate_option_hash(options)
@toc_levels = options['min_level']..options['max_level']
@toc_levels = options["min_level"]..options["max_level"]
@ignore_within = options["ignore_within"]
@entries = parse_content
end

Expand Down Expand Up @@ -42,6 +43,10 @@ def parse_content
entries = []
headers = Hash.new(0)

if @ignore_within
@doc.css(@ignore_within).remove
end

# TODO: Use kramdown auto ids
@doc.css(toc_headings).reject { |n| n.classes.include?('no_toc') }.each do |node|
text = node.text
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module JekyllToc
VERSION = '0.7.1'.freeze
VERSION = '0.7.2'.freeze
end
36 changes: 36 additions & 0 deletions test/test_various_toc_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,42 @@ def test_tags_inside_heading
HTML
actual = doc.css('ul.section-nav').to_s

assert_equal(expected, actual)
end

TEST_HTML_IGNORE = <<-HTML
<h1>h1</h1>
<div class="exclude">
<h2>h2</h2>
</div>
<h3>h3</h3>
<div class="exclude">
<h4>h4</h4>
<h4>h5</h4>
</div>
<h6>h6</h6>
HTML

def test_nested_toc_with_ignore_within_option
parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_IGNORE, { "ignore_within" => '.exclude'})
doc = Nokogiri::HTML(parser.toc)
expected = <<-HTML
<ul class="section-nav">
<li class="toc-entry toc-h1">
<a href="#h1">h1</a>
<ul>
<li class="toc-entry toc-h3">
<a href="#h3">h3</a>
<ul>
<li class="toc-entry toc-h6"><a href="#h6">h6</a></li>
</ul>
</li>
</ul>
</li>
</ul>
HTML
actual = doc.css('ul.section-nav').to_s

assert_equal(expected, actual)
end
end

0 comments on commit 933ebdf

Please sign in to comment.