Skip to content

Commit

Permalink
fix: fix formatting issue while using nested toc shortcodes (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored May 22, 2022
1 parent 6ab98d1 commit 6b46420
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 34 deletions.
2 changes: 1 addition & 1 deletion exampleSite/content/en/shortcodes/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Hint shortcode can be used as hint/alerts/notification block.

Attributes:

| Name | Usage | default |
| Name | Usage | Default |
| ---------------- | --------------------------------------------------------------------------------- | --------------------- |
| type | color types to choose from | note |
| icon (optional) | custom icon to use | undefined (type name) |
Expand Down
2 changes: 1 addition & 1 deletion exampleSite/content/en/shortcodes/images/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ If you need more flexibility for your embedded images, you could use the `img` s

**Attributes:**

| Name | Usage | default |
| Name | Usage | Default |
| ------------- | ------------------------------------------------------------------------------------------------------------- | ----------------- |
| name (string) | Name of the image resource defined in your front matter. | empty |
| alt (string) | Description for displayed image. | resource `.Title` |
Expand Down
2 changes: 1 addition & 1 deletion exampleSite/content/en/shortcodes/includes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Include shortcode can include files of different types. By specifying a language

Attributes:

| Name | Usage | default |
| Name | Usage | Default |
| -------- | ----------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- |
| file | path to the included file relative to the Hugo root | undefined |
| language | language for [syntax highlighting](https://gohugo.io/content-management/syntax-highlighting/#list-of-chroma-highlighting-languages) | undefined |
Expand Down
12 changes: 11 additions & 1 deletion exampleSite/content/en/shortcodes/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ title: ToC

Simple wrapper to generate a page Table of Content from a shortcode.

**Attributes:**

| Name | Usage | Default |
| ------ | -------------------------- | ------------------------------------------------------------- |
| format | format of the returned ToC | <!-- spellchecker-disable -->html<!-- spellchecker-enable --> |

**Syntax:**

<!-- prettier-ignore-start -->
```tpl
{{</* toc */>}}
{{</* toc (format=[html|raw]) */>}}
```
<!-- prettier-ignore-end -->

**Example:**

{{< toc >}}

## Level 1
Expand Down
2 changes: 1 addition & 1 deletion layouts/shortcodes/expand.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
</label>
<input id="{{ $id }}-{{ .Ordinal }}" type="checkbox" class="gdoc-expand__control hidden" />
<div class="gdoc-markdown--nested gdoc-expand__content">
{{ .Inner | $.Page.RenderString }}
{{ .Inner | $.Page.RenderString | htmlUnescape | safeHTML }}
</div>
</div>
44 changes: 21 additions & 23 deletions layouts/shortcodes/toc-tree.html
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
{{ $tocLevels := default (default 6 .Site.Params.GeekdocToC) .Page.Params.GeekdocToC }}
{{- $tocLevels := default (default 6 .Site.Params.GeekdocToC) .Page.Params.GeekdocToC }}

{{ if $tocLevels }}
{{- if $tocLevels }}
<div class="gdoc-toc gdoc-toc__level--{{ $tocLevels }}">
{{ template "toc-tree" dict "sect" .Page.Pages }}
</div>
{{ end }}
{{- end }}


<!-- templates -->
{{ define "toc-tree" }}
{{- define "toc-tree" }}
<ul>
{{ range .sect.GroupBy "Weight" }}
{{ range .ByTitle }}
{{ if or (not .Params.GeekdocHidden) (not (default true .Params.GeekdocHiddenTocTree)) }}
{{- range .sect.GroupBy "Weight" }}
{{- range .ByTitle }}
{{- if or (not .Params.GeekdocHidden) (not (default true .Params.GeekdocHiddenTocTree)) }}
<li>
{{ if or .Content .Params.GeekdocFlatSection }}
{{- if or .Content .Params.GeekdocFlatSection }}
<span>
<a href="{{ .RelPermalink }}" class="gdoc-toc__entry">
{{ partial "utils/title" . }}{{ with .Params.GeekdocDescription }}:{{ end }}
{{- partial "utils/title" . }}{{ with .Params.GeekdocDescription }}:{{ end }}
</a>
{{ with .Params.GeekdocDescription }}{{ . }}{{ end }}
{{- with .Params.GeekdocDescription }}{{ . }}{{ end }}
</span>
{{ else }}
{{- else -}}
<span>
{{ partial "utils/title" . }}{{ with .Params.GeekdocDescription }}
: {{ . }}
{{ end }}
{{- partial "utils/title" . }}{{ with .Params.GeekdocDescription }}: {{ . }}{{ end }}
</span>
{{ end }}
{{- end -}}

{{ $numberOfPages := (add (len .Pages) (len .Sections)) }}
{{ if and (ne $numberOfPages 0) (not .Params.GeekdocFlatSection) }}
{{ template "toc-tree" dict "sect" .Pages }}
{{ end }}
{{- $numberOfPages := (add (len .Pages) (len .Sections)) }}
{{- if and (ne $numberOfPages 0) (not .Params.GeekdocFlatSection) }}
{{- template "toc-tree" dict "sect" .Pages }}
{{- end }}
</li>
{{ end }}
{{ end }}
{{ end }}
{{- end }}
{{- end }}
{{- end }}
</ul>
{{ end }}
{{- end }}
17 changes: 11 additions & 6 deletions layouts/shortcodes/toc.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{{ $tocLevels := default (default 6 .Site.Params.GeekdocToC) .Page.Params.GeekdocToC }}
{{- $format := default "html" (.Get "format") }}
{{- $tocLevels := default (default 6 .Site.Params.GeekdocToC) .Page.Params.GeekdocToC }}

{{ if and $tocLevels .Page.TableOfContents }}
<div class="gdoc-toc gdoc-toc__level--{{ $tocLevels }}">
{{- if and $tocLevels .Page.TableOfContents -}}
{{- if not (eq ($format | lower) "raw") -}}
<div class="gdoc-toc gdoc-toc__level--{{ $tocLevels }}">
{{ .Page.TableOfContents }}
<hr />
</div>
{{- else -}}
{{ .Page.TableOfContents }}
<hr />
</div>
{{ end }}
{{- end -}}
{{- end -}}

0 comments on commit 6b46420

Please sign in to comment.