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

My0150 nav to top #133

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 17 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Metrics/ClassLength:

Naming/FileName:
Enabled: false
Naming/VariableNumber:
Enabled: false

Layout/LineLength:
Enabled: false
Expand All @@ -41,3 +43,6 @@ Style/HashTransformValues:
Enabled: true
Style/ExponentialNotation:
Enabled: true

Performance/StringInclude:
Enabled: false
9 changes: 7 additions & 2 deletions lib/table_of_contents/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
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 @@ -19,7 +19,7 @@ def toc
end

def build_toc
%(<#{list_tag} class="#{@configuration.list_class}">\n#{build_toc_list(@entries)}</#{list_tag}>)
%(<#{list_tag} id="#{@configuration.list_id}" class="#{@configuration.list_class}">\n#{build_toc_list(@entries)}</#{list_tag}>)
end

def inject_anchors_into_html
Expand All @@ -28,6 +28,14 @@ def inject_anchors_into_html
entry[:header_content].add_previous_sibling(
%(<a class="anchor" href="##{entry[:id]}" aria-hidden="true"><span class="octicon octicon-link"></span></a>)
)

# 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(
%(<span style="float: right"><a class="anchor_to_top" href="#{@configuration.list_id}" aria-hidden="true">#{@configuration.nav_to_toc_symbol}</a></span>)
)
end

@doc.inner_html
Expand Down
2 changes: 1 addition & 1 deletion test/parser/test_inject_anchors_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, %(<ul class="section-nav">))
refute_includes(html, %(<ul id="toc" class="section-nav">))
end
end
2 changes: 1 addition & 1 deletion test/parser/test_invalid_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class TestInvalidOptions < Minitest::Test
BASE_HTML = '<h1>h1</h1>'
EXPECTED_HTML = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" class="section-nav">
<li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
</ul>
HTML
Expand Down
4 changes: 2 additions & 2 deletions test/parser/test_ordered_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_basic_ordered_list_top_heading
parse_with_ordered_list
html = @parser.toc

assert_match(/^<ol class="section-nav">/, html)
assert_match(/^<ol id="toc" class="section-nav">/, html)
end

def test_ordered_list_sub_headings
Expand All @@ -41,7 +41,7 @@ def test_ordered_list_top_heading_with_classes
parse_with_ordered_list_and_classes
html = @parser.toc

assert_match(/^<ol class="top-list-class">/, html)
assert_match(/^<ol id="toc" class="top-list-class">/, html)
end

def test_ordered_list_sub_headings_with_classes
Expand Down
2 changes: 1 addition & 1 deletion test/parser/test_toc_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ def test_nested_toc
def test_injects_toc_container
html = @parser.toc

assert_match(/<ul class="section-nav">/, html)
assert_match(/<ul id="toc" class="section-nav">/, html)
end
end
2 changes: 1 addition & 1 deletion test/parser/test_toc_only_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setup
def test_injects_toc_container
html = @parser.build_toc

assert_includes(html, %(<ul class="section-nav">))
assert_includes(html, %(<ul id=\"toc\" class="section-nav">))
end

def test_does_not_return_content
Expand Down
30 changes: 15 additions & 15 deletions test/parser/test_various_toc_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestVariousTocHtml < Minitest::Test
def test_nested_toc
parser = Jekyll::TableOfContents::Parser.new(TEST_HTML)
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" 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>
Expand All @@ -31,7 +31,7 @@ def test_nested_toc
def test_nested_toc_with_min_and_max
parser = Jekyll::TableOfContents::Parser.new(TEST_HTML, 'min_level' => 2, 'max_level' => 5)
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" class="section-nav">
<li class="toc-entry toc-h3"><a href="#h3">h3</a></li>
</ul>
HTML
Expand All @@ -47,7 +47,7 @@ def test_complex_nested_toc
<h6>h6</h6>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" 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></li>
Expand All @@ -74,7 +74,7 @@ def test_decremental_headings1
<h1>h1</h1>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" class="section-nav">
<li class="toc-entry toc-h6"><a href="#h6">h6</a></li>
<li class="toc-entry toc-h5"><a href="#h5">h5</a></li>
<li class="toc-entry toc-h4"><a href="#h4">h4</a></li>
Expand All @@ -96,7 +96,7 @@ def test_decremental_headings2
<h5>h5</h5>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" 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></li>
Expand Down Expand Up @@ -129,7 +129,7 @@ def test_no_toc
<h4 class="no_toc">no_toc h4</h4>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" class="section-nav">
<li class="toc-entry toc-h1"><a href="#h1">h1</a>
<ul>
<li class="toc-entry toc-h2"><a href="#h2">h2</a>
Expand All @@ -156,7 +156,7 @@ def test_japanese_toc
<h3>う</h3>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" class="section-nav">
<li class="toc-entry toc-h1"><a href="#%E3%81%82">あ</a>
<ul>
<li class="toc-entry toc-h2"><a href="#%E3%81%84">い</a>
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_angle_bracket
<h1>&amp; &lt; &gt;</h1>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" class="section-nav">
<li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
<li class="toc-entry toc-h1"><a href="#base-href">&lt;base href&gt;</a></li>
<li class="toc-entry toc-h1"><a href="#--">&amp; &lt; &gt;</a></li>
Expand All @@ -200,7 +200,7 @@ def test_tags_inside_heading
<h2><em>h2</em></h2>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" class="section-nav">
<li class="toc-entry toc-h2"><a href="#h2">h2</a></li>
<li class="toc-entry toc-h2"><a href="#h2-1">h2</a></li>
</ul>
Expand All @@ -219,7 +219,7 @@ def test_nested_toc_with_no_toc_section_class
<h6>h6</h6>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" 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>
Expand Down Expand Up @@ -254,7 +254,7 @@ def test_nested_toc_with_no_toc_section_class_option
<h6>h6</h6>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" 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>
Expand Down Expand Up @@ -291,7 +291,7 @@ def test_multiple_no_toc_section_classes
<h6>h6</h6>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" 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>
Expand Down Expand Up @@ -321,7 +321,7 @@ def test_toc_with_explicit_id
<h1 id="third">h3</h1>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" class="section-nav">
<li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
<li class="toc-entry toc-h1"><a href="#second">h2</a></li>
<li class="toc-entry toc-h1"><a href="#third">h3</a></li>
Expand All @@ -342,7 +342,7 @@ def test_anchor_is_uniq
<h1>h1</h1>
HTML
expected = <<~HTML.chomp
<ul class="section-nav">
<ul id="toc" class="section-nav">
<li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
<li class="toc-entry toc-h1"><a href="#h1-1">h1</a></li>
<li class="toc-entry toc-h1"><a href="#h1-2">h1</a></li>
Expand All @@ -358,7 +358,7 @@ def test_custom_css_classes
'item_class' => 'custom-item', 'list_class' => 'custom-list', 'sublist_class' => 'custom-sublist', 'item_prefix' => 'custom-prefix-'
)
expected = <<~HTML.chomp
<ul class="custom-list">
<ul id="toc" class="custom-list">
<li class="custom-item custom-prefix-h1"><a href="#h1">h1</a>
<ul class="custom-sublist">
<li class="custom-item custom-prefix-h3"><a href="#h3">h3</a>
Expand Down
2 changes: 2 additions & 0 deletions test/test_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def test_default_configuration
assert_equal(1..6, configuration.toc_levels)
refute(configuration.ordered_list)
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)
assert_equal('toc-entry', configuration.item_class)
Expand All @@ -21,6 +22,7 @@ def test_type_error
assert_equal(1..6, configuration.toc_levels)
refute(configuration.ordered_list)
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)
assert_equal('toc-entry', configuration.item_class)
Expand Down
4 changes: 2 additions & 2 deletions test/test_jekyll-toc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_toc

def test_toc_only2
@context = enable_toc_context
assert_equal "<ul class=\"section-nav\">\n</ul>", toc_only(DUMMY_HTML)
assert_equal "<ul id=\"toc\" class=\"section-nav\">\n</ul>", toc_only(DUMMY_HTML)
end

def test_inject_anchors2
Expand All @@ -34,7 +34,7 @@ def test_inject_anchors2

def test_toc2
@context = enable_toc_context
assert_equal "<ul class=\"section-nav\">\n</ul>#{DUMMY_HTML}", toc(DUMMY_HTML)
assert_equal "<ul id=\"toc\" class=\"section-nav\">\n</ul>#{DUMMY_HTML}", toc(DUMMY_HTML)
end

private
Expand Down
2 changes: 1 addition & 1 deletion test/test_toc_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("<ul class=\"section-nav\">\n<li class=\"toc-entry toc-h1\"><a href=\"#test\">test</a></li>\n</ul>", tag.render(context))
assert_equal("<ul id=\"toc\" class=\"section-nav\">\n<li class=\"toc-entry toc-h1\"><a href=\"#test\">test</a></li>\n</ul>", tag.render(context))
end

def test_toc_tag_returns_empty_string
Expand Down