Skip to content

Commit

Permalink
Merge pull request #55 from toshimaru/v0.7
Browse files Browse the repository at this point in the history
v0.7.0 Release
  • Loading branch information
toshimaru authored Sep 29, 2018
2 parents 2a79a88 + 3166f8b commit bca82c2
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 33 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
[![Code Climate](https://codeclimate.com/github/toshimaru/jekyll-toc/badges/gpa.svg)](https://codeclimate.com/github/toshimaru/jekyll-toc)
[![Test Coverage](https://api.codeclimate.com/v1/badges/cd56b207f327603662a1/test_coverage)](https://codeclimate.com/github/toshimaru/jekyll-toc/test_coverage)

# Table of Contents

- [Installation](#installation)
- [Usage](#usage)
- [Generated HTML](#generated-html)
- [Customization](#customization)
- [Skip TOC](#skip-toc)
- [TOC levels](#toc-levels)
- [CSS Styling](#css-styling)

# Installation

Add jekyll-toc plugin in your site's `Gemfile`, and run `bundle install`.
Expand Down Expand Up @@ -73,6 +83,8 @@ location with the `toc_only` filter.

## Generated HTML

![screenshot](https://user-images.githubusercontent.com/803398/28401295-0dcfb7ca-6d54-11e7-892b-2f2e6ca755a7.png)

jekyll-toc generates an unordered list. The HTML output is as follows.

```html
Expand All @@ -97,13 +109,9 @@ jekyll-toc generates an unordered list. The HTML output is as follows.
</ul>
```

It looks like the image below.

![screenshot](https://user-images.githubusercontent.com/803398/28401295-0dcfb7ca-6d54-11e7-892b-2f2e6ca755a7.png)

## Customization

### Skip TOC(`no_toc`)
### Skip TOC

The heding is ignored in the toc when you add `no_toc` to the class.

Expand All @@ -113,7 +121,7 @@ The heding is ignored in the toc when you add `no_toc` to the class.
<h2>h2</h2>
```

### TOC level
### TOC levels

The toc levels can be configured on `_config.yml`.

Expand Down
14 changes: 5 additions & 9 deletions lib/table_of_contents/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(html, options = {})
end

def build_toc
%(<ul class="section-nav">\n#{build_toc_list(@entries, last_ul_used: true)}</ul>)
%(<ul class="section-nav">\n#{build_toc_list(@entries)}</ul>)
end

def inject_anchors_into_html
Expand Down Expand Up @@ -68,7 +68,7 @@ def parse_content
end

# Returns the list items for entries
def build_toc_list(entries, last_ul_used: false)
def build_toc_list(entries)
i = 0
toc_list = ''.dup
min_h_num = entries.map { |e| e[:h_num] }.min
Expand All @@ -83,7 +83,7 @@ def build_toc_list(entries, last_ul_used: false)
next_entry = entries[i + 1]
if next_entry[:h_num] > min_h_num
nest_entries = get_nest_entries(entries[i + 1, entries.count], min_h_num)
toc_list << %(\n<ul>\n#{build_toc_list(nest_entries, last_ul_used: true)}</ul>\n)
toc_list << %(\n<ul>\n#{build_toc_list(nest_entries)}</ul>\n)
i += nest_entries.count
end
end
Expand All @@ -92,11 +92,7 @@ def build_toc_list(entries, last_ul_used: false)
elsif entry[:h_num] > min_h_num
# If the current entry should be indented in the list, generate a sublist
nest_entries = get_nest_entries(entries[i, entries.count], min_h_num)
if last_ul_used
toc_list << build_toc_list(nest_entries, last_ul_used: true)
else
toc_list << %(<ul>\n#{build_toc_list(nest_entries, last_ul_used: true)}</ul>\n)
end
toc_list << build_toc_list(nest_entries)
i += nest_entries.count - 1
end
i += 1
Expand All @@ -116,7 +112,7 @@ def get_nest_entries(entries, min_h_num)
end

def toc_headings
@toc_levels.map { |level| "h#{level}" }.join(",")
@toc_levels.map { |level| "h#{level}" }.join(',')
end

def generate_option_hash(options)
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module JekyllToc
VERSION = '0.7.0.beta1'.freeze
VERSION = '0.7.0'.freeze
end
49 changes: 35 additions & 14 deletions test/test_kramdown_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class TestKramdownList < Minitest::Test
# NOTE: kramdown automatically injects `id` attribute
# TODO: test Japanese heading
def test_kramdown_heading
text = <<-MARKDOWN
# h1
Expand All @@ -14,10 +13,27 @@ def test_kramdown_heading
expected = <<-HTML
<h1 id="h1">h1</h1>
<h2 id=\"h2\">h2</h2>
<h2 id="h2">h2</h2>
HTML
actual = Kramdown::Document.new(text).to_html

assert_equal(expected, Kramdown::Document.new(text).to_html)
assert_equal(expected, actual)
end

def test_japanese_heading
text = <<-MARKDOWN
# 日本語見出し1
## 日本語見出し2
MARKDOWN
expected = <<-HTML
<h1 id="section">日本語見出し1</h1>
<h2 id="section-1">日本語見出し2</h2>
HTML
actual = Kramdown::Document.new(text).to_html

assert_equal(expected, actual)
end

def test_kramdown_list_1
Expand Down Expand Up @@ -49,8 +65,9 @@ def test_kramdown_list_1
</li>
</ul>
HTML
actual = Kramdown::Document.new(text).to_html

assert_equal(expected, Kramdown::Document.new(text).to_html)
assert_equal(expected, actual)
end

def test_kramdown_list_2
Expand Down Expand Up @@ -79,8 +96,9 @@ def test_kramdown_list_2
</li>
</ul>
HTML
actual = Kramdown::Document.new(text).to_html

assert_equal(expected, Kramdown::Document.new(text).to_html)
assert_equal(expected, actual)
end

def test_kramdown_list_3
Expand All @@ -95,8 +113,9 @@ def test_kramdown_list_3
* level-3 * level-2 * level-1
</code></pre>
HTML
actual = Kramdown::Document.new(text).to_html

assert_equal(expected, Kramdown::Document.new(text).to_html)
assert_equal(expected, actual)
end

def test_kramdown_list_4
Expand All @@ -119,18 +138,19 @@ def test_kramdown_list_4
<li>level-1</li>
</ul>
HTML
actual = Kramdown::Document.new(text).to_html

assert_equal(expected, Kramdown::Document.new(text).to_html)
assert_equal(expected, actual)
end

def test_kramdown_list_5
text = <<-MARKDOWN
def test_kramdown_list_5
text = <<-MARKDOWN
* level-1
* level-3
* level-2
* level-1
MARKDOWN
expected = <<-HTML
MARKDOWN
expected = <<-HTML
<ul>
<li>level-1
<ul>
Expand All @@ -140,8 +160,9 @@ def test_kramdown_list_5
</li>
<li>level-1</li>
</ul>
HTML
HTML
actual = Kramdown::Document.new(text).to_html

assert_equal(expected, Kramdown::Document.new(text).to_html)
end
assert_equal(expected, actual)
end
end
6 changes: 3 additions & 3 deletions test/test_option_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'test_helper'

class TestOptionError < Minitest::Test
BASE_HTML = "<h1>h1</h1>"
BASE_HTML = '<h1>h1</h1>'
EXPECTED_HTML = <<-HTML
<ul class="section-nav">
<li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
Expand All @@ -18,14 +18,14 @@ def test_option_is_nil
end

def test_option_is_epmty_string
parser = Jekyll::TableOfContents::Parser.new(BASE_HTML, "")
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")
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)
Expand Down
29 changes: 29 additions & 0 deletions test/test_various_toc_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class TestVariousTocHtml < Minitest::Test
<h4 class="no_toc">no_toc h4</h4>
HTML

TEST_JAPANESE_HTML = <<-HTML
<h1></h1>
<h2></h2>
<h3></h3>
HTML

def test_nested_toc
parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_1)
doc = Nokogiri::HTML(parser.toc)
Expand Down Expand Up @@ -177,4 +183,27 @@ def test_no_toc

assert_equal(expected, actual)
end

def test_japanese_toc
parser = Jekyll::TableOfContents::Parser.new(TEST_JAPANESE_HTML)
doc = Nokogiri::HTML(parser.toc)
expected = <<-HTML
<ul class="section-nav">
<li class="toc-entry toc-h1">
<a href="#%E3%81%82"></a>
<ul>
<li class="toc-entry toc-h2">
<a href="#%E3%81%84"></a>
<ul>
<li class="toc-entry toc-h3"><a href="#%E3%81%86"></a></li>
</ul>
</li>
</ul>
</li>
</ul>
HTML
actual = doc.css('ul.section-nav').to_s

assert_equal(expected, actual)
end
end

0 comments on commit bca82c2

Please sign in to comment.