From 2422c4541c20abaa69466d9ca712a06cce70df7a Mon Sep 17 00:00:00 2001 From: Teo Uosukainen Date: Thu, 30 May 2024 17:42:42 +0300 Subject: [PATCH 1/3] initial work on fixing empty links --- lib/table_of_contents/configuration.rb | 4 ++-- lib/table_of_contents/parser.rb | 13 +++++++------ test/parser/test_inject_anchors_filter.rb | 10 ++++++++-- test/test_configuration.rb | 8 ++++---- test/test_helper.rb | 12 ++++++------ 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/table_of_contents/configuration.rb b/lib/table_of_contents/configuration.rb index 595c73d..cb6e6d9 100644 --- a/lib/table_of_contents/configuration.rb +++ b/lib/table_of_contents/configuration.rb @@ -22,10 +22,10 @@ class Configuration def initialize(options) options = generate_option_hash(options) - @toc_levels = options['min_level']..options['max_level'] + @toc_levels = (options['min_level']..options['max_level']).to_a @ordered_list = options['ordered_list'] @no_toc_class = 'no_toc' - @no_toc_section_class = options['no_toc_section_class'] + @no_toc_section_class = Array(options['no_toc_section_class']) @list_id = options['list_id'] @list_class = options['list_class'] @sublist_class = options['sublist_class'] diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index 23509cc..6ef8b45 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -24,14 +24,14 @@ def build_toc def inject_anchors_into_html @entries.each do |entry| - # NOTE: `entry[:id]` is automatically URL encoded by Nokogiri - entry[:header_content].add_previous_sibling( - %() - ) + header = entry[:header_content] + header_content = header.inner_html + header.parent.inner_html = %() end - @doc.inner_html end + + private @@ -40,7 +40,7 @@ def inject_anchors_into_html def parse_content headers = Hash.new(0) - (@doc.css(toc_headings) - @doc.css(toc_headings_in_no_toc_section)) + entries = (@doc.css(toc_headings) - @doc.css(toc_headings_in_no_toc_section)) .reject { |n| n.classes.include?(@configuration.no_toc_class) } .inject([]) do |entries, node| text = node.text @@ -57,6 +57,7 @@ def parse_content h_num: node.name.delete('h').to_i } end + entries end # Returns the list items for entries diff --git a/test/parser/test_inject_anchors_filter.rb b/test/parser/test_inject_anchors_filter.rb index 61d00b9..d30a252 100644 --- a/test/parser/test_inject_anchors_filter.rb +++ b/test/parser/test_inject_anchors_filter.rb @@ -11,8 +11,14 @@ def setup def test_injects_anchors_into_content html = @parser.inject_anchors_into_html - - assert_match(%r{Simple H1}, html) + #puts "START OF HTML #{html} END OF HTML IN TEST" + + (1..6).each do |level| + assert_match( + %r{}, + html + ) + end end def test_does_not_inject_toc diff --git a/test/test_configuration.rb b/test/test_configuration.rb index a3e77c9..f0d16dd 100644 --- a/test/test_configuration.rb +++ b/test/test_configuration.rb @@ -6,9 +6,9 @@ class TestConfiguration < Minitest::Test def test_default_configuration configuration = Jekyll::TableOfContents::Configuration.new({}) - assert_equal(1..6, configuration.toc_levels) + assert_equal((1..6).to_a, configuration.toc_levels) refute(configuration.ordered_list) - assert_equal('no_toc_section', configuration.no_toc_section_class) + assert_equal(['no_toc_section'], configuration.no_toc_section_class) assert_equal('toc', configuration.list_id) assert_equal('section-nav', configuration.list_class) assert_equal('', configuration.sublist_class) @@ -19,9 +19,9 @@ def test_default_configuration def test_type_error configuration = Jekyll::TableOfContents::Configuration.new('TypeError!') - assert_equal(1..6, configuration.toc_levels) + assert_equal((1..6).to_a, configuration.toc_levels) refute(configuration.ordered_list) - assert_equal('no_toc_section', configuration.no_toc_section_class) + assert_equal(['no_toc_section'], configuration.no_toc_section_class) assert_equal('toc', configuration.list_id) assert_equal('section-nav', configuration.list_class) assert_equal('', configuration.sublist_class) diff --git a/test/test_helper.rb b/test/test_helper.rb index 3faa4eb..aa5177b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,12 +11,12 @@ require 'jekyll-toc' SIMPLE_HTML = <<~HTML -

Simple H1

-

Simple H2

-

Simple H3

-

Simple H4

-
Simple H5
-
Simple H6
+

Simple H1

+

Simple H2

+

Simple H3

+

Simple H4

+
Simple H5
+
Simple H6
HTML module TestHelpers From 6171349f3ea1fcb61136c44507ad23811e547950 Mon Sep 17 00:00:00 2001 From: Teo Uosukainen Date: Thu, 30 May 2024 19:23:20 +0300 Subject: [PATCH 2/3] reverted redundant changes, tests work --- lib/table_of_contents/parser.rb | 6 +----- test/parser/test_inject_anchors_filter.rb | 11 +++-------- test/parser/test_toc_filter.rb | 2 +- test/parser/test_various_toc_html.rb | 6 +++--- test/test_helper.rb | 12 ++++++------ 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index 6ef8b45..c0565d9 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -24,14 +24,10 @@ def build_toc def inject_anchors_into_html @entries.each do |entry| - header = entry[:header_content] - header_content = header.inner_html - header.parent.inner_html = %() + entry[:header_content].wrap(%()) end @doc.inner_html end - - private diff --git a/test/parser/test_inject_anchors_filter.rb b/test/parser/test_inject_anchors_filter.rb index d30a252..03bcd1b 100644 --- a/test/parser/test_inject_anchors_filter.rb +++ b/test/parser/test_inject_anchors_filter.rb @@ -11,15 +11,10 @@ def setup def test_injects_anchors_into_content html = @parser.inject_anchors_into_html - #puts "START OF HTML #{html} END OF HTML IN TEST" - - (1..6).each do |level| - assert_match( - %r{}, - html - ) - end + + assert_match(%r{}, html) end + def test_does_not_inject_toc html = @parser.inject_anchors_into_html diff --git a/test/parser/test_toc_filter.rb b/test/parser/test_toc_filter.rb index 6fe2fb7..ebd8103 100644 --- a/test/parser/test_toc_filter.rb +++ b/test/parser/test_toc_filter.rb @@ -12,7 +12,7 @@ def setup def test_injects_anchors html = @parser.toc - assert_match(%r{Simple H1}, html) + assert_match(%r{}, html) end def test_nested_toc diff --git a/test/parser/test_various_toc_html.rb b/test/parser/test_various_toc_html.rb index 9fdd221..6cb6e4d 100644 --- a/test/parser/test_various_toc_html.rb +++ b/test/parser/test_various_toc_html.rb @@ -172,9 +172,9 @@ def test_japanese_toc assert_equal(expected, parser.build_toc) html_with_anchors = parser.inject_anchors_into_html - assert_match(%r{あ}, html_with_anchors) - assert_match(%r{い}, html_with_anchors) - assert_match(%r{う}, html_with_anchors) + assert_match(%r{}, html_with_anchors) + assert_match(%r{}, html_with_anchors) + assert_match(%r{}, html_with_anchors) end # ref. https://github.com/toshimaru/jekyll-toc/issues/45 diff --git a/test/test_helper.rb b/test/test_helper.rb index aa5177b..3faa4eb 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,12 +11,12 @@ require 'jekyll-toc' SIMPLE_HTML = <<~HTML -

Simple H1

-

Simple H2

-

Simple H3

-

Simple H4

-
Simple H5
-
Simple H6
+

Simple H1

+

Simple H2

+

Simple H3

+

Simple H4

+
Simple H5
+
Simple H6
HTML module TestHelpers From c5e22995c860d1cc1636c8f0bceedd0c70f25c02 Mon Sep 17 00:00:00 2001 From: Teo Uosukainen Date: Thu, 30 May 2024 20:01:34 +0300 Subject: [PATCH 3/3] revert change in parse_content --- lib/table_of_contents/parser.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index c0565d9..fb5d793 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -36,7 +36,7 @@ def inject_anchors_into_html def parse_content headers = Hash.new(0) - entries = (@doc.css(toc_headings) - @doc.css(toc_headings_in_no_toc_section)) + (@doc.css(toc_headings) - @doc.css(toc_headings_in_no_toc_section)) .reject { |n| n.classes.include?(@configuration.no_toc_class) } .inject([]) do |entries, node| text = node.text @@ -53,7 +53,6 @@ def parse_content h_num: node.name.delete('h').to_i } end - entries end # Returns the list items for entries