diff --git a/.gitignore b/.gitignore index 3573f4a..7bdc6ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,20 @@ +# gems *.gem /Gemfile.lock -/.bundle/ -/coverage *.gemfile.lock +/gems/ + +# local gem package folder +/pkg/ + +# tests / test coverage +.jekyll-cache/ +/coverage + +# bundler +/.bundle/ +/vendor/bundle/ +/gemfiles/.bundle/ +/gemfiles/vendor/bundle/ + +# unsorted diff --git a/.rubocop.yml b/.rubocop.yml index 3abbf6b..53b5802 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -20,6 +20,8 @@ Metrics/ClassLength: Naming/FileName: Enabled: false +Naming/VariableNumber: + Enabled: false Layout/LineLength: Enabled: false @@ -41,3 +43,6 @@ Style/HashTransformValues: Enabled: true Style/ExponentialNotation: Enabled: true + +Performance/StringInclude: + Enabled: false diff --git a/lib/table_of_contents/configuration.rb b/lib/table_of_contents/configuration.rb index aa6a9c4..89529f5 100644 --- a/lib/table_of_contents/configuration.rb +++ b/lib/table_of_contents/configuration.rb @@ -5,17 +5,20 @@ module TableOfContents # jekyll-toc configuration class class Configuration attr_reader :toc_levels, :no_toc_class, :ordered_list, :no_toc_section_class, - :list_class, :sublist_class, :item_class, :item_prefix + :list_id, :list_class, :sublist_class, :item_class, :item_prefix, + :nav_to_toc_symbol DEFAULT_CONFIG = { 'min_level' => 1, 'max_level' => 6, 'ordered_list' => false, 'no_toc_section_class' => 'no_toc_section', + 'list_id' => 'toc', 'list_class' => 'section-nav', 'sublist_class' => '', 'item_class' => 'toc-entry', - 'item_prefix' => 'toc-' + 'item_prefix' => 'toc-', + 'nav_to_toc_symbol' => '↥' }.freeze def initialize(options) @@ -25,10 +28,12 @@ def initialize(options) @ordered_list = options['ordered_list'] @no_toc_class = 'no_toc' @no_toc_section_class = options['no_toc_section_class'] + @list_id = options['list_id'] @list_class = options['list_class'] @sublist_class = options['sublist_class'] @item_class = options['item_class'] @item_prefix = options['item_prefix'] + @nav_to_toc_symbol = options['nav_to_toc_symbol'] end private diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index 0521950..7f24531 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -19,7 +19,7 @@ def toc end def build_toc - %(<#{list_tag} class="#{@configuration.list_class}">\n#{build_toc_list(@entries)}) + %(<#{list_tag} id="#{@configuration.list_id}" class="#{@configuration.list_class}">\n#{build_toc_list(@entries)}) end def inject_anchors_into_html @@ -28,6 +28,14 @@ def inject_anchors_into_html entry[:header_content].add_previous_sibling( %() ) + + # Add link 'nav to toc' + arr_to_top = [2, 3] + next unless arr_to_top.include?(entry[:h_num]) + + entry[:header_content].add_next_sibling( + %() + ) end @doc.inner_html diff --git a/test/parser/test_inject_anchors_filter.rb b/test/parser/test_inject_anchors_filter.rb index 160da4c..61d00b9 100644 --- a/test/parser/test_inject_anchors_filter.rb +++ b/test/parser/test_inject_anchors_filter.rb @@ -18,6 +18,6 @@ def test_injects_anchors_into_content def test_does_not_inject_toc html = @parser.inject_anchors_into_html - refute_includes(html, %(