diff --git a/README.md b/README.md index 78b6930..a279b6e 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,26 @@ toc: The default level range is `

` to `

`. +### Ignore within + +It can be configured to ignore elements within a selector: + +```yml +toc: + ignore_within: .exclude +``` + +```html +

h1

+
+

h2

+

h3

+
+

h4

+``` + +Which would result in only the `

` & `

` within the example being included in the TOC. + ### CSS Styling The toc can be modified with CSS. The sample CSS is the following. diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index 0eb5be8..4e6910c 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -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 @@ -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 diff --git a/lib/version.rb b/lib/version.rb index 2857f4b..5caebbb 100644 --- a/lib/version.rb +++ b/lib/version.rb @@ -1,3 +1,3 @@ module JekyllToc - VERSION = '0.7.1'.freeze + VERSION = '0.7.2'.freeze end diff --git a/test/test_various_toc_html.rb b/test/test_various_toc_html.rb index 5deeb7d..0812762 100644 --- a/test/test_various_toc_html.rb +++ b/test/test_various_toc_html.rb @@ -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

+
+

h2

+
+

h3

+
+

h4

+

h5

+
+
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 + + HTML + actual = doc.css('ul.section-nav').to_s + assert_equal(expected, actual) end end