Skip to content

Commit db6abf2

Browse files
fekete-robertchalin
authored andcommitted
Updates generating right-hand toc to use tocbot
1 parent a824b2a commit db6abf2

File tree

6 files changed

+82
-1
lines changed

6 files changed

+82
-1
lines changed

assets/scss/_tocbot.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Used to customize right-hand sidebar when using Tocbot
2+
// Override active link color
3+
.is-active-link::before {
4+
background-color: $primary !important;
5+
}

assets/scss/main.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
@import "shortcodes";
3737
@import "swagger";
3838
@import "support/rtl";
39-
39+
@import "tocbot";
4040

4141
@if $td-enable-google-fonts {
4242
@import url($web-font-path);

layouts/_partials/scripts.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@
7474
{{ template "algolia/scripts" .Site.Params.search.algolia -}}
7575
{{ end -}}
7676
<script src='{{ "js/tabpane-persist.js" | relURL }}'></script>
77+
78+
{{ if .Site.Params.jstoc.enable -}}
79+
{{- partial "scripts/jstoc.html" . -}}
80+
{{ end -}}
81+
7782
{{ partial "hooks/body-end.html" . -}}
7883

7984
{{ define "algolia/scripts" -}}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{{ $version := .Site.Params.jstoc.version | default "4.34.0" -}}
2+
{{ $hash := .Site.Params.jstoc.hash | default "sha512-EMZRx2Hno6csUwgZoOMjyKDp1UdNRrCQH0R97VEPYynqgSUrRItJn73HnRUwqujCat2/Zg731UEw+7xDXAL1ag==" -}}
3+
{{ $css_hash := .Site.Params.jstoc.css_hash | default "sha512-4q0OX9NAYcRTFEfy9nTK0AV9N7MxM665neDXEW3CjAj1pXc6+8Bcd6ryXl6cY8mTBBXt0aXepnSDLLQZSuJRww==" -}}
4+
{{ $css_url := printf "https://cdnjs.cloudflare.com/ajax/libs/tocbot/%s/tocbot.min.css" $version -}}
5+
{{ $cdn_url := printf "https://cdnjs.cloudflare.com/ajax/libs/tocbot/%s/tocbot.min.js" $version -}}
6+
7+
{{ with try (resources.GetRemote $css_url) -}}
8+
{{ with .Err -}}
9+
{{ errorf "Could not retrieve Tocbot css file from CDN. Reason: %s." . -}}
10+
{{ else with.Value -}}
11+
{{ with resources.Copy (printf "css/tocbot.min.css" ) . }}
12+
{{ $secureCSS := . | resources.Fingerprint "sha512" -}}
13+
<link rel="stylesheet" href="{{- .RelPermalink -}}" integrity="{{- $secureCSS.Data.Integrity -}}" crossorigin="anonymous" referrerpolicy="no-referrer">
14+
{{ end -}}
15+
{{ end -}}
16+
{{ else -}}
17+
{{ errorf "Invalid Tocbot version %s, could not retrieve this version from CDN." $version -}}
18+
{{ end -}}
19+
20+
{{ with try (resources.GetRemote $cdn_url) -}}
21+
{{ with .Err -}}
22+
{{ errorf "Could not retrieve Tocbot script from CDN. Reason: %s." . -}}
23+
{{ else with.Value -}}
24+
{{ with resources.Copy (printf "js/tocbot.min.js" ) . }}
25+
{{ $secureJS := . | resources.Fingerprint "sha512" -}}
26+
<script src="{{- .RelPermalink -}}" integrity="{{- $secureJS.Data.Integrity -}}" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
27+
{{ end -}}
28+
{{ end -}}
29+
{{ else -}}
30+
{{ errorf "Invalid Tocbot version %s, could not retrieve this version from CDN." $version -}}
31+
{{ end -}}
32+
33+
<!-- Initialize tocbot -->
34+
<script type="text/javascript">
35+
tocbot.init({
36+
// Where to render the table of contents.
37+
tocSelector: '.td-toc',
38+
// Where to grab the headings to build the table of contents.
39+
contentSelector: '.td-content',
40+
// Which headings to grab inside of the contentSelector element.
41+
headingSelector: '{{ with .Site.Params.jstoc.custom_headings }}{{ . }}{{ else }}h2, h3, h4{{ end }}',
42+
// ignore headings that are hidden in DOM
43+
ignoreHiddenElements: true
44+
});
45+
</script>

userguide/content/en/docs/adding-content/lookandfeel.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,29 @@ feature of Hugo in your configuration file, or in the front matter of the
555555
highest-level page you want to modify.
556556

557557
[bs-docs]: https://getbootstrap.com/docs/
558+
559+
## Sidebar table of contents
560+
561+
By default, Docsy shows the table of contents for the current page in the right sidebar using the built-in function of Hugo. You can replace that with a JavaScript-based ToC that uses the [https://tscanlin.github.io/tocbot/](Tocbot library) by setting the following in your `config.toml` file:
562+
563+
```toml
564+
[params.jstoc]
565+
enable = true
566+
```
567+
568+
By default, h2-h4 headings are included in the sidebar, which includes tips and warnings if you are using the [alert shortcode of Docsy](/docs/adding-content/shortcodes/#alert). To change that,
569+
provide a comma-separated list of the headings to include in the `custom_headings` parameter, for example:
570+
571+
```toml
572+
[params.jstoc]
573+
enable = true
574+
custom_headings = "h2, h3"
575+
```
576+
577+
Compared to the default sidebar ToC, this solution:
578+
579+
- has a marker that shows the current location of the screen (useful for long pages)
580+
- shows the correct title even if the title contains a shortcode
581+
- shows the title in the toc even if it was included from another file
582+
583+
![Screenshot of JavaScript-based sidebar table of contents](/images/sidebar-toc-with-tocbot.png)
257 KB
Loading

0 commit comments

Comments
 (0)