From 1f0be2fdb432ef81fc22de0f04cd1038e0c7901a Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Wed, 8 Jan 2020 18:25:49 +0100 Subject: [PATCH 01/29] Url-Encoding for element id (spaces and UTF-8 support) --- lib/table_of_contents/parser.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index 85dd11c..34bc906 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require "erb" +include ERB::Util + module Jekyll module TableOfContents # Parse html contents and generate table of contents @@ -45,6 +48,7 @@ def parse_content .downcase .gsub(PUNCTUATION_REGEXP, '') # remove punctuation .tr(' ', '-') # replace spaces with dash + id = url_encode(id) suffix_num = headers[id] headers[id] += 1 From 35a1a1b3c2b453bb1653e1e78dd55eb2da4c4aeb Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Wed, 8 Jan 2020 18:30:00 +0100 Subject: [PATCH 02/29] Save parent element of entry, to add a anchor id --- lib/table_of_contents/parser.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index 34bc906..2133d46 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -25,6 +25,9 @@ def build_toc def inject_anchors_into_html @entries.each do |entry| + # Add id to h-element + entry[:header_parent].set_attribute("id", "#{entry[:id]}") + entry[:header_content].add_previous_sibling( %() ) @@ -57,6 +60,7 @@ def parse_content id: suffix_num.zero? ? id : "#{id}-#{suffix_num}", text: CGI.escapeHTML(text), node_name: node.name, + header_parent: node, header_content: node.children.first, h_num: node.name.delete('h').to_i } From 7ed55a238feb95fcbd37f7038c6058c9892abb6e Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Wed, 8 Jan 2020 18:32:25 +0100 Subject: [PATCH 03/29] Replace em-font-icon element with a unicode arrow --- lib/table_of_contents/parser.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index 2133d46..5dd1fb6 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -28,9 +28,9 @@ def inject_anchors_into_html # Add id to h-element entry[:header_parent].set_attribute("id", "#{entry[:id]}") - entry[:header_content].add_previous_sibling( - %() - ) + # Add link icon after text + entry[:header_content].add_next_sibling(%() + end @doc.inner_html From c714baffbe89ab09d963bd389717c07297831357 Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Wed, 8 Jan 2020 18:36:57 +0100 Subject: [PATCH 04/29] Format Code --- lib/table_of_contents/parser.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index 5dd1fb6..04fd25b 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -20,7 +20,7 @@ def toc end def build_toc - %() + %() end def inject_anchors_into_html @@ -29,9 +29,18 @@ def inject_anchors_into_html entry[:header_parent].set_attribute("id", "#{entry[:id]}") # Add link icon after text - entry[:header_content].add_next_sibling(%() - - end + entry[:header_content].add_next_sibling( + %( + ) + + # Add link 'nav to toc' + arrToTop = [ 2, 3 ] + if arrToTop.include?(entry[:h_num]) then + entry[:header_content].add_next_sibling( + %() + ) + end + end @doc.inner_html end From 2f054aa7824bc1a507dd14afacb6151f0ad66fb5 Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Wed, 8 Jan 2020 18:59:00 +0100 Subject: [PATCH 05/29] Format Code --- lib/table_of_contents/parser.rb | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index 04fd25b..f0b1279 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -25,21 +25,21 @@ def build_toc def inject_anchors_into_html @entries.each do |entry| - # Add id to h-element - entry[:header_parent].set_attribute("id", "#{entry[:id]}") + # Add id to h-element + entry[:header_parent].set_attribute("id", "#{entry[:id]}") - # Add link icon after text - entry[:header_content].add_next_sibling( - %( - ) - - # Add link 'nav to toc' - arrToTop = [ 2, 3 ] - if arrToTop.include?(entry[:h_num]) then - entry[:header_content].add_next_sibling( - %() - ) - end + # Add link icon after text + entry[:header_content].add_next_sibling( + %() + ) + + # Add link 'nav to toc' + arrToTop = [ 2, 3 ] + if arrToTop.include?(entry[:h_num]) then + entry[:header_content].add_next_sibling( + %() + ) + end end @doc.inner_html From b68f210770210f5ea1378a6fa202569989141bf5 Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Fri, 17 Jan 2020 16:55:17 +0100 Subject: [PATCH 06/29] Add class for the link to top --- lib/table_of_contents/parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index f0b1279..f8025fd 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -37,7 +37,7 @@ def inject_anchors_into_html arrToTop = [ 2, 3 ] if arrToTop.include?(entry[:h_num]) then entry[:header_content].add_next_sibling( - %() + %() ) end end From 7931ec2ce95f255edee275b1069d739af7aa59b8 Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Sat, 18 Jan 2020 12:11:13 +0100 Subject: [PATCH 07/29] PR94 - codeclimate fixes --- lib/table_of_contents/parser.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index f8025fd..c968ea3 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -1,9 +1,10 @@ # frozen_string_literal: true -require "erb" -include ERB::Util +require 'erb' module Jekyll + include ERB::Util + module TableOfContents # Parse html contents and generate table of contents class Parser @@ -26,21 +27,21 @@ def build_toc def inject_anchors_into_html @entries.each do |entry| # Add id to h-element - entry[:header_parent].set_attribute("id", "#{entry[:id]}") - + entry[:header_parent].set_attribute('id', '#{entry[:id]}') + # Add link icon after text entry[:header_content].add_next_sibling( %() ) # Add link 'nav to toc' - arrToTop = [ 2, 3 ] - if arrToTop.include?(entry[:h_num]) then + arrToTop = [2, 3] + if arrToTop.include?(entry[:h_num]) entry[:header_content].add_next_sibling( %() ) end - end + end @doc.inner_html end From be51dccd98359c0df231ab3b30137d920d711ca0 Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Sat, 18 Jan 2020 12:23:42 +0100 Subject: [PATCH 08/29] PR94 - codeclimate fixes --- lib/table_of_contents/parser.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index c968ea3..0d3dc4d 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -1,10 +1,9 @@ -# frozen_string_literal: true - require 'erb' +include ERB::Util +# Jekyll Modul, the root of everything module Jekyll - include ERB::Util - + # Module to wrap the classes for TOC creation module TableOfContents # Parse html contents and generate table of contents class Parser @@ -27,7 +26,7 @@ def build_toc def inject_anchors_into_html @entries.each do |entry| # Add id to h-element - entry[:header_parent].set_attribute('id', '#{entry[:id]}') + entry[:header_parent].set_attribute('id', "#{entry[:id]}") # Add link icon after text entry[:header_content].add_next_sibling( @@ -35,8 +34,8 @@ def inject_anchors_into_html ) # Add link 'nav to toc' - arrToTop = [2, 3] - if arrToTop.include?(entry[:h_num]) + arr_to_top = [2, 3] + if arr_to_top.include?(entry[:h_num]) entry[:header_content].add_next_sibling( %() ) From 9d41988a9fba32cfe421496aa44d044a93e9e2b9 Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Sat, 18 Jan 2020 12:42:39 +0100 Subject: [PATCH 09/29] PR94 - codeclimate fixes --- lib/table_of_contents/parser.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index 0d3dc4d..cf303fe 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -1,5 +1,6 @@ +# frozen_string_literal: true + require 'erb' -include ERB::Util # Jekyll Modul, the root of everything module Jekyll @@ -7,6 +8,8 @@ module Jekyll module TableOfContents # Parse html contents and generate table of contents class Parser + include ERB::Util + PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u def initialize(html, options = {}) @@ -26,7 +29,7 @@ def build_toc def inject_anchors_into_html @entries.each do |entry| # Add id to h-element - entry[:header_parent].set_attribute('id', "#{entry[:id]}") + entry[:header_parent].set_attribute('id', "#{entry[:id]}.to_s") # Add link icon after text entry[:header_content].add_next_sibling( From 790cc88c00da4fdcf28751416fb6a9104bc118a6 Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Sat, 18 Jan 2020 12:56:46 +0100 Subject: [PATCH 10/29] PR94 - codeclimate fixes --- lib/table_of_contents/parser.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index cf303fe..a169e80 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -38,11 +38,12 @@ def inject_anchors_into_html # Add link 'nav to toc' arr_to_top = [2, 3] - if arr_to_top.include?(entry[:h_num]) + next unless arr_to_top.include?(entry[:h_num]) + #if arr_to_top.include?(entry[:h_num]) entry[:header_content].add_next_sibling( %() ) - end + #end end @doc.inner_html From 941f02a5acbe81b0129ed5e2d3d1515f622216e8 Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Sat, 18 Jan 2020 13:00:41 +0100 Subject: [PATCH 11/29] PR94 - codeclimate fixes --- lib/table_of_contents/parser.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index a169e80..98a9e96 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -39,11 +39,9 @@ def inject_anchors_into_html # Add link 'nav to toc' arr_to_top = [2, 3] next unless arr_to_top.include?(entry[:h_num]) - #if arr_to_top.include?(entry[:h_num]) - entry[:header_content].add_next_sibling( - %() - ) - #end + entry[:header_content].add_next_sibling( + %() + ) end @doc.inner_html From 56ebcc478224a910f9317d1bc208bc693e909848 Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Sat, 18 Jan 2020 15:07:08 +0100 Subject: [PATCH 12/29] Add Testing Section to readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 5c5b398..b26fda8 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,14 @@ They are of the form: This is only useful when the TOC itself should be placed at some other location with the `toc_only` filter. +## Testing + +To run the tests you `rake`. First install gem packages local and run `rake test`. + +```shell +rake test +``` + ## Generated HTML jekyll-toc generates an unordered list. The HTML output is as follows. From b223d13f307663934940e050b0234b17ab6619c7 Mon Sep 17 00:00:00 2001 From: Nicolas Karg <50399433+N7K4@users.noreply.github.com> Date: Sat, 18 Jan 2020 15:07:21 +0100 Subject: [PATCH 13/29] Fix UnitTest test_toc_tag --- test/test_toc_tag.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_toc_tag.rb b/test/test_toc_tag.rb index ce2b30e..7f7c642 100644 --- a/test/test_toc_tag.rb +++ b/test/test_toc_tag.rb @@ -17,7 +17,7 @@ def test_toc_tag site: @stubbed_context1.new({ 'toc' => nil }) ) tag = Jekyll::TocTag.parse('toc_tag', '', Tokenizer.new(''), ParseContext.new) - assert_equal tag.render(context), "" + assert_equal tag.render(context), "" end def test_toc_tag_returns_empty_string From 5586a92db559ec87cdcbc339b76b64972f489532 Mon Sep 17 00:00:00 2001 From: Nicolas Karg Date: Sun, 19 Jan 2020 06:50:47 +0100 Subject: [PATCH 14/29] Fix unit tests, id and anchor --- test/parser/test_inject_anchors_filter.rb | 2 +- test/parser/test_option_error.rb | 4 +- test/parser/test_toc_filter.rb | 4 +- test/parser/test_toc_only_filter.rb | 2 +- test/parser/test_various_toc_html.rb | 48 +++++++++++------------ test/test_jekyll-toc.rb | 4 +- 6 files changed, 32 insertions(+), 32 deletions(-) diff --git a/test/parser/test_inject_anchors_filter.rb b/test/parser/test_inject_anchors_filter.rb index c9158e5..8c55aa0 100644 --- a/test/parser/test_inject_anchors_filter.rb +++ b/test/parser/test_inject_anchors_filter.rb @@ -12,7 +12,7 @@ def setup def test_injects_anchors_into_content html = @parser.inject_anchors_into_html - assert_match(%r{Simple H1}, html) + assert_match(%r{}, html) end def test_does_not_inject_toc diff --git a/test/parser/test_option_error.rb b/test/parser/test_option_error.rb index c469e29..acf3977 100644 --- a/test/parser/test_option_error.rb +++ b/test/parser/test_option_error.rb @@ -5,7 +5,7 @@ class TestOptionError < Minitest::Test BASE_HTML = '

h1

' EXPECTED_HTML = <<~HTML -