Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build the right-side table of contents from javascript #1049

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion assets/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
@import "taxonomy";
@import "drawio";
@import "shortcodes";
@import "tocbot";
@import "swagger";


@if $td-enable-google-fonts {
@import url($web-font-path);
}
Expand Down
31 changes: 31 additions & 0 deletions assets/scss/tocbot.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.td-toc{overflow-y:auto}.td-toc>.toc-list{overflow:hidden;position:relative}.td-toc>.toc-list li{list-style:none}.toc-list{margin:0;padding-left:10px}a.toc-link{color:currentColor;height:100%;word-wrap:break-word;white-space: normal;}.is-collapsible{max-height:100000px;overflow:hidden;transition:all 300ms ease-in-out}.is-collapsed{max-height:0}.is-position-fixed{position:fixed !important;top:0}.is-active-link{font-weight:700}.toc-link::before{background-color:#EEE;content:' ';display:inline-block;height:inherit;left:0;margin-top:-1px;position:absolute;width:2px}.is-active-link::before{background-color:$primary;}

/*
Tocbot 4.21.0 release (https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.21.0/tocbot.css) is available under the MIT License

CSS is slightly modified for better Docsy integration

When updating to a newer release, retain the license

The MIT License (MIT)

Copyright (c) 2016 Tim Scanlin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
18 changes: 18 additions & 0 deletions layouts/partials/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@
{{ template "algolia/scripts" .Site.Params.search.algolia -}}
{{ end -}}
<script src='{{ "js/tabpane-persist.js" | relURL }}'></script>

{{ if .Site.Params.jstoc.enable }}
<!-- Initialize tocbot -->
<script src="{{ "/js/tocbot.min.js" | relURL }}"></script>
<script type="text/javascript">
tocbot.init({
// 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>
{{ end }}

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

{{ define "algolia/scripts" -}}
Expand Down
26 changes: 26 additions & 0 deletions static/js/tocbot.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -424,3 +424,29 @@ Both `head.html` and `scripts.html` are then used to build Docsy's [base page la
</body>
</html>
```

## 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:

```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

![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.