Skip to content

Commit

Permalink
feat: add parameter to open ToC globally or in frontmatter of post (#138
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cruizba authored Oct 31, 2023
1 parent 94f92a6 commit a6ece0e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ disqusShortname = ''
description = "Minimal Hugo blog theme with light and dark mode support"
mainSections = ['posts']
toc = true # set to false to disable table of contents 'globally'
tocOpen = false # set to true to open table of contents by default
goToTop = true # set to false to disable 'go to top' button
additionalScripts = ['js/custom.js', 'js/custom-2.js']
# Will try to load 'assets/js/custom.js' and 'assets/js/custom-2.js'.
Expand Down Expand Up @@ -130,6 +131,7 @@ disqusShortname = ''
description = "Un tema Hugo veloce e minimalista con supporto per la modalità chiara e scura, per la gestione di un sito o di un blog personale"
mainSections = ['posts']
toc = true # set to false to disable table of contents 'globally'
tocOpen = false # set to true to open table of contents by default
goToTop = true # set to false to disable 'go to top' button
additionalScripts = ['js/custom.js', 'js/custom-2.js']
# Will try to load 'assets/js/custom.js' and 'assets/js/custom-2.js'.
Expand Down
19 changes: 19 additions & 0 deletions exampleSite/content/en/posts/table-of-content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,22 @@ To disable ToC on certain posts, you have to follow two steps.
toc: false
---
```

## Open table of content

By default, ToC is closed. To open it by default, set parameter `tocOpen` to `true` in `config.toml`.

```toml
[params]
tocOpen = true
```

Or simply add the `tocOpen` parameter to the front matter of the post.

```yaml
---
title: How to enable table of content
date: 2023-05-02
tocOpen: true
---
```
8 changes: 7 additions & 1 deletion layouts/_default/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ <h1 class="header-title">{{ .Title }}</h1>
</header>
{{- if .Site.Params.toc -}}
{{ if not (eq .Params.toc false) }}
<details class="toc">
{{ $tocOpen := "" }}
{{ if or .Site.Params.tocOpen .Params.tocOpen }}
{{ if not (eq .Params.tocOpen false) }}
{{ $tocOpen = "open" }}
{{ end }}
{{ end }}
<details class="toc" {{ $tocOpen }}>
<summary><b>{{ T "single.table_of_contents" }}</b></summary>
{{ .TableOfContents }}
</details>
Expand Down

0 comments on commit a6ece0e

Please sign in to comment.