Skip to content

Commit

Permalink
Automatically expand short in-page tables of contents (#367)
Browse files Browse the repository at this point in the history
Most tables of contents within a single documentation page are quite
short, so having them start out collapsed just requires extra clicks.
This expands them just enough that they're no longer than
the (smallest possible version of) the global table of contents.
  • Loading branch information
nex3 committed Sep 6, 2019
1 parent 835ff5a commit add846d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@
ul.elements.first.elements.first['href'] = a['href']
end

# Expand the page's table of contents to the deepest level possible without
# making it longer than the most-collapsed-possible documentation table of
# contents.
entries = fragment.css("> li")
total_entries = entries.count
loop do
child_entries = entries.css("> ul > li")
total_entries += child_entries.count
break if total_entries > data.documentation.toc.count

sections = entries.xpath("a[following-sibling::ul]")
sections.add_class("section open")
break if sections.empty?

entries = child_entries
end

current_page.add_metadata(table_of_contents: fragment.to_html)
body
end
Expand Down

0 comments on commit add846d

Please sign in to comment.