Skip to content

Commit 64cdc22

Browse files
committed
Display a table of contents in the sidebar of man pages
Adds the table of contents to the frontmatter of the generated HTML pages, then renders it in the Hugo template.
1 parent 7bd9873 commit 64cdc22

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

layouts/partials/sidebar.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@
4949
<a href="{{ relURL "community" }}"{{ if (eq $section "community") }} class="active"{{ end }}>Community</a>
5050
</li>
5151
</ul>
52+
53+
{{ $headings := .Params.headings }}
54+
{{ if $headings }}
55+
<ul>
56+
<li> Table of Contents
57+
<ul class="expanded">
58+
{{ range $i, $item := $headings }}
59+
<li><a href="#{{ .id }}">{{ .text }}</a> </li>
60+
{{ end }}
61+
</ul>
62+
</li>
63+
</ul>
64+
{{ end }}
65+
5266
{{ if (eq .Params.Sidebar "book") }}
5367
<hr class="sidebar">
5468
{{- /* If this page displays a section of the ProGit book, map all the translations thereof */ -}}

script/update-docs.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ def expand_l10n(path, content, get_f_content, categories, ext)
8181
content
8282
end
8383

84+
def extract_headings(html)
85+
doc = Nokogiri::HTML::DocumentFragment.parse(html)
86+
headings = []
87+
88+
doc.css('h2').each do |heading|
89+
headings << {
90+
'text' => heading.text.strip,
91+
'id' => heading['id']
92+
}
93+
end
94+
95+
headings
96+
end
97+
8498
def index_l10n_doc(filter_tags, doc_list, get_content)
8599
rebuild = ENV.fetch("REBUILD_DOC", nil)
86100
rerun = ENV["RERUN"] || rebuild || false
@@ -220,6 +234,11 @@ def index_l10n_doc(filter_tags, doc_list, get_content)
220234
"aliases" => ["/docs/#{path}/#{lang}/index.html"]
221235
}
222236

237+
headings = extract_headings(html)
238+
if headings.length > 0
239+
front_matter['headings'] = headings
240+
end
241+
223242
FileUtils.mkdir_p(doc_path)
224243
File.open("#{doc_path}/#{lang}.html", "w") do |out|
225244
out.write(wrap_front_matter(front_matter))
@@ -525,6 +544,11 @@ def index_doc(filter_tags, doc_list, get_content)
525544
"version" => doc_versions[changed_in],
526545
}
527546

547+
headings = extract_headings(html)
548+
if headings.length > 0
549+
front_matter['headings'] = headings
550+
end
551+
528552
if changed_in != doc_version_index && File.exist?("#{doc_path}/#{version}.html")
529553
# remove obsolete file
530554
File.delete("#{doc_path}/#{version}.html")
@@ -736,3 +760,4 @@ def local_index_doc(index_fun)
736760
else
737761
abort("Need two arguments: (<path-to-repo> | remote) (en | l10n)!")
738762
end
763+

0 commit comments

Comments
 (0)