{{- /*
  Renders a table of contents (ToC) for the page.
  - Uses `.Fragments.Headings` to generate a nested ToC if headings exist and `notoc` is not set to `true`.
  - Limits heading levels to a min and max range (`$min` and `$max`).
  - Wraps the ToC in a `data-pagefind-ignore` container to exclude it from search indexing.
  - Includes a recursive template (`walkHeadingFragments`) to handle nested headings.
  */
-}}
{{- $toc := false }}
{{- with .Fragments }}
  {{- $toc = and (ne page.Params.notoc true) .Headings }}
{{- end }}
{{- with $toc }}
  <div data-pagefind-ignore class="not-prose">
    <div class="pb-0 text-lg lg:pb-2">
      {{ T "tableOfContents" }}
    </div>
    <nav class="toc">
      {{ $root := (index page.Fragments.Headings 0).Headings }}
      {{- template "walkHeadingFragments" $root }}
    </nav>
  </div>
{{- end }}

{{- define "walkHeadingFragments" }}
  {{- $min := default 2 page.Params.toc_min }}
  {{- $max := default 3 page.Params.toc_max }}
  <ul class="pl-2">
    {{- range . }}
      {{- if and (ge .Level $min) (le .Level $max) }}
        <li>
          <a class="link lg:no-underline" href="#{{ .ID }}"
            >{{ markdownify .Title }}</a
          >
        </li>
      {{- end }}
      {{- with .Headings }}
        {{ template "walkHeadingFragments" . }}
      {{- end }}
    {{- end }}
  </ul>
{{- end }}
