jekyll-toc 0.4.0 → 0.5.0.rc
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jekyll-toc.rb +21 -11
- data/lib/table_of_contents/parser.rb +14 -2
- data/lib/version.rb +1 -1
- data/test/test_kramdown_list.rb +22 -2
- data/test/test_various_toc_html.rb +14 -2
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0353251fa1c46baa96730df912c764c123c9f2bd
|
4
|
+
data.tar.gz: 855171e677a52e8547d066589206328623b6fffd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b527d0826b5b28d783514d10e4323bcfa2f99b1b9e1595fb1124e59bc11e5cc97f250ca31d673c99d9e82bd0bb539c3f3e47ab07e997ff8ea36283513f6b4072
|
7
|
+
data.tar.gz: 978a80e1a914ad9141449dc4bb5c51dbe377b09cf8732be42c8a576e8c5c08ecdd6f718e12fd8e6323b395e12c469767e0d1db41eb7cfe41f227c63b90e60af2
|
data/lib/jekyll-toc.rb
CHANGED
@@ -2,31 +2,41 @@ require 'nokogiri'
|
|
2
2
|
require 'table_of_contents/parser'
|
3
3
|
|
4
4
|
module Jekyll
|
5
|
+
class TocTag < Liquid::Tag
|
6
|
+
def render(context)
|
7
|
+
return unless context.registers[:page]['toc'] == true
|
8
|
+
content_html = context.registers[:page].content
|
9
|
+
::Jekyll::TableOfContents::Parser.new(content_html).build_toc
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
5
13
|
module TableOfContentsFilter
|
6
14
|
def toc_only(html)
|
7
|
-
return html unless
|
8
|
-
|
9
|
-
::Jekyll::TableOfContents::Parser.new(html).build_toc
|
15
|
+
return html unless toc_enabled?
|
16
|
+
::Jekyll::TableOfContents::Parser.new(html, toc_config).build_toc
|
10
17
|
end
|
11
18
|
|
12
19
|
def inject_anchors(html)
|
13
|
-
return html unless
|
14
|
-
|
15
|
-
::Jekyll::TableOfContents::Parser.new(html).inject_anchors_into_html
|
20
|
+
return html unless toc_enabled?
|
21
|
+
::Jekyll::TableOfContents::Parser.new(html, toc_config).inject_anchors_into_html
|
16
22
|
end
|
17
23
|
|
18
24
|
def toc(html)
|
19
|
-
return html unless
|
20
|
-
|
21
|
-
::Jekyll::TableOfContents::Parser.new(html).toc
|
25
|
+
return html unless toc_enabled?
|
26
|
+
::Jekyll::TableOfContents::Parser.new(html, toc_config).toc
|
22
27
|
end
|
23
28
|
|
24
29
|
private
|
25
30
|
|
26
|
-
def
|
27
|
-
@context.registers[:page]
|
31
|
+
def toc_enabled?
|
32
|
+
@context.registers[:page]['toc'] == true
|
33
|
+
end
|
34
|
+
|
35
|
+
def toc_config
|
36
|
+
@context.registers[:site].config["toc"]
|
28
37
|
end
|
29
38
|
end
|
30
39
|
end
|
31
40
|
|
32
41
|
Liquid::Template.register_filter(Jekyll::TableOfContentsFilter)
|
42
|
+
# Liquid::Template.register_tag('toc', Jekyll::TocTag) # will be enabled at v1.0
|
@@ -4,8 +4,15 @@ module Jekyll
|
|
4
4
|
class Parser
|
5
5
|
PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u
|
6
6
|
|
7
|
-
|
7
|
+
DEFAULT_CONFIG = {
|
8
|
+
"min_level" => 1,
|
9
|
+
"max_level" => 6,
|
10
|
+
}
|
11
|
+
|
12
|
+
def initialize(html, options = {})
|
8
13
|
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
14
|
+
options = DEFAULT_CONFIG.merge(options)
|
15
|
+
@toc_levels = options["min_level"]..options["max_level"]
|
9
16
|
@entries = parse_content
|
10
17
|
end
|
11
18
|
|
@@ -33,7 +40,8 @@ module Jekyll
|
|
33
40
|
entries = []
|
34
41
|
headers = Hash.new(0)
|
35
42
|
|
36
|
-
|
43
|
+
# TODO: Use kramdown auto ids
|
44
|
+
@doc.css(toc_headings).each do |node|
|
37
45
|
text = node.text
|
38
46
|
id = text.downcase
|
39
47
|
id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation
|
@@ -104,6 +112,10 @@ module Jekyll
|
|
104
112
|
nest_entries << entry
|
105
113
|
end
|
106
114
|
end
|
115
|
+
|
116
|
+
def toc_headings
|
117
|
+
@toc_levels.map { |level| "h#{level}" }.join(",")
|
118
|
+
end
|
107
119
|
end
|
108
120
|
end
|
109
121
|
end
|
data/lib/version.rb
CHANGED
data/test/test_kramdown_list.rb
CHANGED
@@ -1,6 +1,23 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class TestKramdownList < Minitest::Test
|
4
|
+
# NOTE: kramdown automatically injects `id` attribute
|
5
|
+
# TODO: test Japanese heading
|
6
|
+
def test_kramdown_heading
|
7
|
+
text = <<-MARKDOWN
|
8
|
+
# h1
|
9
|
+
|
10
|
+
## h2
|
11
|
+
MARKDOWN
|
12
|
+
expected = <<-HTML
|
13
|
+
<h1 id="h1">h1</h1>
|
14
|
+
|
15
|
+
<h2 id=\"h2\">h2</h2>
|
16
|
+
HTML
|
17
|
+
|
18
|
+
assert_equal(expected, Kramdown::Document.new(text).to_html)
|
19
|
+
end
|
20
|
+
|
4
21
|
def test_kramdown_list_1
|
5
22
|
text = <<-MARKDOWN
|
6
23
|
* level-1
|
@@ -72,12 +89,15 @@ class TestKramdownList < Minitest::Test
|
|
72
89
|
* level-1
|
73
90
|
MARKDOWN
|
74
91
|
expected = <<-HTML
|
92
|
+
<pre><code> * level-4
|
93
|
+
* level-3 * level-2 * level-1
|
94
|
+
</code></pre>
|
75
95
|
HTML
|
76
96
|
|
77
97
|
assert_equal(expected, Kramdown::Document.new(text).to_html)
|
78
98
|
end
|
79
99
|
|
80
|
-
def
|
100
|
+
def test_kramdown_list_4
|
81
101
|
text = <<-MARKDOWN
|
82
102
|
* level-1
|
83
103
|
* level-4
|
@@ -101,7 +121,7 @@ class TestKramdownList < Minitest::Test
|
|
101
121
|
assert_equal(expected, Kramdown::Document.new(text).to_html)
|
102
122
|
end
|
103
123
|
|
104
|
-
def
|
124
|
+
def test_kramdown_list_5
|
105
125
|
text = <<-MARKDOWN
|
106
126
|
* level-1
|
107
127
|
* level-3
|
@@ -53,6 +53,18 @@ class TestVariousTocHtml < Minitest::Test
|
|
53
53
|
assert_equal(expected, doc.css('ul.section-nav').to_s)
|
54
54
|
end
|
55
55
|
|
56
|
+
def test_nested_toc_with_min_and_max
|
57
|
+
parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_1, { "min_level" => 2, "max_level" => 5 })
|
58
|
+
doc = Nokogiri::HTML(parser.toc)
|
59
|
+
expected = <<-HTML
|
60
|
+
<ul class="section-nav">
|
61
|
+
<li class="toc-entry toc-h3"><a href="#h3">h3</a></li>
|
62
|
+
</ul>
|
63
|
+
HTML
|
64
|
+
|
65
|
+
assert_equal(expected, doc.css('ul.section-nav').to_s)
|
66
|
+
end
|
67
|
+
|
56
68
|
def test_complex_nested_toc
|
57
69
|
parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_2)
|
58
70
|
doc = Nokogiri::HTML(parser.toc)
|
@@ -76,7 +88,7 @@ class TestVariousTocHtml < Minitest::Test
|
|
76
88
|
assert_equal(expected, doc.css('ul.section-nav').to_s)
|
77
89
|
end
|
78
90
|
|
79
|
-
def
|
91
|
+
def test_decremental_headings1
|
80
92
|
parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_3)
|
81
93
|
doc = Nokogiri::HTML(parser.toc)
|
82
94
|
expected = <<-HTML
|
@@ -94,7 +106,7 @@ class TestVariousTocHtml < Minitest::Test
|
|
94
106
|
end
|
95
107
|
|
96
108
|
|
97
|
-
def
|
109
|
+
def test_decremental_headings2
|
98
110
|
parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_4)
|
99
111
|
doc = Nokogiri::HTML(parser.toc)
|
100
112
|
expected = <<-HTML
|
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.
|
4
|
+
version: 0.5.0.rc
|
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: 2017-
|
12
|
+
date: 2017-11-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -154,12 +154,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
154
154
|
version: 2.1.0
|
155
155
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
156
|
requirements:
|
157
|
-
- - "
|
157
|
+
- - ">"
|
158
158
|
- !ruby/object:Gem::Version
|
159
|
-
version:
|
159
|
+
version: 1.3.1
|
160
160
|
requirements: []
|
161
161
|
rubyforge_project:
|
162
|
-
rubygems_version: 2.6.
|
162
|
+
rubygems_version: 2.6.11
|
163
163
|
signing_key:
|
164
164
|
specification_version: 4
|
165
165
|
summary: Jekyll Table of Contents plugin
|