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

Add id to h-elements #109

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b1c34b5
Add configurable list_id for root toc-element
N7K4 Apr 30, 2020
9036248
Fix unit tests (add id="toc")
N7K4 Apr 30, 2020
694e87f
Add config key inject_anchors_content to set the cntent of the anchor
N7K4 Apr 30, 2020
aeea2ba
Add id to h-elements
N7K4 Apr 30, 2020
91a9047
Merge branch 'master' into fix-Octoicon
N7K4 Apr 30, 2020
6376d1e
Merge pull request #1 from N7K4/fix-Octoicon
N7K4 Apr 30, 2020
cf9b870
Merge pull request #2 from N7K4/TagHelementWithId
N7K4 Apr 30, 2020
287391f
Delete coverage.yml
N7K4 Apr 30, 2020
f01c72f
Create test.yml
N7K4 Apr 30, 2020
da34e5d
Delete ci.yml
N7K4 Apr 30, 2020
357f1b3
Delete rubocop.yml
N7K4 Apr 30, 2020
c8977a2
Update test.yml
N7K4 Apr 30, 2020
7503818
Update test_inject_anchors_filter.rb
N7K4 Apr 30, 2020
422c996
Update test_toc_filter.rb
N7K4 Apr 30, 2020
1fc508b
Update test_inject_anchors_filter.rb
N7K4 Apr 30, 2020
3d23f2d
Update test_toc_filter.rb
N7K4 Apr 30, 2020
b9cf2f2
Merge pull request #3 from N7K4/fix-Octoicon
N7K4 Apr 30, 2020
ef3987f
Update test_toc_tag.rb
N7K4 Apr 30, 2020
e48bcad
Update test_inject_anchors_filter.rb
N7K4 Apr 30, 2020
f6c9185
Update test_configuration.rb
N7K4 Apr 30, 2020
9f2993a
Update test_inject_anchors_filter.rb
N7K4 Apr 30, 2020
6d6bf32
Update test_toc_filter.rb
N7K4 Apr 30, 2020
864047a
Update test_various_toc_html.rb
N7K4 Apr 30, 2020
b6cebfe
Fix Unit Tests
N7K4 Apr 30, 2020
8cbd06a
Merge branch 'master' of github.com:N7K4/jekyll-toc
N7K4 Apr 30, 2020
2fd78bd
Update gitignore with packages
N7K4 Apr 30, 2020
9449132
Change default config from unicode to simple text
N7K4 Apr 30, 2020
6b0bf74
Fix Unittests ID in H-element
N7K4 Apr 30, 2020
21fd3d6
Fix UnitTests
N7K4 Apr 30, 2020
77d3b4d
Delete coverage.yml
N7K4 Apr 30, 2020
d339d38
Delete rubocop.yml
N7K4 Apr 30, 2020
1f12d81
Add GitHub Actions
N7K4 Apr 30, 2020
6f0828a
Update .rubocop.yml
N7K4 Apr 30, 2020
03a4829
Merge branch 'master' into TagHelementWithId
N7K4 Apr 30, 2020
03b7a5d
fix rubocop
N7K4 Apr 30, 2020
030ea03
rubocop rules
N7K4 Apr 30, 2020
2178f41
RuboCop::Cop::Style::RedundantInterpolation
N7K4 Apr 30, 2020
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
58 changes: 29 additions & 29 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
name: CI
on: [push]
jobs:
build:
strategy:
matrix:
ruby: [2.4, 2.5, 2.6, 2.7]
gemfile:
- gemfiles/jekyll_4.0.gemfile
- gemfiles/jekyll_3.8.gemfile
- gemfiles/jekyll_3.7.gemfile
exclude:
- ruby: 2.4
gemfile: gemfiles/jekyll_4.0.gemfile
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby ${{ matrix.ruby }}
uses: eregon/use-ruby-action@master
with:
ruby-version: ${{ matrix.ruby }}
- name: bundle install
run: |
gem install bundler
bundle install --jobs 4 --retry 3
- name: Run Test
run: bundle exec rake
name: CI
on: [push]
jobs:
build:
strategy:
matrix:
ruby: [2.4, 2.5, 2.6, 2.7]
gemfile:
- gemfiles/jekyll_4.0.gemfile
- gemfiles/jekyll_3.8.gemfile
- gemfiles/jekyll_3.7.gemfile
exclude:
- ruby: 2.4
gemfile: gemfiles/jekyll_4.0.gemfile
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby ${{ matrix.ruby }}
uses: eregon/use-ruby-action@master
with:
ruby-version: ${{ matrix.ruby }}
- name: bundle install
run: |
gem install bundler
bundle install --jobs 4 --retry 3
- name: Run Test
run: bundle exec rake
44 changes: 22 additions & 22 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
name: Coverage
on: [push]
jobs:
build:
strategy:
matrix:
ruby: [2.6]
runs-on: ubuntu-latest
name: coverage
steps:
- uses: actions/checkout@v2
- name: Set up Ruby ${{ matrix.ruby }}
uses: eregon/use-ruby-action@master
with:
ruby-version: ${{ matrix.ruby }}
- name: bundle install
run: bundle install --jobs 4 --retry 3
- uses: paambaati/[email protected]
env:
CC_TEST_REPORTER_ID: 6b81e393ea6ad38560386f650ea2fb0e57a7beb5e20f8c8364fabee30d5bff07
with:
coverageCommand: bundle exec rake
name: Coverage
on: [push]
jobs:
build:
strategy:
matrix:
ruby: [2.6]
runs-on: ubuntu-latest
name: coverage
steps:
- uses: actions/checkout@v2
- name: Set up Ruby ${{ matrix.ruby }}
uses: eregon/use-ruby-action@master
with:
ruby-version: ${{ matrix.ruby }}
- name: bundle install
run: bundle install --jobs 4 --retry 3
- uses: paambaati/[email protected]
env:
CC_TEST_REPORTER_ID: 6b81e393ea6ad38560386f650ea2fb0e57a7beb5e20f8c8364fabee30d5bff07
with:
coverageCommand: bundle exec rake
38 changes: 19 additions & 19 deletions .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
name: RuboCop
on: [push]
jobs:
build:
strategy:
matrix:
ruby: [2.6]
runs-on: ubuntu-latest
name: rubocop
steps:
- uses: actions/checkout@v2
- name: Set up Ruby ${{ matrix.ruby }}
uses: eregon/use-ruby-action@master
with:
ruby-version: ${{ matrix.ruby }}
- name: bundle install
run: bundle install --jobs 4 --retry 3
- name: Run RuboCop
run: bundle exec rubocop
name: RuboCop
on: [push]
jobs:
build:
strategy:
matrix:
ruby: [2.6]
runs-on: ubuntu-latest
name: rubocop
steps:
- uses: actions/checkout@v2
- name: Set up Ruby ${{ matrix.ruby }}
uses: eregon/use-ruby-action@master
with:
ruby-version: ${{ matrix.ruby }}
- name: bundle install
run: bundle install --jobs 4 --retry 3
- name: Run RuboCop
run: bundle exec rubocop
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: UnitTest
on: [push]
jobs:
build:
strategy:
matrix:
ruby: [2.6]
gemfile:
- gemfiles/jekyll_4.0.gemfile
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby ${{ matrix.ruby }}
uses: eregon/use-ruby-action@master
with:
ruby-version: ${{ matrix.ruby }}
- name: bundle install
run: |
gem install bundler
bundle install --jobs 4 --retry 3
- name: Run Test
run: bundle exec rake
11 changes: 9 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# gems
*.gem
/Gemfile.lock
/.bundle/
/coverage
*.gemfile.lock
/gems/

# tests / test coverage
/coverage

# bundler
/.bundle/
/vendor/bundle/
18 changes: 18 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ AllCops:
Exclude:
- "*.gemspec"
- "gemfiles/*"
- gems/**/*
- node_modules/**/*
- vendor/bundle/**/*
- Rakefile
- Gemfile

Expand All @@ -21,9 +24,24 @@ Style/WordArray:
Naming/FileName:
Enabled: false

Layout/EndOfLine:
Enabled: false

Layout/LineLength:
Enabled: false

Layout/SpaceAroundMethodCallOperator:
Enabled: false

Lint/StructNewOverride:
Enabled: false

Lint/RaiseException:
Enabled: false

Style/ExponentialNotation:
Enabled: false

Style/HashEachMethods:
Enabled: true

Expand Down
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,33 @@ above the content but at some other place of the page, i.e. an aside.

#### `inject_anchors` filter

Injects HTML anchors into the content without actually outputting the TOC itself.
They are of the form:
Injects HTML anchors into the content without actually outputting the TOC itself.

They are of the form (usage of [octicons](https://github.com/primer/octicons) as icon collection):

```html
<a class="anchor" href="#heading1-1" aria-hidden="true">
<span class="octicon octicon-link"></span>
</a>
```

This is only useful when the TOC itself should be placed at some other
location with the `toc_only` filter.
They are of the form (unicode symbol):

```html
<a class="anchor" href="#heading1-1" aria-hidden="true">
&#128279;&nbsp;
</a>
```

This is useful when the TOC itself should be placed at some other location with the `toc_only` filter.

The second usecase is when you want to **share a link** to the page and link directly to the headline / section.

There is a new configuration `inject_anchors_content` key, possible values are (or what you prefer)

* `&#128279;&nbsp;` (default) to add a unicode symbol (chain) for an link
* `<span class="octicon octicon-link"></span>` to use the octicon icon
* ` ` an empty string or a space is for **hide** the anchor

## Generated HTML

Expand Down
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,16 +5,19 @@ module TableOfContents
# jekyll-toc configuration class
class Configuration
attr_accessor :toc_levels, :no_toc_class, :no_toc_section_class,
:list_class, :sublist_class, :item_class, :item_prefix
:list_class, :list_id, :sublist_class, :item_class, :item_prefix,
:inject_anchors_content

DEFAULT_CONFIG = {
'min_level' => 1,
'max_level' => 6,
'no_toc_section_class' => 'no_toc_section',
'list_class' => 'section-nav',
'list_id' => 'toc',
'sublist_class' => '',
'item_class' => 'toc-entry',
'item_prefix' => 'toc-'
'item_prefix' => 'toc-',
'inject_anchors_content' => 'MyLinkAnchor'
}.freeze

def initialize(options)
Expand All @@ -24,9 +27,11 @@ def initialize(options)
@no_toc_class = 'no_toc'
@no_toc_section_class = options['no_toc_section_class']
@list_class = options['list_class']
@list_id = options['list_id']
@sublist_class = options['sublist_class']
@item_class = options['item_class']
@item_prefix = options['item_prefix']
@inject_anchors_content = options['inject_anchors_content']
end

private
Expand Down
8 changes: 6 additions & 2 deletions lib/table_of_contents/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ def toc
end

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

def inject_anchors_into_html
@entries.each do |entry|
# Add id to h-element
entry[:header_parent].set_attribute('id', entry[:id].to_s)

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

Expand Down Expand Up @@ -53,6 +56,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
}
Expand Down
46 changes: 23 additions & 23 deletions test/parser/test_inject_anchors_filter.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# frozen_string_literal: true

require 'test_helper'

class TestInjectAnchorsFilter < Minitest::Test
include TestHelpers

def setup
read_html_and_create_parser
end

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)
end

def test_does_not_inject_toc
html = @parser.inject_anchors_into_html

assert_nil(/<ul class="section-nav">/ =~ html)
end
end
# frozen_string_literal: true
require 'test_helper'
class TestInjectAnchorsFilter < Minitest::Test
include TestHelpers
def setup
read_html_and_create_parser
end
def test_injects_anchors_into_content
html = @parser.inject_anchors_into_html
assert_match(%r{<a class="anchor" href="#simple-h1" aria-hidden="true">MyLinkAnchor</a>Simple H1}, html)
end
def test_does_not_inject_toc
html = @parser.inject_anchors_into_html
assert_nil(/<ul id="toc" class="section-nav">/ =~ html)
end
end
Loading