diff options
author | Robert Speicher <rspeicher@gmail.com> | 2017-09-01 15:04:32 -0400 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2017-09-01 15:08:34 -0400 |
commit | eb0edaf21840392a6bfbf2391632bb0f2d2f7561 (patch) | |
tree | 9522979d8b38f18777bcf8b3582bd884d88ffec5 | |
parent | 3c67b9a35c719b3da89e0dd928d745dea195a430 (diff) | |
download | gitlab-ce-eb0edaf21840392a6bfbf2391632bb0f2d2f7561.tar.gz |
Refactor table of contents buildinganakashima/gitlab-ce-fix_wiki_toc_indent
-rw-r--r-- | lib/banzai/filter/table_of_contents_filter.rb | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/lib/banzai/filter/table_of_contents_filter.rb b/lib/banzai/filter/table_of_contents_filter.rb index 5522986ba21..47151626208 100644 --- a/lib/banzai/filter/table_of_contents_filter.rb +++ b/lib/banzai/filter/table_of_contents_filter.rb @@ -43,15 +43,7 @@ module Banzai end end - if header_root.children.length > 0 - result[:toc] = %q{<ul class="section-nav">} - - header_root.children.each do |child| - push_toc(child) - end - - result[:toc] << '</ul>' - end + push_toc(header_root.children, root: true) doc end @@ -62,19 +54,19 @@ module Banzai %Q{<a id="user-content-#{href}" class="anchor" href="##{href}" aria-hidden="true"></a>} end - def push_toc(header_node) - result[:toc] << %Q{<li><a href="##{header_node.href}">#{header_node.text}</a>} - - if header_node.children.length > 0 - result[:toc] << '<ul>' + def push_toc(children, root: false) + return if children.empty? - header_node.children.each do |child| - push_toc(child) - end + klass = ' class="section-nav"' if root - result[:toc] << '</ul>' - end + result[:toc] << "<ul#{klass}>" + children.each { |child| push_anchor(child) } + result[:toc] << '</ul>' + end + def push_anchor(header_node) + result[:toc] << %Q{<li><a href="##{header_node.href}">#{header_node.text}</a>} + push_toc(header_node.children) result[:toc] << '</li>' end |