Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions assets/scss/_tocbot.scss
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with the jstoc.js file name, this should be named _jstoc.scss.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Tocbot style customization

.is-active-link::before {
background-color: $primary !important;
}
2 changes: 1 addition & 1 deletion assets/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@import "shortcodes";
@import "swagger";
@import "support/rtl";

@import "tocbot";

@if $td-enable-google-fonts {
@import url($web-font-path);
Expand Down
5 changes: 5 additions & 0 deletions layouts/_partials/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
{{ template "algolia/scripts" .Site.Params.search.algolia -}}
{{ end -}}
<script src='{{ "js/tabpane-persist.js" | relURL }}'></script>

{{ if .Site.Params.jstoc.enable -}}
{{- partial "scripts/jstoc.html" . -}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{{- partial "scripts/jstoc.html" . -}}
{{ partial "scripts/jstoc.html" . -}}

{{ end -}}

{{ partial "hooks/body-end.html" . -}}

{{ define "algolia/scripts" -}}
Expand Down
44 changes: 44 additions & 0 deletions layouts/_partials/scripts/jstoc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{{ $version := .Site.Params.jstoc.version | default "latest" -}}

{{ $css_url := printf "https://cdn.jsdelivr.net/npm/tocbot@%s/dist/tocbot%s.css" $version (cond hugo.IsProduction ".min" "") -}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor the code to define $resourceBaseName as tocbot%s using (cond hugo.IsProduction ".min" ""). Note that this variable will be used in four places.

{{ $cdn_url := printf "https://cdn.jsdelivr.net/npm/tocbot@%s/dist/tocbot%s.js" $version (cond hugo.IsProduction ".min" "") -}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{{ $cdn_url := printf "https://cdn.jsdelivr.net/npm/tocbot@%s/dist/tocbot%s.js" $version (cond hugo.IsProduction ".min" "") -}}
{{ $js_url := printf "https://cdn.jsdelivr.net/npm/tocbot@%s/dist/tocbot%s.js" $version (cond hugo.IsProduction ".min" "") -}}


{{ with try (resources.GetRemote $css_url) -}}
{{ with .Err -}}
{{ errorf "Could not retrieve Tocbot css file from CDN. Reason: %s." . -}}
{{ else with.Value -}}
{{ with resources.Copy (printf "css/tocbotx%s.css" (cond hugo.IsProduction ".min" "")) . }}
{{ $secureCSS := . | resources.Fingerprint "sha512" -}}
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ $secureCSS.Data.Integrity }}" crossorigin="anonymous" referrerpolicy="no-referrer">
{{ end -}}
{{ end -}}
{{ else -}}
{{ errorf "Invalid Tocbot version %s, could not retrieve this version from CDN." $version -}}
{{ end -}}

{{ with try (resources.GetRemote $cdn_url) -}}
{{ with .Err -}}
{{ errorf "Could not retrieve Tocbot script from CDN. Reason: %s." . -}}
{{ else with.Value -}}
{{ with resources.Copy (printf "js/tocbot%s.js" (cond hugo.IsProduction ".min" "")) . }}
{{ $secureJS := . | resources.Fingerprint "sha512" -}}
<script src="{{ .RelPermalink }}" integrity="{{ $secureJS.Data.Integrity }}" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
{{ end -}}
{{ end -}}
{{ else -}}
{{ errorf "Invalid Tocbot version %s, could not retrieve this version from CDN." $version -}}
{{ end -}}

<!-- Initialize tocbot -->
<script type="text/javascript">
tocbot.init({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use two spaces as the default indentation.

// Where to render the table of contents.
tocSelector: '.td-toc',
// Where to grab the headings to build the table of contents.
contentSelector: '.td-content',
// Which headings to grab inside of the contentSelector element.
headingSelector: '{{ with .Site.Params.jstoc.custom_headings }}{{ . }}{{ else }}h2, h3, h4{{ end }}',
// ignore headings that are hidden in DOM
ignoreHiddenElements: true
});
</script>
26 changes: 26 additions & 0 deletions userguide/content/en/docs/adding-content/lookandfeel.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,29 @@ feature of Hugo in your configuration file, or in the front matter of the
highest-level page you want to modify.

[bs-docs]: https://getbootstrap.com/docs/

## Right sidebar table of contents

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:

```toml
[params.jstoc]
enable = true
```

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,
provide a comma-separated list of the headings to include in the `custom_headings` parameter, for example:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
provide a comma-separated list of the headings to include in the `custom_headings` parameter, for example:
provide a comma-separated list of the headings to include in the `headings` parameter, for example:

Fix here and elsewhere.


```toml
[params.jstoc]
enable = true
custom_headings = "h2, h3"
```

Compared to the default sidebar ToC, this solution:

- has a marker that shows the current location of the screen (useful for long pages)
- shows the correct title even if the title contains a shortcode
- shows the title in the toc even if it was included from another file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: this is no longer a limitation when the include functionality is properly coded.


![Screenshot of JavaScript-based sidebar table of contents](/images/sidebar-toc-with-tocbot.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading