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

Replace octicon element with a static unicode symbol #130

Open
wants to merge 3 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
2 changes: 2 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 Down
6 changes: 4 additions & 2 deletions lib/table_of_contents/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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_class, :sublist_class, :item_class, :item_prefix, :anchor_symbol

DEFAULT_CONFIG = {
'min_level' => 1,
Expand All @@ -15,7 +15,8 @@ class Configuration
'list_class' => 'section-nav',
'sublist_class' => '',
'item_class' => 'toc-entry',
'item_prefix' => 'toc-'
'item_prefix' => 'toc-',
'anchor_symbol' => ' 🔗' # with leading space
}.freeze

def initialize(options)
Expand All @@ -29,6 +30,7 @@ def initialize(options)
@sublist_class = options['sublist_class']
@item_class = options['item_class']
@item_prefix = options['item_prefix']
@anchor_symbol = options['anchor_symbol']
end

private
Expand Down
10 changes: 8 additions & 2 deletions lib/table_of_contents/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,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(
%(<a class="anchor" href="##{entry[:id]}" aria-hidden="true"><span class="octicon octicon-link"></span></a>)

# entry[:header_content].add_previous_sibling(
# %(<a class="anchor" href="##{entry[:id]}" aria-hidden="true"><span class="octicon octicon-link"></span></a>)
# )

# Add link icon after text
entry[:header_content].add_next_sibling(
%(<a class="anchor" href="##{entry[:id]}"aria-hidden="true">#{@configuration.anchor_symbol}</a>)
)
end

Expand Down
3 changes: 2 additions & 1 deletion test/parser/test_inject_anchors_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def setup
def test_injects_anchors_into_content
html = @parser.inject_anchors_into_html

assert_match(%r{<a class="anchor" href="#simple-h1" aria-hidden="true"><span.*span></a>Simple H1}, html)
# assert_match(%r{<a class="anchor" href="#simple-h1" aria-hidden="true"><span.*span></a>Simple H1}, html)
assert_match(%r{Simple H1<a class="anchor" href="#simple-h1" aria-hidden="true"> 🔗</a>}, html)
end

def test_does_not_inject_toc
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 @@ -12,7 +12,7 @@ def setup
def test_injects_anchors
html = @parser.toc

assert_match(%r{<a class="anchor" href="#simple-h1" aria-hidden="true"><span.*span></a>Simple H1}, html)
assert_match(%r{Simple H1<a class="anchor" href="#simple-h1" aria-hidden="true"> 🔗</a>}, html)
end

def test_nested_toc
Expand Down
6 changes: 3 additions & 3 deletions test/parser/test_various_toc_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ def test_japanese_toc

assert_equal(expected, parser.build_toc)
html_with_anchors = parser.inject_anchors_into_html
assert_match(%r{<a class="anchor" href="#%E3%81%82" aria-hidden="true"><span.*span></a>}, html_with_anchors)
assert_match(%r{<a class="anchor" href="#%E3%81%84" aria-hidden="true"><span.*span></a>}, html_with_anchors)
assert_match(%r{<a class="anchor" href="#%E3%81%86" aria-hidden="true"><span.*span></a>}, html_with_anchors)
assert_match(%r{<a class="anchor" href="#%E3%81%82" aria-hidden="true"> 🔗</a>}, html_with_anchors)
assert_match(%r{<a class="anchor" href="#%E3%81%84" aria-hidden="true"> 🔗</a>}, html_with_anchors)
assert_match(%r{<a class="anchor" href="#%E3%81%86" aria-hidden="true"> 🔗</a>}, html_with_anchors)
end

# ref. https://github.com/toshimaru/jekyll-toc/issues/45
Expand Down