Created
October 12, 2023 13:59
-
-
Save erdii/ed087efd9495b53ffdb17d6e2c2d72f4 to your computer and use it in GitHub Desktop.
hugo pko dev docs toc example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ $startLevel := .Site.Params.tocStartLevel | default 2 }} | |
{{ $endLevel := .Site.Params.tocEndLevel | default 3 }} | |
{{ $tagRe := printf "h[%d-%d]" $startLevel $endLevel }} | |
{{ $tocRe := printf "<%s.*?>(.|\n])+?</%s>" $tagRe $tagRe }} | |
{{ $headers := findRE $tocRe .Content }} | |
<nav id="TableOfContents"> | |
<ul> | |
<li class="nav-h0"><a href="#page-top">{{.Title}}</a></li> | |
{{ range $headers }} | |
{{ $tagname := substr . 1 2 }} | |
{{ $header := . | plainify | htmlUnescape }} | |
<li class="toc-{{$tagname}}"> | |
<a href='#{{ $header | anchorize }}'>{{ $header }}</a> | |
</li> | |
{{ end }} | |
</ul> | |
</nav> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment