Skip to content

Commit

Permalink
Allow mutiple no_toc_section_classes (#72)
Browse files Browse the repository at this point in the history
* Allow mutiple no_toc_section_class

* multiple no_toc_sections

* Update README.md

* Rename test method name
  • Loading branch information
toshimaru committed Nov 27, 2018
1 parent fc8aad5 commit d7e39a6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ toc:
no_toc_section_class: exclude # default: no_toc_section
```

Configuring mutiple classes are allowed:

```yml
toc:
no_toc_section_class:
- no_toc_section
- exclude
- your_custom_skip_class_name
```

#### TOC levels

The toc levels can be configured on `_config.yml`:
Expand Down
10 changes: 9 additions & 1 deletion lib/table_of_contents/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,15 @@ def toc_headings
end

def toc_headings_in_no_toc_section
@toc_levels.map { |level| ".#{@no_toc_section_class} h#{level}" }.join(',')
if @no_toc_section_class.is_a? Array
@no_toc_section_class.map { |cls| toc_headings_within(cls) }.join(',')
else
toc_headings_within(@no_toc_section_class)
end
end

def toc_headings_within(class_name)
@toc_levels.map { |level| ".#{class_name} h#{level}" }.join(',')
end

def generate_option_hash(options)
Expand Down
43 changes: 43 additions & 0 deletions test/test_various_toc_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,49 @@ def test_nested_toc_with_no_toc_section_class_option
assert_includes(html, '<h5>h5</h5>')
end

TEST_HTML_IGNORE_3 = <<~HTML
<h1>h1</h1>
<div class="no_toc_section">
<h2>h2</h2>
</div>
<h3>h3</h3>
<div class="exclude">
<h4>h4</h4>
<h5>h5</h5>
</div>
<h6>h6</h6>
HTML

def test_multiple_no_toc_section_classes
parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_IGNORE_3, 'no_toc_section_class' => ['no_toc_section', '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)

html = parser.inject_anchors_into_html
assert_match(%r{<h1>.+</h1>}m, html)
assert_match(%r{<h3>.+</h3>}m, html)
assert_match(%r{<h6>.+</h6>}m, html)
assert_includes(html, '<h2>h2</h2>')
assert_includes(html, '<h4>h4</h4>')
assert_includes(html, '<h5>h5</h5>')
end

TEST_EXPLICIT_ID = <<~HTML
<h1>h1</h1>
<h1 id="second">h2</h1>
Expand Down

0 comments on commit d7e39a6

Please sign in to comment.