jekyll-toc 0.12.1 → 0.15.0.rc

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module TableOfContents
5
+ VERSION = '0.15.0.rc'
6
+ end
7
+ end
@@ -2,9 +2,9 @@
2
2
 
3
3
  require 'test_helper'
4
4
 
5
- class TestOptionError < Minitest::Test
5
+ class TestInvalidOptions < Minitest::Test
6
6
  BASE_HTML = '<h1>h1</h1>'
7
- EXPECTED_HTML = <<~HTML
7
+ EXPECTED_HTML = <<~HTML.chomp
8
8
  <ul class="section-nav">
9
9
  <li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
10
10
  </ul>
@@ -12,29 +12,21 @@ class TestOptionError < Minitest::Test
12
12
 
13
13
  def test_option_is_nil
14
14
  parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, nil)
15
- doc = Nokogiri::HTML(parser.toc)
16
- expected = EXPECTED_HTML
17
- assert_equal(expected, doc.css('ul.section-nav').to_s)
15
+ assert_equal(EXPECTED_HTML, parser.build_toc)
18
16
  end
19
17
 
20
18
  def test_option_is_epmty_string
21
19
  parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, '')
22
- doc = Nokogiri::HTML(parser.toc)
23
- expected = EXPECTED_HTML
24
- assert_equal(expected, doc.css('ul.section-nav').to_s)
20
+ assert_equal(EXPECTED_HTML, parser.build_toc)
25
21
  end
26
22
 
27
23
  def test_option_is_string
28
24
  parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, 'string')
29
- doc = Nokogiri::HTML(parser.toc)
30
- expected = EXPECTED_HTML
31
- assert_equal(expected, doc.css('ul.section-nav').to_s)
25
+ assert_equal(EXPECTED_HTML, parser.build_toc)
32
26
  end
33
27
 
34
28
  def test_option_is_array
35
29
  parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, [])
36
- doc = Nokogiri::HTML(parser.toc)
37
- expected = EXPECTED_HTML
38
- assert_equal(expected, doc.css('ul.section-nav').to_s)
30
+ assert_equal(EXPECTED_HTML, parser.build_toc)
39
31
  end
40
32
  end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class TestOrderedList < Minitest::Test
6
+ include TestHelpers
7
+
8
+ def test_default_configuration
9
+ configuration = Jekyll::TableOfContents::Configuration.new({})
10
+
11
+ assert_equal configuration.ordered_list, false
12
+ end
13
+
14
+ def test_disabled_ordered_list
15
+ configuration = Jekyll::TableOfContents::Configuration.new('ordered_list' => false)
16
+
17
+ assert_equal configuration.ordered_list, false
18
+ end
19
+
20
+ def test_enabled_ordered_list
21
+ configuration = Jekyll::TableOfContents::Configuration.new('ordered_list' => true)
22
+
23
+ assert_equal configuration.ordered_list, true
24
+ end
25
+
26
+ def test_basic_ordered_list_top_heading
27
+ parse_with_ordered_list
28
+ html = @parser.toc
29
+
30
+ assert_match(/^<ol class="section-nav">/, html)
31
+ end
32
+
33
+ def test_ordered_list_sub_headings
34
+ parse_with_ordered_list
35
+ html = @parser.toc
36
+
37
+ assert_match(/<ol>\n<li class="toc-entry/, html)
38
+ end
39
+
40
+ def test_ordered_list_top_heading_with_classes
41
+ parse_with_ordered_list_and_classes
42
+ html = @parser.toc
43
+
44
+ assert_match(/^<ol class="top-list-class">/, html)
45
+ end
46
+
47
+ def test_ordered_list_sub_headings_with_classes
48
+ parse_with_ordered_list_and_classes
49
+ html = @parser.toc
50
+
51
+ assert_match(/<ol class="sublist-class">/, html)
52
+ end
53
+
54
+ def test_ordered_list_subheadings_with_classes_nested_structure
55
+ parse_with_ordered_list_and_classes
56
+ html = @parser.toc
57
+
58
+ occurrences = html.scan(/<ol class="sublist-class">/).count
59
+
60
+ assert_equal occurrences, 5
61
+ end
62
+
63
+ private
64
+
65
+ def parse_with_ordered_list
66
+ read_html_and_create_parser('ordered_list' => true)
67
+ end
68
+
69
+ def parse_with_ordered_list_and_classes
70
+ read_html_and_create_parser(
71
+ 'ordered_list' => true,
72
+ 'list_class' => 'top-list-class',
73
+ 'sublist_class' => 'sublist-class'
74
+ )
75
+ end
76
+ end
@@ -3,75 +3,19 @@
3
3
  require 'test_helper'
4
4
 
5
5
  class TestVariousTocHtml < Minitest::Test
6
- # ref. https://github.com/toshimaru/jekyll-toc/issues/45
7
- ANGLE_BRACKET_HTML = <<~HTML
8
- <h1>h1</h1>
9
- <h1>&lt;base href&gt;</h1>
10
- <h1>&amp; &lt; &gt;</h1>
11
- HTML
12
-
13
- NO_TOC_HTML = <<~HTML
14
- <h1>h1</h1>
15
- <h1 class="no_toc">no_toc h1</h1>
16
- <h2>h2</h2>
17
- <h2 class="no_toc">no_toc h2</h2>
18
- <h3>h3</h3>
19
- <h3 class="no_toc">no_toc h3</h3>
20
- <h4>h4</h4>
21
- <h4 class="no_toc">no_toc h4</h4>
22
- HTML
23
-
24
- JAPANESE_HEADINGS_HTML = <<~HTML
25
- <h1>あ</h1>
26
- <h2>い</h2>
27
- <h3>う</h3>
28
- HTML
29
-
30
- TAGS_INSIDE_HEADINGS_HTML = <<~HTML
31
- <h2><strong>h2</strong></h2>
32
- <h2><em>h2</em></h2>
33
- HTML
34
-
35
- TEST_HTML_1 = <<~HTML
36
- <h1>h1</h1>
37
- <h3>h3</h3>
38
- <h6>h6</h6>
39
- HTML
40
-
41
- TEST_HTML_2 = <<~HTML
6
+ TEST_HTML = <<~HTML
42
7
  <h1>h1</h1>
43
8
  <h3>h3</h3>
44
- <h2>h2</h2>
45
- <h6>h6</h6>
46
- HTML
47
-
48
- TEST_HTML_3 = <<~HTML
49
9
  <h6>h6</h6>
50
- <h5>h5</h5>
51
- <h4>h4</h4>
52
- <h3>h3</h3>
53
- <h2>h2</h2>
54
- <h1>h1</h1>
55
- HTML
56
-
57
- TEST_HTML_4 = <<~HTML
58
- <h1>h1</h1>
59
- <h3>h3</h3>
60
- <h2>h2</h2>
61
- <h4>h4</h4>
62
- <h5>h5</h5>
63
10
  HTML
64
11
 
65
12
  def test_nested_toc
66
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_1)
67
- doc = Nokogiri::HTML(parser.toc)
68
- expected = <<~HTML
13
+ parser = Jekyll::TableOfContents::Parser.new(TEST_HTML)
14
+ expected = <<~HTML.chomp
69
15
  <ul class="section-nav">
70
- <li class="toc-entry toc-h1">
71
- <a href="#h1">h1</a>
16
+ <li class="toc-entry toc-h1"><a href="#h1">h1</a>
72
17
  <ul>
73
- <li class="toc-entry toc-h3">
74
- <a href="#h3">h3</a>
18
+ <li class="toc-entry toc-h3"><a href="#h3">h3</a>
75
19
  <ul>
76
20
  <li class="toc-entry toc-h6"><a href="#h6">h6</a></li>
77
21
  </ul>
@@ -80,35 +24,34 @@ class TestVariousTocHtml < Minitest::Test
80
24
  </li>
81
25
  </ul>
82
26
  HTML
83
- actual = doc.css('ul.section-nav').to_s
84
27
 
85
- assert_equal(expected, actual)
28
+ assert_equal(expected, parser.build_toc)
86
29
  end
87
30
 
88
31
  def test_nested_toc_with_min_and_max
89
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_1, 'min_level' => 2, 'max_level' => 5)
90
- doc = Nokogiri::HTML(parser.toc)
91
- expected = <<~HTML
32
+ parser = Jekyll::TableOfContents::Parser.new(TEST_HTML, 'min_level' => 2, 'max_level' => 5)
33
+ expected = <<~HTML.chomp
92
34
  <ul class="section-nav">
93
35
  <li class="toc-entry toc-h3"><a href="#h3">h3</a></li>
94
36
  </ul>
95
37
  HTML
96
- actual = doc.css('ul.section-nav').to_s
97
38
 
98
- assert_equal(expected, actual)
39
+ assert_equal(expected, parser.build_toc)
99
40
  end
100
41
 
101
42
  def test_complex_nested_toc
102
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_2)
103
- doc = Nokogiri::HTML(parser.toc)
104
- expected = <<~HTML
43
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
44
+ <h1>h1</h1>
45
+ <h3>h3</h3>
46
+ <h2>h2</h2>
47
+ <h6>h6</h6>
48
+ HTML
49
+ expected = <<~HTML.chomp
105
50
  <ul class="section-nav">
106
- <li class="toc-entry toc-h1">
107
- <a href="#h1">h1</a>
51
+ <li class="toc-entry toc-h1"><a href="#h1">h1</a>
108
52
  <ul>
109
53
  <li class="toc-entry toc-h3"><a href="#h3">h3</a></li>
110
- <li class="toc-entry toc-h2">
111
- <a href="#h2">h2</a>
54
+ <li class="toc-entry toc-h2"><a href="#h2">h2</a>
112
55
  <ul>
113
56
  <li class="toc-entry toc-h6"><a href="#h6">h6</a></li>
114
57
  </ul>
@@ -117,15 +60,20 @@ class TestVariousTocHtml < Minitest::Test
117
60
  </li>
118
61
  </ul>
119
62
  HTML
120
- actual = doc.css('ul.section-nav').to_s
121
63
 
122
- assert_equal(expected, actual)
64
+ assert_equal(expected, parser.build_toc)
123
65
  end
124
66
 
125
67
  def test_decremental_headings1
126
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_3)
127
- doc = Nokogiri::HTML(parser.toc)
128
- expected = <<~HTML
68
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
69
+ <h6>h6</h6>
70
+ <h5>h5</h5>
71
+ <h4>h4</h4>
72
+ <h3>h3</h3>
73
+ <h2>h2</h2>
74
+ <h1>h1</h1>
75
+ HTML
76
+ expected = <<~HTML.chomp
129
77
  <ul class="section-nav">
130
78
  <li class="toc-entry toc-h6"><a href="#h6">h6</a></li>
131
79
  <li class="toc-entry toc-h5"><a href="#h5">h5</a></li>
@@ -135,25 +83,26 @@ class TestVariousTocHtml < Minitest::Test
135
83
  <li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
136
84
  </ul>
137
85
  HTML
138
- actual = doc.css('ul.section-nav').to_s
139
86
 
140
- assert_equal(expected, actual)
87
+ assert_equal(expected, parser.build_toc)
141
88
  end
142
89
 
143
90
  def test_decremental_headings2
144
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_4)
145
- doc = Nokogiri::HTML(parser.toc)
146
- expected = <<~HTML
91
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
92
+ <h1>h1</h1>
93
+ <h3>h3</h3>
94
+ <h2>h2</h2>
95
+ <h4>h4</h4>
96
+ <h5>h5</h5>
97
+ HTML
98
+ expected = <<~HTML.chomp
147
99
  <ul class="section-nav">
148
- <li class="toc-entry toc-h1">
149
- <a href="#h1">h1</a>
100
+ <li class="toc-entry toc-h1"><a href="#h1">h1</a>
150
101
  <ul>
151
102
  <li class="toc-entry toc-h3"><a href="#h3">h3</a></li>
152
- <li class="toc-entry toc-h2">
153
- <a href="#h2">h2</a>
103
+ <li class="toc-entry toc-h2"><a href="#h2">h2</a>
154
104
  <ul>
155
- <li class="toc-entry toc-h4">
156
- <a href="#h4">h4</a>
105
+ <li class="toc-entry toc-h4"><a href="#h4">h4</a>
157
106
  <ul>
158
107
  <li class="toc-entry toc-h5"><a href="#h5">h5</a></li>
159
108
  </ul>
@@ -165,22 +114,27 @@ class TestVariousTocHtml < Minitest::Test
165
114
  </ul>
166
115
  HTML
167
116
 
168
- assert_equal(expected, doc.css('ul.section-nav').to_s)
117
+ assert_equal(expected, parser.build_toc)
169
118
  end
170
119
 
171
120
  def test_no_toc
172
- parser = Jekyll::TableOfContents::Parser.new(NO_TOC_HTML)
173
- doc = Nokogiri::HTML(parser.toc)
174
- expected = <<~HTML
121
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
122
+ <h1>h1</h1>
123
+ <h1 class="no_toc">no_toc h1</h1>
124
+ <h2>h2</h2>
125
+ <h2 class="no_toc">no_toc h2</h2>
126
+ <h3>h3</h3>
127
+ <h3 class="no_toc">no_toc h3</h3>
128
+ <h4>h4</h4>
129
+ <h4 class="no_toc">no_toc h4</h4>
130
+ HTML
131
+ expected = <<~HTML.chomp
175
132
  <ul class="section-nav">
176
- <li class="toc-entry toc-h1">
177
- <a href="#h1">h1</a>
133
+ <li class="toc-entry toc-h1"><a href="#h1">h1</a>
178
134
  <ul>
179
- <li class="toc-entry toc-h2">
180
- <a href="#h2">h2</a>
135
+ <li class="toc-entry toc-h2"><a href="#h2">h2</a>
181
136
  <ul>
182
- <li class="toc-entry toc-h3">
183
- <a href="#h3">h3</a>
137
+ <li class="toc-entry toc-h3"><a href="#h3">h3</a>
184
138
  <ul>
185
139
  <li class="toc-entry toc-h4"><a href="#h4">h4</a></li>
186
140
  </ul>
@@ -191,21 +145,21 @@ class TestVariousTocHtml < Minitest::Test
191
145
  </li>
192
146
  </ul>
193
147
  HTML
194
- actual = doc.css('ul.section-nav').to_s
195
148
 
196
- assert_equal(expected, actual)
149
+ assert_equal(expected, parser.build_toc)
197
150
  end
198
151
 
199
152
  def test_japanese_toc
200
- parser = Jekyll::TableOfContents::Parser.new(JAPANESE_HEADINGS_HTML)
201
- doc = Nokogiri::HTML(parser.toc)
202
- expected = <<~HTML
153
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
154
+ <h1>あ</h1>
155
+ <h2>い</h2>
156
+ <h3>う</h3>
157
+ HTML
158
+ expected = <<~HTML.chomp
203
159
  <ul class="section-nav">
204
- <li class="toc-entry toc-h1">
205
- <a href="#%E3%81%82">あ</a>
160
+ <li class="toc-entry toc-h1"><a href="#%E3%81%82">あ</a>
206
161
  <ul>
207
- <li class="toc-entry toc-h2">
208
- <a href="#%E3%81%84">い</a>
162
+ <li class="toc-entry toc-h2"><a href="#%E3%81%84">い</a>
209
163
  <ul>
210
164
  <li class="toc-entry toc-h3"><a href="#%E3%81%86">う</a></li>
211
165
  </ul>
@@ -214,59 +168,61 @@ class TestVariousTocHtml < Minitest::Test
214
168
  </li>
215
169
  </ul>
216
170
  HTML
217
- actual = doc.css('ul.section-nav').to_s
218
171
 
219
- assert_equal(expected, actual)
172
+ assert_equal(expected, parser.build_toc)
173
+ html_with_anchors = parser.inject_anchors_into_html
174
+ assert_match(%r{<a class="anchor" href="#%E3%81%82" aria-hidden="true"><span.*span></a>あ}, html_with_anchors)
175
+ assert_match(%r{<a class="anchor" href="#%E3%81%84" aria-hidden="true"><span.*span></a>い}, html_with_anchors)
176
+ assert_match(%r{<a class="anchor" href="#%E3%81%86" aria-hidden="true"><span.*span></a>う}, html_with_anchors)
220
177
  end
221
178
 
179
+ # ref. https://github.com/toshimaru/jekyll-toc/issues/45
222
180
  def test_angle_bracket
223
- parser = Jekyll::TableOfContents::Parser.new(ANGLE_BRACKET_HTML)
224
- doc = Nokogiri::HTML(parser.toc)
225
- expected = <<~HTML
181
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
182
+ <h1>h1</h1>
183
+ <h1>&lt;base href&gt;</h1>
184
+ <h1>&amp; &lt; &gt;</h1>
185
+ HTML
186
+ expected = <<~HTML.chomp
226
187
  <ul class="section-nav">
227
188
  <li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
228
189
  <li class="toc-entry toc-h1"><a href="#base-href">&lt;base href&gt;</a></li>
229
190
  <li class="toc-entry toc-h1"><a href="#--">&amp; &lt; &gt;</a></li>
230
191
  </ul>
231
192
  HTML
232
- actual = doc.css('ul.section-nav').to_s
233
193
 
234
- assert_equal(expected, actual)
194
+ assert_equal(expected, parser.build_toc)
235
195
  end
236
196
 
237
197
  def test_tags_inside_heading
238
- parser = Jekyll::TableOfContents::Parser.new(TAGS_INSIDE_HEADINGS_HTML)
239
- doc = Nokogiri::HTML(parser.toc)
240
- expected = <<~HTML
198
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
199
+ <h2><strong>h2</strong></h2>
200
+ <h2><em>h2</em></h2>
201
+ HTML
202
+ expected = <<~HTML.chomp
241
203
  <ul class="section-nav">
242
204
  <li class="toc-entry toc-h2"><a href="#h2">h2</a></li>
243
205
  <li class="toc-entry toc-h2"><a href="#h2-1">h2</a></li>
244
206
  </ul>
245
207
  HTML
246
- actual = doc.css('ul.section-nav').to_s
247
208
 
248
- assert_equal(expected, actual)
209
+ assert_equal(expected, parser.build_toc)
249
210
  end
250
211
 
251
- TEST_HTML_IGNORE = <<~HTML
252
- <h1>h1</h1>
253
- <div class="no_toc_section">
254
- <h2>h2</h2>
255
- </div>
256
- <h3>h3</h3>
257
- <h6>h6</h6>
258
- HTML
259
-
260
212
  def test_nested_toc_with_no_toc_section_class
261
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_IGNORE)
262
- doc = Nokogiri::HTML(parser.toc)
263
- expected = <<~HTML
213
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
214
+ <h1>h1</h1>
215
+ <div class="no_toc_section">
216
+ <h2>h2</h2>
217
+ </div>
218
+ <h3>h3</h3>
219
+ <h6>h6</h6>
220
+ HTML
221
+ expected = <<~HTML.chomp
264
222
  <ul class="section-nav">
265
- <li class="toc-entry toc-h1">
266
- <a href="#h1">h1</a>
223
+ <li class="toc-entry toc-h1"><a href="#h1">h1</a>
267
224
  <ul>
268
- <li class="toc-entry toc-h3">
269
- <a href="#h3">h3</a>
225
+ <li class="toc-entry toc-h3"><a href="#h3">h3</a>
270
226
  <ul>
271
227
  <li class="toc-entry toc-h6"><a href="#h6">h6</a></li>
272
228
  </ul>
@@ -275,8 +231,7 @@ class TestVariousTocHtml < Minitest::Test
275
231
  </li>
276
232
  </ul>
277
233
  HTML
278
- actual = doc.css('ul.section-nav').to_s
279
- assert_equal(expected, actual)
234
+ assert_equal(expected, parser.build_toc)
280
235
 
281
236
  html = parser.inject_anchors_into_html
282
237
  assert_match(%r{<h1>.+</h1>}m, html)
@@ -285,29 +240,24 @@ class TestVariousTocHtml < Minitest::Test
285
240
  assert_includes(html, '<h2>h2</h2>')
286
241
  end
287
242
 
288
- TEST_HTML_IGNORE_2 = <<~HTML
289
- <h1>h1</h1>
290
- <div class="exclude">
291
- <h2>h2</h2>
292
- </div>
293
- <h3>h3</h3>
294
- <div class="exclude">
295
- <h4>h4</h4>
296
- <h5>h5</h5>
297
- </div>
298
- <h6>h6</h6>
299
- HTML
300
-
301
243
  def test_nested_toc_with_no_toc_section_class_option
302
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_IGNORE_2, 'no_toc_section_class' => 'exclude')
303
- doc = Nokogiri::HTML(parser.toc)
304
- expected = <<~HTML
244
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML, 'no_toc_section_class' => 'exclude')
245
+ <h1>h1</h1>
246
+ <div class="exclude">
247
+ <h2>h2</h2>
248
+ </div>
249
+ <h3>h3</h3>
250
+ <div class="exclude">
251
+ <h4>h4</h4>
252
+ <h5>h5</h5>
253
+ </div>
254
+ <h6>h6</h6>
255
+ HTML
256
+ expected = <<~HTML.chomp
305
257
  <ul class="section-nav">
306
- <li class="toc-entry toc-h1">
307
- <a href="#h1">h1</a>
258
+ <li class="toc-entry toc-h1"><a href="#h1">h1</a>
308
259
  <ul>
309
- <li class="toc-entry toc-h3">
310
- <a href="#h3">h3</a>
260
+ <li class="toc-entry toc-h3"><a href="#h3">h3</a>
311
261
  <ul>
312
262
  <li class="toc-entry toc-h6"><a href="#h6">h6</a></li>
313
263
  </ul>
@@ -316,8 +266,7 @@ class TestVariousTocHtml < Minitest::Test
316
266
  </li>
317
267
  </ul>
318
268
  HTML
319
- actual = doc.css('ul.section-nav').to_s
320
- assert_equal(expected, actual)
269
+ assert_equal(expected, parser.build_toc)
321
270
 
322
271
  html = parser.inject_anchors_into_html
323
272
  assert_match(%r{<h1>.+</h1>}m, html)
@@ -328,29 +277,24 @@ class TestVariousTocHtml < Minitest::Test
328
277
  assert_includes(html, '<h5>h5</h5>')
329
278
  end
330
279
 
331
- TEST_HTML_IGNORE_3 = <<~HTML
332
- <h1>h1</h1>
333
- <div class="no_toc_section">
334
- <h2>h2</h2>
335
- </div>
336
- <h3>h3</h3>
337
- <div class="exclude">
338
- <h4>h4</h4>
339
- <h5>h5</h5>
340
- </div>
341
- <h6>h6</h6>
342
- HTML
343
-
344
280
  def test_multiple_no_toc_section_classes
345
- parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_IGNORE_3, 'no_toc_section_class' => ['no_toc_section', 'exclude'])
346
- doc = Nokogiri::HTML(parser.toc)
347
- expected = <<~HTML
281
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML, 'no_toc_section_class' => ['no_toc_section', 'exclude'])
282
+ <h1>h1</h1>
283
+ <div class="no_toc_section">
284
+ <h2>h2</h2>
285
+ </div>
286
+ <h3>h3</h3>
287
+ <div class="exclude">
288
+ <h4>h4</h4>
289
+ <h5>h5</h5>
290
+ </div>
291
+ <h6>h6</h6>
292
+ HTML
293
+ expected = <<~HTML.chomp
348
294
  <ul class="section-nav">
349
- <li class="toc-entry toc-h1">
350
- <a href="#h1">h1</a>
295
+ <li class="toc-entry toc-h1"><a href="#h1">h1</a>
351
296
  <ul>
352
- <li class="toc-entry toc-h3">
353
- <a href="#h3">h3</a>
297
+ <li class="toc-entry toc-h3"><a href="#h3">h3</a>
354
298
  <ul>
355
299
  <li class="toc-entry toc-h6"><a href="#h6">h6</a></li>
356
300
  </ul>
@@ -359,8 +303,7 @@ class TestVariousTocHtml < Minitest::Test
359
303
  </li>
360
304
  </ul>
361
305
  HTML
362
- actual = doc.css('ul.section-nav').to_s
363
- assert_equal(expected, actual)
306
+ assert_equal(expected, parser.build_toc)
364
307
 
365
308
  html = parser.inject_anchors_into_html
366
309
  assert_match(%r{<h1>.+</h1>}m, html)
@@ -371,24 +314,20 @@ class TestVariousTocHtml < Minitest::Test
371
314
  assert_includes(html, '<h5>h5</h5>')
372
315
  end
373
316
 
374
- TEST_EXPLICIT_ID = <<~HTML
375
- <h1>h1</h1>
376
- <h1 id="second">h2</h1>
377
- <h1 id="third">h3</h1>
378
- HTML
379
-
380
317
  def test_toc_with_explicit_id
381
- parser = Jekyll::TableOfContents::Parser.new(TEST_EXPLICIT_ID)
382
- doc = Nokogiri::HTML(parser.toc)
383
- expected = <<~HTML
318
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
319
+ <h1>h1</h1>
320
+ <h1 id="second">h2</h1>
321
+ <h1 id="third">h3</h1>
322
+ HTML
323
+ expected = <<~HTML.chomp
384
324
  <ul class="section-nav">
385
325
  <li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
386
326
  <li class="toc-entry toc-h1"><a href="#second">h2</a></li>
387
327
  <li class="toc-entry toc-h1"><a href="#third">h3</a></li>
388
328
  </ul>
389
329
  HTML
390
- actual = doc.css('ul.section-nav').to_s
391
- assert_equal(expected, actual)
330
+ assert_equal(expected, parser.build_toc)
392
331
 
393
332
  html = parser.inject_anchors_into_html
394
333
  assert_includes(html, %(<a class="anchor" href="#h1" aria-hidden="true">))
@@ -396,40 +335,33 @@ class TestVariousTocHtml < Minitest::Test
396
335
  assert_includes(html, %(<a class="anchor" href="#third" aria-hidden="true">))
397
336
  end
398
337
 
399
-
400
- TEST_UNIQ_ID = <<~HTML
401
- <h1>h1</h1>
402
- <h1>h1</h1>
403
- <h1>h1</h1>
404
- HTML
405
-
406
338
  def test_anchor_is_uniq
407
- parser = Jekyll::TableOfContents::Parser.new(TEST_UNIQ_ID)
408
- doc = Nokogiri::HTML(parser.toc)
409
- expected = <<~HTML
339
+ parser = Jekyll::TableOfContents::Parser.new(<<~HTML)
340
+ <h1>h1</h1>
341
+ <h1>h1</h1>
342
+ <h1>h1</h1>
343
+ HTML
344
+ expected = <<~HTML.chomp
410
345
  <ul class="section-nav">
411
346
  <li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
412
347
  <li class="toc-entry toc-h1"><a href="#h1-1">h1</a></li>
413
348
  <li class="toc-entry toc-h1"><a href="#h1-2">h1</a></li>
414
349
  </ul>
415
350
  HTML
416
- actual = doc.css('ul.section-nav').to_s
417
- assert_equal(expected, actual)
351
+
352
+ assert_equal(expected, parser.build_toc)
418
353
  end
419
354
 
420
355
  def test_custom_css_classes
421
356
  parser = Jekyll::TableOfContents::Parser.new(
422
- TEST_HTML_1,
357
+ TEST_HTML,
423
358
  'item_class' => 'custom-item', 'list_class' => 'custom-list', 'sublist_class' => 'custom-sublist', 'item_prefix' => 'custom-prefix-'
424
359
  )
425
- doc = Nokogiri::HTML(parser.toc)
426
- expected = <<~HTML
360
+ expected = <<~HTML.chomp
427
361
  <ul class="custom-list">
428
- <li class="custom-item custom-prefix-h1">
429
- <a href="#h1">h1</a>
362
+ <li class="custom-item custom-prefix-h1"><a href="#h1">h1</a>
430
363
  <ul class="custom-sublist">
431
- <li class="custom-item custom-prefix-h3">
432
- <a href="#h3">h3</a>
364
+ <li class="custom-item custom-prefix-h3"><a href="#h3">h3</a>
433
365
  <ul class="custom-sublist">
434
366
  <li class="custom-item custom-prefix-h6"><a href="#h6">h6</a></li>
435
367
  </ul>
@@ -439,6 +371,6 @@ class TestVariousTocHtml < Minitest::Test
439
371
  </ul>
440
372
  HTML
441
373
 
442
- assert_equal(expected, doc.css('ul.custom-list').to_s)
374
+ assert_equal(expected, parser.build_toc)
443
375
  end
444
376
  end