Skip to content

Commit

Permalink
Merge pull request #51 from jfredrickson5/master
Browse files Browse the repository at this point in the history
Add custom CSS classes
  • Loading branch information
toshimaru committed Oct 18, 2018
2 parents 4141b82 + df7f317 commit 7bae052
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ toc:
max_level: 5 # default: 6
```

You can apply custom CSS classes to the generated `<ul>` and `<li>` tags.

```yml
toc:
# Default is "section-nav":
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-
```

The default level range is `<h1>` to `<h6>`.

#### CSS Styling
Expand Down
17 changes: 13 additions & 4 deletions lib/table_of_contents/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ class Parser
DEFAULT_CONFIG = {
'no_toc_section_class' => 'no_toc_section',
'min_level' => 1,
'max_level' => 6
'max_level' => 6,
"list_class" => "section-nav",
"sublist_class" => "",
"item_class" => "toc-entry",
"item_prefix" => "toc-"
}.freeze

def initialize(html, options = {})
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
options = generate_option_hash(options)
@toc_levels = options['min_level']..options['max_level']
@no_toc_section_class = options['no_toc_section_class']
@list_class = options["list_class"]
@sublist_class = options["sublist_class"]
@item_class = options["item_class"]
@item_prefix = options["item_prefix"]
@entries = parse_content
end

Expand All @@ -26,7 +34,7 @@ def toc
end

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

def inject_anchors_into_html
Expand Down Expand Up @@ -77,15 +85,16 @@ def build_toc_list(entries)

while i < entries.count
entry = entries[i]
ul_attributes = @sublist_class.empty? ? "" : %( class="#{@sublist_class}")
if entry[:h_num] == min_h_num
# If the current entry should not be indented in the list, add the entry to the list
toc_list << %(<li class="toc-entry toc-#{entry[:node_name]}"><a href="##{entry[:id]}#{entry[:uniq]}">#{entry[:text]}</a>)
toc_list << %(<li class="#{@item_class} #{@item_prefix}#{entry[:node_name]}"><a href="##{entry[:id]}#{entry[:uniq]}">#{entry[:text]}</a>)
# If the next entry should be indented in the list, generate a sublist
if i + 1 < entries.count
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)}</ul>\n)
toc_list << %(\n<ul#{ul_attributes}>\n#{build_toc_list(nest_entries)}</ul>\n)
i += nest_entries.count
end
end
Expand Down
22 changes: 22 additions & 0 deletions test/test_various_toc_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,25 @@ def test_toc_with_explicit_id
assert_includes(html, %(<a id="third" class="anchor" href="#third" aria-hidden="true">))
end
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-" })
doc = Nokogiri::HTML(parser.toc)
expected = <<-HTML
<ul class="custom-list">
<li class="custom-item custom-prefix-h1">
<a href="#h1">h1</a>
<ul class="custom-sublist">
<li class="custom-item custom-prefix-h3">
<a href="#h3">h3</a>
<ul class="custom-sublist">
<li class="custom-item custom-prefix-h6"><a href="#h6">h6</a></li>
</ul>
</li>
</ul>
</li>
</ul>
HTML

assert_equal(expected, doc.css('ul.custom-list').to_s)
end

0 comments on commit 7bae052

Please sign in to comment.