jekyll-toc 0.7.0.alpha1 → 0.7.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/lib/jekyll-toc.rb +3 -1
- data/lib/table_of_contents/parser.rb +10 -11
- data/lib/version.rb +1 -1
- data/test/test_helper.rb +2 -1
- data/test/test_inject_anchors_filter.rb +2 -0
- data/test/test_kramdown_list.rb +2 -0
- data/test/test_option_error.rb +2 -0
- data/test/test_toc_filter.rb +2 -0
- data/test/test_toc_only_filter.rb +2 -0
- data/test/test_various_toc_html.rb +49 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b7578981ba66427ed37a31d9705ad0e174d5646d31f5cd028fbe28038249a83
|
4
|
+
data.tar.gz: 63cfa7ca781467175dea8072975b41416fc556e2e4fa3c0f77f741ff0746c401
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 639bb631c1370b4c0f01f5769fa79048f883603d9582441d2c7d214e89bd2e578d6c174b72c48d5d1acaf52e496fe59332c8b6626eb4b1786c1ee6934bcce542
|
7
|
+
data.tar.gz: 120b7f1bafb23b34c99149d4db56418644e9918b436a9753ecf07ac5cabb19a3410eac999785277cc38bf6e3a88de2bc84efbac4e3162485250d460affcc5d92
|
data/README.md
CHANGED
@@ -103,6 +103,16 @@ It looks like the image below.
|
|
103
103
|
|
104
104
|
## Customization
|
105
105
|
|
106
|
+
### Skip TOC(`no_toc`)
|
107
|
+
|
108
|
+
The heding is ignored in the toc when you add `no_toc` to the class.
|
109
|
+
|
110
|
+
```html
|
111
|
+
<h1>h1</h1>
|
112
|
+
<h1 class="no_toc">This heding is ignored in the toc</h1>
|
113
|
+
<h2>h2</h2>
|
114
|
+
```
|
115
|
+
|
106
116
|
### TOC level
|
107
117
|
|
108
118
|
The toc levels can be configured on `_config.yml`.
|
data/lib/jekyll-toc.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'nokogiri'
|
2
4
|
require 'table_of_contents/parser'
|
3
5
|
|
@@ -33,7 +35,7 @@ module Jekyll
|
|
33
35
|
end
|
34
36
|
|
35
37
|
def toc_config
|
36
|
-
@context.registers[:site].config[
|
38
|
+
@context.registers[:site].config['toc'] || {}
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Jekyll
|
2
4
|
module TableOfContents
|
3
5
|
# Parse html contents and generate table of contents
|
@@ -5,14 +7,14 @@ module Jekyll
|
|
5
7
|
PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u
|
6
8
|
|
7
9
|
DEFAULT_CONFIG = {
|
8
|
-
|
9
|
-
|
10
|
-
}
|
10
|
+
'min_level' => 1,
|
11
|
+
'max_level' => 6
|
12
|
+
}.freeze
|
11
13
|
|
12
14
|
def initialize(html, options = {})
|
13
15
|
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
14
16
|
options = generate_option_hash(options)
|
15
|
-
@toc_levels = options[
|
17
|
+
@toc_levels = options['min_level']..options['max_level']
|
16
18
|
@entries = parse_content
|
17
19
|
end
|
18
20
|
|
@@ -41,11 +43,11 @@ module Jekyll
|
|
41
43
|
headers = Hash.new(0)
|
42
44
|
|
43
45
|
# TODO: Use kramdown auto ids
|
44
|
-
@doc.css(toc_headings).each do |node|
|
46
|
+
@doc.css(toc_headings).reject { |n| n.classes.include?('no_toc') }.each do |node|
|
45
47
|
text = node.text
|
46
48
|
id = text.downcase
|
47
49
|
id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation
|
48
|
-
id.
|
50
|
+
id.tr!(' ', '-') # replace spaces with dash
|
49
51
|
|
50
52
|
uniq = headers[id] > 0 ? "-#{headers[id]}" : ''
|
51
53
|
headers[id] += 1
|
@@ -57,7 +59,6 @@ module Jekyll
|
|
57
59
|
uniq: uniq,
|
58
60
|
text: text,
|
59
61
|
node_name: node.name,
|
60
|
-
no_toc: node.attribute('class') && node.attribute('class').value.include?('no_toc'),
|
61
62
|
content_node: header_content,
|
62
63
|
h_num: node.name.delete('h').to_i
|
63
64
|
}
|
@@ -69,14 +70,12 @@ module Jekyll
|
|
69
70
|
# Returns the list items for entries
|
70
71
|
def build_toc_list(entries, last_ul_used: false)
|
71
72
|
i = 0
|
72
|
-
toc_list = ''
|
73
|
+
toc_list = ''.dup
|
73
74
|
min_h_num = entries.map { |e| e[:h_num] }.min
|
74
75
|
|
75
76
|
while i < entries.count
|
76
77
|
entry = entries[i]
|
77
|
-
if entry[:
|
78
|
-
# Do nothing / skip entry
|
79
|
-
elsif entry[:h_num] == min_h_num
|
78
|
+
if entry[:h_num] == min_h_num
|
80
79
|
# If the current entry should not be indented in the list, add the entry to the list
|
81
80
|
toc_list << %(<li class="toc-entry toc-#{entry[:node_name]}"><a href="##{entry[:id]}#{entry[:uniq]}">#{entry[:text]}</a>)
|
82
81
|
# If the next entry should be indented in the list, generate a sublist
|
data/lib/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'simplecov'
|
2
4
|
SimpleCov.start
|
3
5
|
|
@@ -12,7 +14,6 @@ SIMPLE_HTML = <<-HTML.freeze
|
|
12
14
|
<h4>Simple H4</h4>
|
13
15
|
<h5>Simple H5</h5>
|
14
16
|
<h6>Simple H6</h6>
|
15
|
-
<h1 class="no_toc">No-toc H1</h1>
|
16
17
|
HTML
|
17
18
|
|
18
19
|
module TestHelpers
|
data/test/test_kramdown_list.rb
CHANGED
data/test/test_option_error.rb
CHANGED
data/test/test_toc_filter.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
5
|
class TestVariousTocHtml < Minitest::Test
|
@@ -31,6 +33,17 @@ class TestVariousTocHtml < Minitest::Test
|
|
31
33
|
<h5>h5</h5>
|
32
34
|
HTML
|
33
35
|
|
36
|
+
NO_TOC_HTML = <<-HTML
|
37
|
+
<h1>h1</h1>
|
38
|
+
<h1 class="no_toc">no_toc h1</h1>
|
39
|
+
<h2>h2</h2>
|
40
|
+
<h2 class="no_toc">no_toc h2</h2>
|
41
|
+
<h3>h3</h3>
|
42
|
+
<h3 class="no_toc">no_toc h3</h3>
|
43
|
+
<h4>h4</h4>
|
44
|
+
<h4 class="no_toc">no_toc h4</h4>
|
45
|
+
HTML
|
46
|
+
|
34
47
|
def test_nested_toc
|
35
48
|
parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_1)
|
36
49
|
doc = Nokogiri::HTML(parser.toc)
|
@@ -49,8 +62,9 @@ class TestVariousTocHtml < Minitest::Test
|
|
49
62
|
</li>
|
50
63
|
</ul>
|
51
64
|
HTML
|
65
|
+
actual = doc.css('ul.section-nav').to_s
|
52
66
|
|
53
|
-
assert_equal(expected,
|
67
|
+
assert_equal(expected, actual)
|
54
68
|
end
|
55
69
|
|
56
70
|
def test_nested_toc_with_min_and_max
|
@@ -61,8 +75,9 @@ class TestVariousTocHtml < Minitest::Test
|
|
61
75
|
<li class="toc-entry toc-h3"><a href="#h3">h3</a></li>
|
62
76
|
</ul>
|
63
77
|
HTML
|
78
|
+
actual = doc.css('ul.section-nav').to_s
|
64
79
|
|
65
|
-
assert_equal(expected,
|
80
|
+
assert_equal(expected, actual)
|
66
81
|
end
|
67
82
|
|
68
83
|
def test_complex_nested_toc
|
@@ -84,8 +99,9 @@ class TestVariousTocHtml < Minitest::Test
|
|
84
99
|
</li>
|
85
100
|
</ul>
|
86
101
|
HTML
|
102
|
+
actual = doc.css('ul.section-nav').to_s
|
87
103
|
|
88
|
-
assert_equal(expected,
|
104
|
+
assert_equal(expected, actual)
|
89
105
|
end
|
90
106
|
|
91
107
|
def test_decremental_headings1
|
@@ -101,11 +117,11 @@ class TestVariousTocHtml < Minitest::Test
|
|
101
117
|
<li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
|
102
118
|
</ul>
|
103
119
|
HTML
|
120
|
+
actual = doc.css('ul.section-nav').to_s
|
104
121
|
|
105
|
-
assert_equal(expected,
|
122
|
+
assert_equal(expected, actual)
|
106
123
|
end
|
107
124
|
|
108
|
-
|
109
125
|
def test_decremental_headings2
|
110
126
|
parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_4)
|
111
127
|
doc = Nokogiri::HTML(parser.toc)
|
@@ -133,4 +149,32 @@ class TestVariousTocHtml < Minitest::Test
|
|
133
149
|
|
134
150
|
assert_equal(expected, doc.css('ul.section-nav').to_s)
|
135
151
|
end
|
152
|
+
|
153
|
+
def test_no_toc
|
154
|
+
parser = Jekyll::TableOfContents::Parser.new(NO_TOC_HTML)
|
155
|
+
doc = Nokogiri::HTML(parser.toc)
|
156
|
+
expected = <<-HTML
|
157
|
+
<ul class="section-nav">
|
158
|
+
<li class="toc-entry toc-h1">
|
159
|
+
<a href="#h1">h1</a>
|
160
|
+
<ul>
|
161
|
+
<li class="toc-entry toc-h2">
|
162
|
+
<a href="#h2">h2</a>
|
163
|
+
<ul>
|
164
|
+
<li class="toc-entry toc-h3">
|
165
|
+
<a href="#h3">h3</a>
|
166
|
+
<ul>
|
167
|
+
<li class="toc-entry toc-h4"><a href="#h4">h4</a></li>
|
168
|
+
</ul>
|
169
|
+
</li>
|
170
|
+
</ul>
|
171
|
+
</li>
|
172
|
+
</ul>
|
173
|
+
</li>
|
174
|
+
</ul>
|
175
|
+
HTML
|
176
|
+
actual = doc.css('ul.section-nav').to_s
|
177
|
+
|
178
|
+
assert_equal(expected, actual)
|
179
|
+
end
|
136
180
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-toc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.0.
|
4
|
+
version: 0.7.0.beta1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- toshimaru
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-09-
|
12
|
+
date: 2018-09-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|