diff --git a/.github/dependabot.yml b/.github/dependabot.yml
new file mode 100644
index 0000000..18acbfa
--- /dev/null
+++ b/.github/dependabot.yml
@@ -0,0 +1,19 @@
+# To get started with Dependabot version updates, you'll need to specify which
+# package ecosystems to update and where the package manifests are located.
+# Please see the documentation for all configuration options:
+# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
+
+version: 2
+updates:
+ - package-ecosystem: "bundler"
+ directory: "/"
+ schedule:
+ interval: "weekly"
+ reviewers:
+ - toshimaru
+ - package-ecosystem: "github-actions"
+ directory: "/"
+ schedule:
+ interval: "weekly"
+ reviewers:
+ - toshimaru
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bf76132..1f8aa18 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1,29 +1,31 @@
name: CI
-on: [push]
+
+on:
+ push:
+ branches:
+ - main
+ pull_request:
+
jobs:
- build:
+ test:
strategy:
matrix:
- ruby: [2.4, 2.5, 2.6, 2.7]
+ ruby: ['2.6', '2.7', '3.0', '3.1', '3.2']
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
+ - gemfiles/jekyll_3.9.gemfile
+ - gemfiles/jekyll_4.0.gemfile
+ - gemfiles/jekyll_4.1.gemfile
+ - gemfiles/jekyll_4.2.gemfile
+ - gemfiles/jekyll_4.3.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
+ - uses: actions/checkout@v3
+ - name: Set up Ruby ${{ matrix.ruby }}
+ uses: ruby/setup-ruby@v1
+ with:
+ ruby-version: ${{ matrix.ruby }}
+ bundler-cache: true
+ - name: Run Test
+ run: bundle exec rake
diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml
index 4d676d7..3ef8da8 100644
--- a/.github/workflows/coverage.yml
+++ b/.github/workflows/coverage.yml
@@ -1,21 +1,22 @@
name: Coverage
-on: [push]
+
+on:
+ push:
+ branches:
+ - main
+ pull_request:
+
jobs:
- build:
- strategy:
- matrix:
- ruby: [2.6]
+ coverage:
runs-on: ubuntu-latest
- name: coverage
steps:
- - uses: actions/checkout@v2
- - name: Set up Ruby ${{ matrix.ruby }}
- uses: eregon/use-ruby-action@master
+ - uses: actions/checkout@v3
+ - name: Set up Ruby
+ uses: ruby/setup-ruby@v1
with:
- ruby-version: ${{ matrix.ruby }}
- - name: bundle install
- run: bundle install --jobs 4 --retry 3
- - uses: paambaati/codeclimate-action@v2.4.0
+ ruby-version: 3.2
+ bundler-cache: true
+ - uses: paambaati/codeclimate-action@v3.2.0
env:
CC_TEST_REPORTER_ID: 6b81e393ea6ad38560386f650ea2fb0e57a7beb5e20f8c8364fabee30d5bff07
with:
diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml
index 75338b2..a02a0a5 100644
--- a/.github/workflows/rubocop.yml
+++ b/.github/workflows/rubocop.yml
@@ -1,19 +1,20 @@
name: RuboCop
-on: [push]
+
+on:
+ push:
+ branches:
+ - main
+ pull_request:
+
jobs:
- build:
- strategy:
- matrix:
- ruby: [2.6]
+ rubocop:
runs-on: ubuntu-latest
- name: rubocop
steps:
- - uses: actions/checkout@v2
- - name: Set up Ruby ${{ matrix.ruby }}
- uses: eregon/use-ruby-action@master
+ - uses: actions/checkout@v3
+ - name: Set up Ruby
+ uses: ruby/setup-ruby@v1
with:
- ruby-version: ${{ matrix.ruby }}
- - name: bundle install
- run: bundle install --jobs 4 --retry 3
+ ruby-version: 3.2
+ bundler-cache: true
- name: Run RuboCop
run: bundle exec rubocop
diff --git a/.rubocop.yml b/.rubocop.yml
index 523762b..c753fcc 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -1,34 +1,43 @@
AllCops:
- TargetRubyVersion: 2.4
+ TargetRubyVersion: 2.6
+ NewCops: enable
Exclude:
- "*.gemspec"
- "gemfiles/*"
+ - "vendor/**/*"
- Rakefile
- Gemfile
+require:
+ - rubocop-performance
+ - rubocop-minitest
Metrics/MethodLength:
Enabled: false
-
Metrics/AbcSize:
Enabled: false
-
Metrics/ClassLength:
Enabled: false
-Style/WordArray:
- Enabled: false
-
Naming/FileName:
Enabled: false
Layout/LineLength:
Enabled: false
+Layout/SpaceAroundMethodCallOperator:
+ Enabled: true
-Style/HashEachMethods:
+Lint/RaiseException:
+ Enabled: true
+Lint/StructNewOverride:
Enabled: true
+Style/WordArray:
+ Enabled: false
+Style/HashEachMethods:
+ Enabled: true
Style/HashTransformKeys:
Enabled: true
-
Style/HashTransformValues:
Enabled: true
+Style/ExponentialNotation:
+ Enabled: true
diff --git a/Appraisals b/Appraisals
index b84be41..5f93642 100644
--- a/Appraisals
+++ b/Appraisals
@@ -1,13 +1,9 @@
# frozen_string_literal: true
-appraise 'jekyll-4.0' do
- gem 'jekyll', '4.0'
-end
-
-appraise 'jekyll-3.8' do
- gem 'jekyll', '3.8'
-end
+SUPPORTED_VERSIONS = %w[3.9 4.0 4.1 4.2 4.3].freeze
-appraise 'jekyll-3.7' do
- gem 'jekyll', '3.7'
+SUPPORTED_VERSIONS.each do |version|
+ appraise "jekyll-#{version}" do
+ gem 'jekyll', version
+ end
end
diff --git a/Gemfile b/Gemfile
index b6a433a..2d2687b 100644
--- a/Gemfile
+++ b/Gemfile
@@ -7,5 +7,8 @@ gem 'minitest-reporters'
gem 'minitest'
gem 'pry'
gem 'rake'
+gem 'rubocop-minitest'
+gem 'rubocop-performance'
+gem 'rubocop-rake'
gem 'rubocop'
-gem 'simplecov', '~> 0.17.1'
+gem 'simplecov', '~> 0.22.0'
diff --git a/LICENSE.md b/LICENSE.md
index 5763175..4b99c06 100644
--- a/LICENSE.md
+++ b/LICENSE.md
@@ -1,6 +1,6 @@
The MIT License (MIT)
-Copyright (c) 2020 Toshimaru
+Copyright (c) 2020-2021 Toshimaru
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
diff --git a/README.md b/README.md
index e85fc30..43012cb 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
# jekyll-toc

-[](http://badge.fury.io/rb/jekyll-toc)
+[](https://badge.fury.io/rb/jekyll-toc)
[](https://codeclimate.com/github/toshimaru/jekyll-toc)
[](https://codeclimate.com/github/toshimaru/jekyll-toc/test_coverage)
@@ -9,16 +9,19 @@
- [Installation](#installation)
- [Usage](#usage)
- - [1. Basic Usage](#1-basic-usage)
- - [2. Advanced Usage](#2-advanced-usage)
+ - [Basic Usage](#basic-usage)
+ - [Advanced Usage](#advanced-usage)
- [Generated HTML](#generated-html)
-- [Default Configuration](#default-configuration)
- [Customization](#customization)
+ - [Default Configuration](#default-configuration)
- [TOC levels](#toc-levels)
+ - [Enable TOC by default](#enable-toc-by-default)
- [Skip TOC](#skip-toc)
- - [Skip TOC Section](#skip-toc-section)
+ - [Skip TOC Sectionally](#skip-toc-sectionally)
- [CSS Styling](#css-styling)
- - [Custom CSS Class](#custom-css-class)
+ - [Custom CSS Class and ID](#custom-css-class-and-id)
+ - [Using Unordered/Ordered lists](#using-unorderedordered-lists)
+- [Alternative Tools](#alternative-tools)
## Installation
@@ -50,7 +53,7 @@ toc: true
There are three Liquid filters, which can be applied to HTML content,
e.g. the Liquid variable `content` available in Jekyll's templates.
-### 1. Basic Usage
+### Basic Usage
#### `toc` filter
@@ -62,11 +65,11 @@ Add the `toc` filter to your site's `{{ content }}` (e.g. `_layouts/post.html`).
This filter places the TOC directly above the content.
-### 2. Advanced Usage
+### Advanced Usage
If you'd like separated TOC and content, you can use `{% toc %}` tag (or `toc_only` filter) and `inject_anchors` filter.
-#### `{% toc %}` tag
+#### `{% toc %}` tag / `toc_only` filter
Generates the TOC itself as described [below](#generated-html).
Mostly useful in cases where the TOC should _not_ be placed immediately
@@ -85,7 +88,8 @@ above the content but at some other place of the page, i.e. an aside.
:warning: **`{% toc %}` Tag Limitation**
-`{% toc %}` can be available only in [Jekyll Posts](https://jekyllrb.com/docs/step-by-step/08-blogging/) and [Jekyll Collections](https://jekyllrb.com/docs/collections/). If you'd like to use `{% toc %}` except posts or collections, please use `toc_only` filter as described below.
+`{% toc %}` works only for [Jekyll Posts](https://jekyllrb.com/docs/step-by-step/08-blogging/) and [Jekyll Collections](https://jekyllrb.com/docs/collections/).
+If you'd like to use `{% toc %}` except posts or collections, please use `toc_only` filter as described below.
```html
@@ -114,10 +118,10 @@ location with the `toc_only` filter.
## Generated HTML
-jekyll-toc generates an unordered list. The HTML output is as follows.
+jekyll-toc generates an unordered list by default. The HTML output is as follows.
```html
-
+
- Heading.1
- Heading.1-1
@@ -140,52 +144,69 @@ jekyll-toc generates an unordered list. The HTML output is as follows.

-## Default Configuration
+## Customization
+
+jekyll-toc is customizable on `_config.yml`.
+
+### Default Configuration
```yml
+# _config.yml
toc:
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-
```
-## Customization
-
### TOC levels
-The toc levels can be configured on `_config.yml`:
-
```yml
+# _config.yml
toc:
min_level: 2 # default: 1
max_level: 5 # default: 6
```
-The default level range is `` to ``.
+The default heading range is from `` to ``.
+
+### Enable TOC by default
+
+You can enable TOC by default with [Front Matter Defaults](https://jekyllrb.com/docs/configuration/front-matter-defaults/):
+
+```yml
+# _config.yml
+defaults:
+ - scope:
+ path: ""
+ values:
+ toc: true
+```
### Skip TOC
-The heading is ignored in the toc when you add `no_toc` to the class.
+The heading is ignored in the toc by adding `no_toc` class.
```html
h1
-This heading is ignored in the toc
+This heading is ignored in the TOC
h2
```
-### Skip TOC Section
+### Skip TOC Sectionally
The headings are ignored inside the element which has `no_toc_section` class.
```html
h1
-
This heading is ignored in the toc
- This heading is ignored in the toc
+ This heading is ignored in the TOC
+ This heading is ignored in the TOC
h4
```
@@ -195,13 +216,15 @@ Which would result in only the `` & `` within the example being included
The class can be configured on `_config.yml`:
```yml
+# _config.yml
toc:
no_toc_section_class: exclude # default: no_toc_section
```
-Configuring multiple classes for `no_toc_section_class` is allowed:
+Configuring multiple classes are allowed:
```yml
+# _config.yml
toc:
no_toc_section_class:
- no_toc_section
@@ -227,21 +250,64 @@ The toc can be modified with CSS. The sample CSS is the following.
Each TOC `li` entry has two CSS classes for further styling. The general `toc-entry` is applied to all `li` elements in the `ul.section-nav`.
-Depending on the heading level each specific entry refers to, it has a second CSS class `toc-XX`, where `XX` is the HTML heading tag name. For example, the TOC entry linking to a heading `...
` (a single
-`#` in Markdown) will get the CSS class `toc-h1`.
+Depending on the heading level each specific entry refers to, it has a second CSS class `toc-XX`, where `XX` is the HTML heading tag name.
+For example, the TOC entry linking to a heading `...
` (a single `#` in Markdown) will get the CSS class `toc-h1`.
-### Custom CSS Class
+### Custom CSS Class and ID
You can apply custom CSS classes to the generated `` and `- ` tags.
```yml
+# _config.yml
+toc:
+ list_id: my-toc-id # Default: "toc"
+ list_class: my-list-class # Default: "section-nav"
+ sublist_class: my-sublist-class # Default: no class for sublists
+ item_class: my-item-class # Default: "toc-entry"
+ item_prefix: item- # Default: "toc-":
+```
+
+### Using Unordered/Ordered lists
+
+By default the table of contents will be generated as an unordered list via `` tags. This can be configured to use ordered lists instead `
`.
+This can be configured in `_config.yml`:
+
+```yml
+# _config.yml
toc:
- # Default is "section-nav":
+ ordered_list: true # default is false
+```
+
+In order to change the list type, use the [list-style-type](https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type) css property.
+Add a class to the `sublist_class` configuration to append it to the `ol` tags so that you can add the `list-style-type` property.
+
+Example
+
+```yml
+# _config.yml
+toc:
+ ordered_list: true
list_class: my-list-class
- # Default is no class for sublists:
sublist_class: my-sublist-class
- # Default is "toc-entry":
- item_class: my-item-class
- # Default is "toc-":
- item_prefix: item-
```
+
+```css
+.my-list-class {
+ list-style-type: upper-alpha;
+}
+
+.my-sublist-class: {
+ list-style-type: lower-alpha;
+}
+```
+
+This will produce:
+
+
+
+## Alternative Tools
+
+- Adding anchor to headings
+ - [AnchorJS](https://www.bryanbraun.com/anchorjs/)
+- Generating TOC for kramdown content
+ - [Automatic “Table of Contents” Generation](https://kramdown.gettalong.org/converter/html.html#toc) (See also. [Create Table of Contents in kramdown](https://blog.toshima.ru/2020/05/22/kramdown-toc))
diff --git a/Rakefile b/Rakefile
index d7278cc..7a0b6c4 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,6 +1,6 @@
require "bundler/gem_tasks"
-task :default => [:test]
+task default: :test
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
diff --git a/gemfiles/jekyll_3.7.gemfile b/gemfiles/jekyll_3.9.gemfile
similarity index 61%
rename from gemfiles/jekyll_3.7.gemfile
rename to gemfiles/jekyll_3.9.gemfile
index 936d10a..b39050a 100644
--- a/gemfiles/jekyll_3.7.gemfile
+++ b/gemfiles/jekyll_3.9.gemfile
@@ -7,8 +7,11 @@ gem "minitest-reporters"
gem "minitest"
gem "pry"
gem "rake"
+gem "rubocop-minitest"
+gem "rubocop-performance"
+gem "rubocop-rake"
gem "rubocop"
-gem "simplecov"
-gem "jekyll", "3.7"
+gem "simplecov", "~> 0.22.0"
+gem "jekyll", "3.9"
gemspec path: "../"
diff --git a/gemfiles/jekyll_4.0.gemfile b/gemfiles/jekyll_4.0.gemfile
index e6ba5d2..c2309b9 100644
--- a/gemfiles/jekyll_4.0.gemfile
+++ b/gemfiles/jekyll_4.0.gemfile
@@ -7,8 +7,11 @@ gem "minitest-reporters"
gem "minitest"
gem "pry"
gem "rake"
+gem "rubocop-minitest"
+gem "rubocop-performance"
+gem "rubocop-rake"
gem "rubocop"
-gem "simplecov"
+gem "simplecov", "~> 0.22.0"
gem "jekyll", "4.0"
gemspec path: "../"
diff --git a/gemfiles/jekyll_3.8.gemfile b/gemfiles/jekyll_4.1.gemfile
similarity index 61%
rename from gemfiles/jekyll_3.8.gemfile
rename to gemfiles/jekyll_4.1.gemfile
index 91a026f..18ad983 100644
--- a/gemfiles/jekyll_3.8.gemfile
+++ b/gemfiles/jekyll_4.1.gemfile
@@ -7,8 +7,11 @@ gem "minitest-reporters"
gem "minitest"
gem "pry"
gem "rake"
+gem "rubocop-minitest"
+gem "rubocop-performance"
+gem "rubocop-rake"
gem "rubocop"
-gem "simplecov"
-gem "jekyll", "3.8"
+gem "simplecov", "~> 0.22.0"
+gem "jekyll", "4.1"
gemspec path: "../"
diff --git a/gemfiles/jekyll_4.2.gemfile b/gemfiles/jekyll_4.2.gemfile
new file mode 100644
index 0000000..25ab912
--- /dev/null
+++ b/gemfiles/jekyll_4.2.gemfile
@@ -0,0 +1,17 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "appraisal"
+gem "minitest-reporters"
+gem "minitest"
+gem "pry"
+gem "rake"
+gem "rubocop-minitest"
+gem "rubocop-performance"
+gem "rubocop-rake"
+gem "rubocop"
+gem "simplecov", "~> 0.22.0"
+gem "jekyll", "4.2"
+
+gemspec path: "../"
diff --git a/gemfiles/jekyll_4.3.gemfile b/gemfiles/jekyll_4.3.gemfile
new file mode 100644
index 0000000..dba97ac
--- /dev/null
+++ b/gemfiles/jekyll_4.3.gemfile
@@ -0,0 +1,17 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "appraisal"
+gem "minitest-reporters"
+gem "minitest"
+gem "pry"
+gem "rake"
+gem "rubocop-minitest"
+gem "rubocop-performance"
+gem "rubocop-rake"
+gem "rubocop"
+gem "simplecov", "~> 0.22.0"
+gem "jekyll", "4.3"
+
+gemspec path: "../"
diff --git a/jekyll-toc.gemspec b/jekyll-toc.gemspec
index c687805..0540ccb 100644
--- a/jekyll-toc.gemspec
+++ b/jekyll-toc.gemspec
@@ -1,24 +1,33 @@
# frozen_string_literal: true
-lib = File.expand_path('../lib', __FILE__)
-$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
-require 'version'
+require_relative 'lib/table_of_contents/version'
Gem::Specification.new do |spec|
spec.name = 'jekyll-toc'
- spec.version = JekyllToc::VERSION
+ spec.version = Jekyll::TableOfContents::VERSION
spec.summary = 'Jekyll Table of Contents plugin'
- spec.description = 'Jekyll (Ruby static website generator) plugin which generates a table of contents.'
- spec.authors = %w(toshimaru torbjoernk)
+ spec.description = 'Jekyll (Ruby static website generator) plugin which generates a Table of Contents for the page.'
+ spec.authors = %w[toshimaru torbjoernk]
spec.email = 'me@toshimaru.net'
- spec.files = `git ls-files -z`.split("\x0")
spec.homepage = 'https://github.com/toshimaru/jekyll-toc'
spec.license = 'MIT'
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
- spec.required_ruby_version = '>= 2.4'
+ spec.metadata['homepage_uri'] = spec.homepage
+ spec.metadata['source_code_uri'] = 'https://github.com/toshimaru/jekyll-toc'
+ spec.metadata['changelog_uri'] = 'https://github.com/toshimaru/jekyll-toc/releases'
+ spec.metadata['rubygems_mfa_required'] = 'true'
- spec.add_dependency 'jekyll', '>= 3.7'
- spec.add_dependency 'nokogiri', '~> 1.9'
+ # Specify which files should be added to the gem when it is released.
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
+ spec.files = Dir.chdir(__dir__) do
+ `git ls-files -z`.split("\x0").reject do |f|
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
+ end
+ end
+
+ spec.required_ruby_version = '>= 2.6'
+
+ spec.add_dependency 'jekyll', '>= 3.9'
+ spec.add_dependency 'nokogiri', '~> 1.12'
end
diff --git a/lib/table_of_contents/configuration.rb b/lib/table_of_contents/configuration.rb
index 61eeeeb..595c73d 100644
--- a/lib/table_of_contents/configuration.rb
+++ b/lib/table_of_contents/configuration.rb
@@ -4,13 +4,15 @@ module Jekyll
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
+ attr_reader :toc_levels, :no_toc_class, :ordered_list, :no_toc_section_class,
+ :list_id, :list_class, :sublist_class, :item_class, :item_prefix
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',
@@ -21,8 +23,10 @@ def initialize(options)
options = generate_option_hash(options)
@toc_levels = options['min_level']..options['max_level']
+ @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']
diff --git a/lib/table_of_contents/helper.rb b/lib/table_of_contents/helper.rb
new file mode 100644
index 0000000..8ed9118
--- /dev/null
+++ b/lib/table_of_contents/helper.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+module Jekyll
+ module TableOfContents
+ # helper methods for Parser
+ module Helper
+ PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u.freeze
+
+ def generate_toc_id(text)
+ text = text.downcase
+ .gsub(PUNCTUATION_REGEXP, '') # remove punctuation
+ .tr(' ', '-') # replace spaces with dash
+ CGI.escape(text)
+ end
+ end
+ end
+end
diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb
index 036251d..23509cc 100644
--- a/lib/table_of_contents/parser.rb
+++ b/lib/table_of_contents/parser.rb
@@ -1,10 +1,12 @@
# frozen_string_literal: true
+require 'table_of_contents/helper'
+
module Jekyll
module TableOfContents
# Parse html contents and generate table of contents
class Parser
- PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u.freeze
+ include ::Jekyll::TableOfContents::Helper
def initialize(html, options = {})
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
@@ -17,11 +19,12 @@ def toc
end
def build_toc
- %(\n#{build_toc_list(@entries)}
)
+ %(<#{list_tag} id="#{@configuration.list_id}" class="#{@configuration.list_class}">\n#{build_toc_list(@entries)}#{list_tag}>)
end
def inject_anchors_into_html
@entries.each do |entry|
+ # NOTE: `entry[:id]` is automatically URL encoded by Nokogiri
entry[:header_content].add_previous_sibling(
%()
)
@@ -41,10 +44,7 @@ def parse_content
.reject { |n| n.classes.include?(@configuration.no_toc_class) }
.inject([]) do |entries, node|
text = node.text
- id = node.attribute('id') || text
- .downcase
- .gsub(PUNCTUATION_REGEXP, '') # remove punctuation
- .tr(' ', '-') # replace spaces with dash
+ id = node.attribute('id') || generate_toc_id(text)
suffix_num = headers[id]
headers[id] += 1
@@ -74,7 +74,7 @@ def build_toc_list(entries)
next_i = i + 1
if next_i < entries.count && entries[next_i][:h_num] > min_h_num
nest_entries = get_nest_entries(entries[next_i, entries.count], min_h_num)
- toc_list << %(\n\n#{build_toc_list(nest_entries)}
\n)
+ toc_list << %(\n<#{list_tag}#{ul_attributes}>\n#{build_toc_list(nest_entries)}#{list_tag}>\n)
i += nest_entries.count
end
# Add the closing tag for the current entry in the list
@@ -107,7 +107,7 @@ def toc_headings
end
def toc_headings_in_no_toc_section
- if @configuration.no_toc_section_class.is_a? Array
+ if @configuration.no_toc_section_class.is_a?(Array)
@configuration.no_toc_section_class.map { |cls| toc_headings_within(cls) }.join(',')
else
toc_headings_within(@configuration.no_toc_section_class)
@@ -121,6 +121,10 @@ def toc_headings_within(class_name)
def ul_attributes
@ul_attributes ||= @configuration.sublist_class.empty? ? '' : %( class="#{@configuration.sublist_class}")
end
+
+ def list_tag
+ @list_tag ||= @configuration.ordered_list ? 'ol' : 'ul'
+ end
end
end
end
diff --git a/lib/table_of_contents/version.rb b/lib/table_of_contents/version.rb
new file mode 100644
index 0000000..1c4efad
--- /dev/null
+++ b/lib/table_of_contents/version.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+module Jekyll
+ module TableOfContents
+ VERSION = '0.17.1'
+ end
+end
diff --git a/lib/version.rb b/lib/version.rb
deleted file mode 100644
index 6a935cf..0000000
--- a/lib/version.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-# frozen_string_literal: true
-
-module JekyllToc
- VERSION = '0.13.1'
-end
diff --git a/test/parser/test_inject_anchors_filter.rb b/test/parser/test_inject_anchors_filter.rb
index c9158e5..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
- assert_nil(// =~ html)
+ refute_includes(html, %())
end
end
diff --git a/test/parser/test_invalid_options.rb b/test/parser/test_invalid_options.rb
new file mode 100644
index 0000000..2e73d14
--- /dev/null
+++ b/test/parser/test_invalid_options.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+require 'test_helper'
+
+class TestInvalidOptions < Minitest::Test
+ BASE_HTML = 'h1
'
+ EXPECTED_HTML = <<~HTML.chomp
+
+ HTML
+
+ def test_option_is_nil
+ parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, nil)
+
+ assert_equal(EXPECTED_HTML, parser.build_toc)
+ end
+
+ def test_option_is_epmty_string
+ parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, '')
+
+ assert_equal(EXPECTED_HTML, parser.build_toc)
+ end
+
+ def test_option_is_string
+ parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, 'string')
+
+ assert_equal(EXPECTED_HTML, parser.build_toc)
+ end
+
+ def test_option_is_array
+ parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, [])
+
+ assert_equal(EXPECTED_HTML, parser.build_toc)
+ end
+end
diff --git a/test/parser/test_option_error.rb b/test/parser/test_option_error.rb
deleted file mode 100644
index c469e29..0000000
--- a/test/parser/test_option_error.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-# frozen_string_literal: true
-
-require 'test_helper'
-
-class TestOptionError < Minitest::Test
- BASE_HTML = 'h1
'
- EXPECTED_HTML = <<~HTML
-
- HTML
-
- def test_option_is_nil
- parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, nil)
- doc = Nokogiri::HTML(parser.toc)
- expected = EXPECTED_HTML
- assert_equal(expected, doc.css('ul.section-nav').to_s)
- end
-
- def test_option_is_epmty_string
- parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, '')
- doc = Nokogiri::HTML(parser.toc)
- expected = EXPECTED_HTML
- assert_equal(expected, doc.css('ul.section-nav').to_s)
- end
-
- def test_option_is_string
- parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, 'string')
- doc = Nokogiri::HTML(parser.toc)
- expected = EXPECTED_HTML
- assert_equal(expected, doc.css('ul.section-nav').to_s)
- end
-
- def test_option_is_array
- parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, [])
- doc = Nokogiri::HTML(parser.toc)
- expected = EXPECTED_HTML
- assert_equal(expected, doc.css('ul.section-nav').to_s)
- end
-end
diff --git a/test/parser/test_ordered_list.rb b/test/parser/test_ordered_list.rb
new file mode 100644
index 0000000..98af378
--- /dev/null
+++ b/test/parser/test_ordered_list.rb
@@ -0,0 +1,76 @@
+# frozen_string_literal: true
+
+require 'test_helper'
+
+class TestOrderedList < Minitest::Test
+ include TestHelpers
+
+ def test_default_configuration
+ configuration = Jekyll::TableOfContents::Configuration.new({})
+
+ refute(configuration.ordered_list)
+ end
+
+ def test_disabled_ordered_list
+ configuration = Jekyll::TableOfContents::Configuration.new('ordered_list' => false)
+
+ refute(configuration.ordered_list)
+ end
+
+ def test_enabled_ordered_list
+ configuration = Jekyll::TableOfContents::Configuration.new('ordered_list' => true)
+
+ assert(configuration.ordered_list)
+ end
+
+ def test_basic_ordered_list_top_heading
+ parse_with_ordered_list
+ html = @parser.toc
+
+ assert_match(/^/, html)
+ end
+
+ def test_ordered_list_sub_headings
+ parse_with_ordered_list
+ html = @parser.toc
+
+ assert_match(/\n- /, html)
+ end
+
+ def test_ordered_list_sub_headings_with_classes
+ parse_with_ordered_list_and_classes
+ html = @parser.toc
+
+ assert_match(/
/, html)
+ end
+
+ def test_ordered_list_subheadings_with_classes_nested_structure
+ parse_with_ordered_list_and_classes
+ html = @parser.toc
+
+ occurrences = html.scan(//).count
+
+ assert_equal(5, occurrences)
+ end
+
+ private
+
+ def parse_with_ordered_list
+ read_html_and_create_parser('ordered_list' => true)
+ end
+
+ def parse_with_ordered_list_and_classes
+ read_html_and_create_parser(
+ 'ordered_list' => true,
+ 'list_class' => 'top-list-class',
+ 'sublist_class' => 'sublist-class'
+ )
+ end
+end
diff --git a/test/parser/test_toc_filter.rb b/test/parser/test_toc_filter.rb
index a2ddd59..6fe2fb7 100644
--- a/test/parser/test_toc_filter.rb
+++ b/test/parser/test_toc_filter.rb
@@ -25,12 +25,13 @@ def test_nested_toc
.css('li.toc-h5')
.css('li.toc-h6')
.text
+
assert_equal('Simple H6', nested_h6_text)
end
def test_injects_toc_container
html = @parser.toc
- assert_match(//, html)
+ assert_match(//, html)
end
end
diff --git a/test/parser/test_toc_only_filter.rb b/test/parser/test_toc_only_filter.rb
index cc0b9eb..128c3fb 100644
--- a/test/parser/test_toc_only_filter.rb
+++ b/test/parser/test_toc_only_filter.rb
@@ -12,12 +12,12 @@ def setup
def test_injects_toc_container
html = @parser.build_toc
- assert_match(//, html)
+ assert_includes(html, %())
end
def test_does_not_return_content
html = @parser.build_toc
- assert_nil(%r{Simple H1
} =~ html)
+ refute_includes(html, %(Simple H1
))
end
end
diff --git a/test/parser/test_various_toc_html.rb b/test/parser/test_various_toc_html.rb
index 2668b31..9fdd221 100644
--- a/test/parser/test_various_toc_html.rb
+++ b/test/parser/test_various_toc_html.rb
@@ -3,75 +3,19 @@
require 'test_helper'
class TestVariousTocHtml < Minitest::Test
- # ref. https://github.com/toshimaru/jekyll-toc/issues/45
- ANGLE_BRACKET_HTML = <<~HTML
- h1
- <base href>
- & < >
- HTML
-
- NO_TOC_HTML = <<~HTML
- h1
- no_toc h1
- h2
- no_toc h2
- h3
- no_toc h3
- h4
- no_toc h4
- HTML
-
- JAPANESE_HEADINGS_HTML = <<~HTML
- あ
- い
- う
- HTML
-
- TAGS_INSIDE_HEADINGS_HTML = <<~HTML
- h2
- h2
- HTML
-
- TEST_HTML_1 = <<~HTML
- h1
- h3
- h6
- HTML
-
- TEST_HTML_2 = <<~HTML
+ TEST_HTML = <<~HTML
h1
h3
- h2
h6
HTML
- TEST_HTML_3 = <<~HTML
- h6
- h5
- h4
- h3
- h2
- h1
- HTML
-
- TEST_HTML_4 = <<~HTML
- h1
- h3
- h2
- h4
- h5
- HTML
-
def test_nested_toc
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_1)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
- -
- h1
+ parser = Jekyll::TableOfContents::Parser.new(TEST_HTML)
+ expected = <<~HTML.chomp
+
+ - h1
- -
- h3
+
- h3
@@ -80,35 +24,34 @@ def test_nested_toc
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
end
def test_nested_toc_with_min_and_max
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_1, 'min_level' => 2, 'max_level' => 5)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
+ parser = Jekyll::TableOfContents::Parser.new(TEST_HTML, 'min_level' => 2, 'max_level' => 5)
+ expected = <<~HTML.chomp
+
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
end
def test_complex_nested_toc
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_2)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
- -
- h1
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
+
h1
+ h3
+ h2
+ h6
+ HTML
+ expected = <<~HTML.chomp
+
+ - h1
- h3
- -
- h2
+
- h2
@@ -117,16 +60,21 @@ def test_complex_nested_toc
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
end
def test_decremental_headings1
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_3)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
+ h6
+ h5
+ h4
+ h3
+ h2
+ h1
+ HTML
+ expected = <<~HTML.chomp
+
- h6
- h5
- h4
@@ -135,25 +83,26 @@ def test_decremental_headings1
- h1
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
end
def test_decremental_headings2
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_4)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
- -
- h1
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
+
h1
+ h3
+ h2
+ h4
+ h5
+ HTML
+ expected = <<~HTML.chomp
+
+ - h1
- h3
- -
- h2
+
- h2
- -
- h4
+
- h4
@@ -165,22 +114,27 @@ def test_decremental_headings2
HTML
- assert_equal(expected, doc.css('ul.section-nav').to_s)
+ assert_equal(expected, parser.build_toc)
end
def test_no_toc
- parser = Jekyll::TableOfContents::Parser.new(NO_TOC_HTML)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
- -
- h1
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
+
h1
+ no_toc h1
+ h2
+ no_toc h2
+ h3
+ no_toc h3
+ h4
+ no_toc h4
+ HTML
+ expected = <<~HTML.chomp
+
+ - h1
- -
- h2
+
- h2
- -
- h3
+
- h3
@@ -191,21 +145,21 @@ def test_no_toc
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
end
def test_japanese_toc
- parser = Jekyll::TableOfContents::Parser.new(JAPANESE_HEADINGS_HTML)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
- -
- あ
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
+
あ
+ い
+ う
+ HTML
+ expected = <<~HTML.chomp
+
+ - あ
- -
- い
+
- い
@@ -214,59 +168,62 @@ def test_japanese_toc
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
+ html_with_anchors = parser.inject_anchors_into_html
+
+ assert_match(%r{あ}, html_with_anchors)
+ assert_match(%r{い}, html_with_anchors)
+ assert_match(%r{う}, html_with_anchors)
end
+ # ref. https://github.com/toshimaru/jekyll-toc/issues/45
def test_angle_bracket
- parser = Jekyll::TableOfContents::Parser.new(ANGLE_BRACKET_HTML)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
+ h1
+ <base href>
+ & < >
+ HTML
+ expected = <<~HTML.chomp
+
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
end
def test_tags_inside_heading
- parser = Jekyll::TableOfContents::Parser.new(TAGS_INSIDE_HEADINGS_HTML)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
+ h2
+ h2
+ HTML
+ expected = <<~HTML.chomp
+
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
end
- TEST_HTML_IGNORE = <<~HTML
- h1
-
-
h2
-
- h3
- h6
- HTML
-
def test_nested_toc_with_no_toc_section_class
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_IGNORE)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
- -
- h1
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
+
h1
+
+
h2
+
+ h3
+ h6
+ HTML
+ expected = <<~HTML.chomp
+
+ - h1
- -
- h3
+
- h3
@@ -275,39 +232,34 @@ def test_nested_toc_with_no_toc_section_class
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
html = parser.inject_anchors_into_html
+
assert_match(%r{.+
}m, html)
assert_match(%r{.+
}m, html)
assert_match(%r{.+
}m, html)
assert_includes(html, 'h2
')
end
- TEST_HTML_IGNORE_2 = <<~HTML
- h1
-
-
h2
-
- h3
-
-
h4
- h5
-
- h6
- HTML
-
def test_nested_toc_with_no_toc_section_class_option
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_IGNORE_2, 'no_toc_section_class' => 'exclude')
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
- -
- h1
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML, 'no_toc_section_class' => 'exclude')
+
h1
+
+
h2
+
+ h3
+
+
h4
+ h5
+
+ h6
+ HTML
+ expected = <<~HTML.chomp
+
+ - h1
- -
- h3
+
- h3
@@ -316,10 +268,10 @@ def test_nested_toc_with_no_toc_section_class_option
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
html = parser.inject_anchors_into_html
+
assert_match(%r{.+
}m, html)
assert_match(%r{.+
}m, html)
assert_match(%r{.+
}m, html)
@@ -328,29 +280,24 @@ def test_nested_toc_with_no_toc_section_class_option
assert_includes(html, 'h5
')
end
- TEST_HTML_IGNORE_3 = <<~HTML
- h1
-
-
h2
-
- h3
-
-
h4
- h5
-
- h6
- HTML
-
def test_multiple_no_toc_section_classes
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_IGNORE_3, 'no_toc_section_class' => ['no_toc_section', 'exclude'])
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
- -
- h1
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML, 'no_toc_section_class' => ['no_toc_section', 'exclude'])
+
h1
+
+
h2
+
+ h3
+
+
h4
+ h5
+
+ h6
+ HTML
+ expected = <<~HTML.chomp
+
+ - h1
- -
- h3
+
- h3
@@ -359,10 +306,10 @@ def test_multiple_no_toc_section_classes
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
html = parser.inject_anchors_into_html
+
assert_match(%r{.+
}m, html)
assert_match(%r{.+
}m, html)
assert_match(%r{.+
}m, html)
@@ -371,64 +318,55 @@ def test_multiple_no_toc_section_classes
assert_includes(html, 'h5
')
end
- TEST_EXPLICIT_ID = <<~HTML
- h1
- h2
- h3
- HTML
-
def test_toc_with_explicit_id
- parser = Jekyll::TableOfContents::Parser.new(TEST_EXPLICIT_ID)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
+ h1
+ h2
+ h3
+ HTML
+ expected = <<~HTML.chomp
+
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+ assert_equal(expected, parser.build_toc)
html = parser.inject_anchors_into_html
+
assert_includes(html, %())
assert_includes(html, %())
assert_includes(html, %())
end
- TEST_UNIQ_ID = <<~HTML
- h1
- h1
- h1
- HTML
-
def test_anchor_is_uniq
- parser = Jekyll::TableOfContents::Parser.new(TEST_UNIQ_ID)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
+ h1
+ h1
+ h1
+ HTML
+ expected = <<~HTML.chomp
+
HTML
- actual = doc.css('ul.section-nav').to_s
- assert_equal(expected, actual)
+
+ assert_equal(expected, parser.build_toc)
end
def test_custom_css_classes
parser = Jekyll::TableOfContents::Parser.new(
- TEST_HTML_1,
- 'item_class' => 'custom-item', 'list_class' => 'custom-list', 'sublist_class' => 'custom-sublist', 'item_prefix' => 'custom-prefix-'
+ TEST_HTML,
+ 'item_class' => 'custom-item', 'list_id' => 'custom-toc-id', 'list_class' => 'custom-list', 'sublist_class' => 'custom-sublist', 'item_prefix' => 'custom-prefix-'
)
- doc = Nokogiri::HTML(parser.toc)
- expected = <<~HTML
-
- -
- h1
+ expected = <<~HTML.chomp
+
+ - h1
- -
- h3
+
- h3
@@ -438,6 +376,6 @@ def test_custom_css_classes
HTML
- assert_equal(expected, doc.css('ul.custom-list').to_s)
+ assert_equal(expected, parser.build_toc)
end
end
diff --git a/test/test_configuration.rb b/test/test_configuration.rb
index caf16f0..a3e77c9 100644
--- a/test/test_configuration.rb
+++ b/test/test_configuration.rb
@@ -3,25 +3,29 @@
require 'test_helper'
class TestConfiguration < Minitest::Test
- def test_default_conf1guration
+ def test_default_configuration
configuration = Jekyll::TableOfContents::Configuration.new({})
- assert_equal configuration.toc_levels, 1..6
- assert_equal configuration.no_toc_section_class, 'no_toc_section'
- assert_equal configuration.list_class, 'section-nav'
- assert_equal configuration.sublist_class, ''
- assert_equal configuration.item_class, 'toc-entry'
- assert_equal configuration.item_prefix, 'toc-'
+ 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)
+ assert_equal('toc-', configuration.item_prefix)
end
def test_type_error
configuration = Jekyll::TableOfContents::Configuration.new('TypeError!')
- assert_equal configuration.toc_levels, 1..6
- assert_equal configuration.no_toc_section_class, 'no_toc_section'
- assert_equal configuration.list_class, 'section-nav'
- assert_equal configuration.sublist_class, ''
- assert_equal configuration.item_class, 'toc-entry'
- assert_equal configuration.item_prefix, 'toc-'
+ 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)
+ assert_equal('toc-', configuration.item_prefix)
end
end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index d15e386..3faa4eb 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -20,7 +20,7 @@
HTML
module TestHelpers
- def read_html_and_create_parser
- @parser = Jekyll::TableOfContents::Parser.new(SIMPLE_HTML)
+ def read_html_and_create_parser(options = {})
+ @parser = Jekyll::TableOfContents::Parser.new(SIMPLE_HTML, options)
end
end
diff --git a/test/test_jekyll-toc.rb b/test/test_jekyll-toc.rb
index 34fed46..1ec4cb0 100644
--- a/test/test_jekyll-toc.rb
+++ b/test/test_jekyll-toc.rb
@@ -9,44 +9,50 @@ class TestTableOfContentsFilter < Minitest::Test
def test_toc_only
@context = disable_toc_context
+
assert_empty toc_only(DUMMY_HTML)
end
def test_inject_anchors
@context = disable_toc_context
+
assert_equal DUMMY_HTML, inject_anchors(DUMMY_HTML)
end
def test_toc
@context = disable_toc_context
+
assert_equal DUMMY_HTML, toc(DUMMY_HTML)
end
def test_toc_only2
@context = enable_toc_context
- assert_equal "", toc_only(DUMMY_HTML)
+
+ assert_equal %(), toc_only(DUMMY_HTML)
end
def test_inject_anchors2
@context = enable_toc_context
+
assert_equal DUMMY_HTML, inject_anchors(DUMMY_HTML)
end
def test_toc2
@context = enable_toc_context
- assert_equal "#{DUMMY_HTML}", toc(DUMMY_HTML)
+
+ assert_equal %(#{DUMMY_HTML}), toc(DUMMY_HTML)
end
private
def disable_toc_context
- Struct.new(:registers).new(page: { 'toc' => false })
+ Struct.new(:registers).new({ page: { 'toc' => false } })
end
def enable_toc_context
- Struct.new(:registers).new(
- page: { 'toc' => true },
- site: Struct.new(:config).new({ 'toc' => false })
- )
+ Struct.new(:registers).new({
+ page: { 'toc' => true },
+ site: Struct.new(:config).new({ 'toc' => false })
+ })
end
end
diff --git a/test/test_kramdown_list.rb b/test/test_kramdown_list.rb
index 60019aa..c3ed43b 100644
--- a/test/test_kramdown_list.rb
+++ b/test/test_kramdown_list.rb
@@ -36,7 +36,7 @@ def test_japanese_heading
assert_equal(expected, actual)
end
- def test_kramdown_list_1
+ def test_kramdown_list_l1_l5
text = <<~MARKDOWN
* level-1
* level-2
@@ -70,7 +70,7 @@ def test_kramdown_list_1
assert_equal(expected, actual)
end
- def test_kramdown_list_2
+ def test_kramdown_list_l1_l3_l2_l4
text = <<~MARKDOWN
* level-1
* level-3
@@ -101,7 +101,7 @@ def test_kramdown_list_2
assert_equal(expected, actual)
end
- def test_kramdown_list_3
+ def test_kramdown_list_l4_l1
text = <<~MARKDOWN
* level-4
* level-3
@@ -118,7 +118,7 @@ def test_kramdown_list_3
assert_equal(expected, actual)
end
- def test_kramdown_list_4
+ def test_kramdown_list_l1_l4_l1
text = <<~MARKDOWN
* level-1
* level-4
@@ -143,7 +143,7 @@ def test_kramdown_list_4
assert_equal(expected, actual)
end
- def test_kramdown_list_5
+ def test_kramdown_list_l1_l3_l1
text = <<~MARKDOWN
* level-1
* level-3
diff --git a/test/test_toc_tag.rb b/test/test_toc_tag.rb
index ce2b30e..17dc9df 100644
--- a/test/test_toc_tag.rb
+++ b/test/test_toc_tag.rb
@@ -12,17 +12,19 @@ def setup
end
def test_toc_tag
- context = @stubbed_context.new(
- page: @stubbed_context2.new({ 'toc' => false }, 'test
'),
- site: @stubbed_context1.new({ 'toc' => nil })
- )
+ context = @stubbed_context.new({
+ page: @stubbed_context2.new({ 'toc' => false }, 'test
'),
+ 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
- context = @stubbed_context.new(page: { 'toc' => false })
+ context = @stubbed_context.new({ page: { 'toc' => false } })
tag = Jekyll::TocTag.parse('toc_tag', '', Tokenizer.new(''), ParseContext.new)
+
assert_empty tag.render(context)
end
end