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

Testing whether its simple to set the table of contents depth level. #222

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 14 additions & 20 deletions themes/hextra/layouts/partials/toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<p class="hx-mb-4 hx-font-semibold hx-tracking-tight">{{ $onThisPage }}</p>
{{- range . -}}
<ul>
{{- with .Headings -}}{{ template "toc-subheading" (dict "headings" . "level" 0) }}{{- end -}}
{{- with .Headings -}}
{{- template "toc-subheading" (dict "headings" . "level" 1 "maxDepth" 2) -}}
{{- end -}}
</ul>
{{- end -}}
{{- end -}}
Expand Down Expand Up @@ -54,28 +56,20 @@
{{ end -}}
</nav>

{{/* TOC subheadings component. This is a recursive component that renders a list of headings. */}}
{{/* Define the toc-subheading template */}}
{{- define "toc-subheading" -}}
{{- $headings := .headings -}}
{{- $level := .level -}}
{{- if ge $level 6 -}}
{{ return }}
{{- end -}}

{{- $padding := (mul $level 4) -}}
{{- $class := cond (eq $level 0) "hx-font-semibold" (printf "ltr:hx-pl-%d rtl:hx-pr-%d" $padding $padding) -}}

{{- range $headings }}
{{- if .Title }}
<li class="hx-my-2 hx-scroll-my-6 hx-scroll-py-6">
<a class="{{ $class }} hx-inline-block hx-text-gray-500 hover:hx-text-gray-900 dark:hx-text-gray-400 dark:hover:hx-text-gray-300 contrast-more:hx-text-gray-900 contrast-more:hx-underline contrast-more:dark:hx-text-gray-50 hx-w-full hx-break-words" href="#{{ anchorize .ID }}">
{{- .Title | safeHTML | plainify | htmlUnescape }}
</a>
{{- $maxDepth := .maxDepth -}}
{{- range .headings -}}
{{- if le $level $maxDepth -}}
<li>
<a href="#{{ .ID }}">{{ .Title }}</a>
{{- with .Headings -}}
<ul>
{{- template "toc-subheading" (dict "headings" . "level" (add $level 1) "maxDepth" $maxDepth) -}}
</ul>
{{- end -}}
</li>
{{- end -}}
{{- with .Headings -}}
{{ template "toc-subheading" (dict "headings" . "level" (add $level 1)) }}
{{- end -}}

{{- end -}}
{{- end -}}