Skip to content

Commit

Permalink
feat: add ability to enable toc on certain posts ignore global toc se…
Browse files Browse the repository at this point in the history
…ttings (#143)

* refactor: move toc sections into own partials file

* feat: add ability to show toc for specific post while toc globally off

* feat: update toc documentation

* feat: update ToC info in russian content

* feat: add notice about priority of post params before global
  • Loading branch information
kirill-zak authored Dec 3, 2023
1 parent e96f650 commit d2a95ac
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
22 changes: 22 additions & 0 deletions exampleSite/content/en/posts/table-of-content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ description: Setup table of content in Hugo blog awesome theme

This theme supports displaying table of content (ToC) in blog posts.

## Parameters

You can manage a ToC with two parameters:
- global `toc` parameter;
- post `toc` parameter.

The post `toc` parameter has higher priority than the global `toc` parameter.

## Enable table of content on all posts

To enable ToC on all posts (globally) set parameter `toc` to `true` in `config.toml`.
Expand All @@ -19,10 +27,24 @@ To enable ToC on all posts (globally) set parameter `toc` to `true` in `config.t

To disable ToC globally, simply ignore the `toc` parameter or set it to `false`.

## Enable table of content on certain posts

To enable ToC on certain posts set parameter `toc` to `true` in post settings.

```yaml
---
title: How to enable table of content
date: 2023-05-02
toc: true
---
```

## Disable table of content on certain posts

To disable ToC on certain posts, you have to follow two steps.

Notice: `.Params.toc` in the post will overide `.Site.Params.toc`. After these steps, parameter `toc` in the post will be `false`.

1. Set parameter `toc` to `true` in `config.toml`.

```toml
Expand Down
22 changes: 21 additions & 1 deletion exampleSite/content/ru/posts/table-of-content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ description: Установка содержания для Hugo темы awesom

Эта тема поддерживает отображение содержания для записей в блоге.

## Параметры

Выв можете управлять содержимом с помощью двух параметров:
- глобальный `toc` параметр;
- параметр `toc` записи.

Параметр `toc` записи имеет более высокий приоритет чем глобальный `toc` параметр.

## Включение содержания для всех записей

Для включения содержания для всех записей (глобально) установите параметр`toc` в значение `true` в `config.toml`.
Expand All @@ -18,7 +26,19 @@ description: Установка содержания для Hugo темы awesom
```

Для отключения содержания глобально, просто проигнорируйте параметр `toc` или установите значение `false`.


## Включение содержания для определённых записей

Для включения содержания для определённых записей, установите параметр `toc` в значение `true` в настройках записи.

```yaml
---
title: Как включить содержание
date: 2023-05-02
toc: true
---
```

## Отключение содержания для определённых записей

Для отключения содержания для определённых записей, Вам необходимо выполнить два шага.
Expand Down
15 changes: 1 addition & 14 deletions layouts/_default/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,7 @@ <h1 class="header-title">{{ .Title }}</h1>
</div>
{{ end }}
</header>
{{- if .Site.Params.toc -}}
{{ if not (eq .Params.toc false) }}
{{ $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>
{{ end }}
{{- end -}}
{{ partial "toc.html" .}}
<div class="page-content">
{{ .Content }}
</div>
Expand Down
18 changes: 18 additions & 0 deletions layouts/partials/toc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{- $toc := .Site.Params.toc -}}

{{- if isset .Params "toc" -}}
{{ $toc = .Params.toc }}
{{- end -}}

{{- if $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>
{{- end -}}

0 comments on commit d2a95ac

Please sign in to comment.