docs/layouts/partials/sidebar.html

63 lines
2.5 KiB
HTML

{{/* Parse toc.yaml and store the resulting map to $scratch */}}
{{ $scratch := partialCached "utils/tocparser.html" . . }}
{{ $ctx := . }}
{{/* Get the name of the first section */}}
{{ $firstSection := (index ($scratch.GetSortedMapValues "sections") 0).title }}
{{/* Render the top-nav in sidebar for small screens */}}
<nav class="text-sm py-4 hidden gap-4 pl-4 md:flex flex-col justify-evenly">
<div class="text-gray-light dark:text-gray-dark">Main sections</div>
{{ range site.Menus.main }}
<div class="pl-2 underline-offset-8 decoration-2 hover:underline decoration-blue-light dark:decoration-blue-dark hover:opacity-75
{{- if eq $firstSection .Name }}
underline
{{- end }}">
<a href="{{ .URL }}">{{ .Name }}</a>
</div>
{{ end }}
</nav>
{{ if $firstSection }}
<hr>
<nav id="sectiontree" class="text-sm w-[300px] md:w-full flex flex-col gap-2 px-3">
<div class="hidden md:block pt-4 text-gray-light dark:text-gray-dark">This section</div>
{{/* The current page is in the table of contents */}}
<ul>
{{/* Walk the toc.yaml nodes under the current main section */}}
{{ range (index site.Data.toc $firstSection) }}
{{ template "tocRender" (dict "ctx" $ctx "entry" .) }}
{{ end }}
</ul>
</nav>
{{ end }}
{{/* Recursive template for sidebar items */}}
{{ define "tocRender" }}
{{ $ctx := .ctx }}
{{ if .entry.sectiontitle }}
{{/* .entry is a section */}}
<li x-data="{ expanded: false }" x-init="expanded = !!$el.querySelector('a[aria-current]')" :data-expanded="expanded">
<button @click="expanded = ! expanded" class="rounded px-4 sidebar-hover w-full flex items-center justify-between">
<span class="py-2 truncate">{{ markdownify .entry.sectiontitle }}</span>
<span class="material-symbols-rounded -my-1 md:text-[28px]" x-text="expanded ? 'expand_less' : 'expand_more'">
</span>
</button>
<ul x-show="expanded" x-collapse class="ml-3 md:ml-[21px]">
{{ range .entry.section }}
{{ template "tocRender" (dict "entry" . "ctx" $ctx) }}
{{ end }}
</ul>
</li>
{{ else }}
{{/* .entry is a page */}}
{{ $isCurrent := eq (urls.Parse $ctx.Permalink).Path .entry.path }}
<li class="pl-4 sidebar-hover rounded
{{ if $isCurrent }} bg-gray-light-200 dark:bg-gray-dark-200{{ end }}">
<a {{ if $isCurrent }}aria-current="page" {{ end }} class="py-2 w-full truncate block"
href="{{ .entry.path }}" title="{{ markdownify .entry.title }}"
><span>{{ markdownify .entry.title }}</span>
</a>
</li>
{{ end }}
{{ end }}