Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mutiple no_toc_section_classes #72

Merged
merged 4 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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