implement TableOfContents in sidebar for posts with more than 400 words

This commit is contained in:
John Bowdre 2021-12-06 20:57:34 -06:00
parent f6b62ea767
commit ce8755c3d3
4 changed files with 175 additions and 0 deletions

View file

@ -15,3 +15,7 @@
noClasses = false
# style = "monokai"
tabWidth = 2
[tableOfContents]
endlevel = 6
ordered = false
startLevel = 3

View file

@ -81,6 +81,7 @@ iconsDir = "images/icons/" # without a leading forward slash
# Show related content at the end of an article based on the 'series' taxonomy. Can be set in post front matter.
# showRelatedInArticle = false
# showRelatedInSidebar = false
toc = true
# website author
[author]

View file

@ -0,0 +1,54 @@
{{- define "main" }}
{{- $s := .Site.Params }}
{{- $p := .Params }}
{{- $scratch := newScratch }}
{{- if isset $p "image" }}
{{- $scratch.Set "image" $p.image }}
{{- else }}
{{ $scratch.Set "image" $s.fallBackOgImage }}
{{- end }}
{{- $image := $scratch.Get "image" }}
{{- $bg := absLangURL (path.Join "images" $image) }}
<div class="{{ if ne $p.singleColumn true }}grid-inverse {{ end }}wrap content">
<article class="post_content">
{{- $t := .Title }}
<h1 class="post_title">{{ $t }}</h1>
{{- partial "post-meta" . }}
{{- partial "image-feature" . }}
<!-- {{ if $p.toc }}
<div class="post_toc">
<h2>{{ T "overview" }}</h2>
{{ .TableOfContents }}
</div>
{{ end }} -->
<div class="post_body">
{{- .Content }}
</div>
{{- $showRelatedInArticle := true }}
{{- if eq $s.showRelatedInArticle false }}
{{- $showRelatedInArticle = false }}
{{- else if eq $p.showRelatedInArticle false }}
{{- $showRelatedInArticle = false }}
{{- end }}
{{- if ne $showRelatedInArticle false }}
{{- partial "related" . }}
{{- end }}
{{- $showComments := true }}
{{- if eq $s.comments false }}
{{- $showComments = false }}
{{- else if eq $p.comments false }}
{{- $showComments = false }}
{{- end }}
{{- if ne $showComments false }}
{{- partial "comments" . }}
{{- end }}
{{- partial "i18nlist" . }}
</article>
{{- if ( ne $p.sidebar false ) }}
{{- partial "sidebar" . }}
{{ end }}
</div>
{{- end }}

View file

@ -0,0 +1,116 @@
{{ $s := site.Params }}
<aside class="sidebar">
<section class="sidebar_inner">
{{- $introDescription := $s.introDescription }}
{{- with .Params.introDescription }}
{{- $introDescription = . }}
{{- end }}
{{- if $introDescription }}
{{- $author := $s.Author }}
{{- $showAuthorPhoto := false }}
{{- $fullAuthor := reflect.IsMap $author }}
{{- if $fullAuthor }}
{{- if $author.photo }}
{{- $showAuthorPhoto = true }}
{{- end }}
{{- end }}
{{- if $showAuthorPhoto }}
<div class="author_header">
<img src="{{ absURL $author.photo }}" alt="{{ $author.name }} photo">
<h2>{{ $author.name }}</h2>
</div>
{{- else }}
<h2>{{ if $fullAuthor }}{{ $author.name }}{{ else }}{{ $author }}{{ end }}</h2>
{{- end }}
<div class="author_bio">
{{ markdownify $introDescription }}
</div>
{{- if ( ne $s.introURL false ) }}
{{- $r := T "read_more" }}
<a href='{{ absLangURL (default "about/" $s.introURL) }}' class="button mt-1" role="button" title='{{ $r }}'>{{ $r }}</a>
{{- end }}
{{- end }}
{{ if and (gt .WordCount 400 ) ($s.toc) }}
<div class="post_toc">
<h2>{{ T "overview" }}</h2>
{{ .TableOfContents }}
</div>
{{ end }}
{{- $relatedInSidebar := true }}
{{- if eq $s.showRelatedInSidebar false }}
{{ $relatedInSidebar = false }}
{{- end }}
{{ if (and ($relatedInSidebar) (isset .Params "series") ) }}
{{$related := where .Site.RegularPages ".Params.series" "eq" .Params.series }}
<h2 class="mt-4">{{ T "series_posts" }}</h2>
<ul>
{{ range $related }}
<li>
<a href="{{ .Permalink }}" class="nav-link" title="{{ .Title }}">{{ .Title }}</a>
</li>
{{ end }}
</ul>
{{ end }}
{{- $posts := where (where .Site.RegularPages "Permalink" "!=" .Permalink) "Type" "in" $s.mainSections }}
{{- $featured := default 8 $s.numberOfFeaturedPosts }}
{{- with first $featured (where $posts "Params.featured" true)}}
<h2 class="mt-4">{{ T "featured_posts" }}</h2>
<ul>
{{- range . }}
<li>
<a href="{{ .Permalink }}" class="nav-link" title="{{ .Title }}">{{ .Title }}</a>
</li>
{{- end }}
</ul>
{{- end }}
<h2 class="mt-4">{{ T "recent_posts" }}</h2>
<ul class="flex-column">
{{- $recent := default 8 $s.numberOfRecentPosts }}
{{- range first $recent $posts }}
<li>
<a href="{{ .Permalink }}" class="nav-link" title="{{ .Title }}">{{ .Title }}</a>
</li>
{{- end }}
</ul>
{{- $tagsLimit := (default 100 $s.numberOfTagsShown) }}
{{- range $key, $value := .Site.Taxonomies }}
{{- if gt $value 0 }}
<div>
<h2 class="mt-4 taxonomy" id="{{ $key }}-section">{{ T $key }}</h2>
<nav class="tags_nav">
{{- $onPageTags := $.Page.Params.tags }}
{{- $slicedTags := ($value.ByCount | first $tagsLimit) }}
{{- range $slicedTags }}
<a href='{{ absLangURL $key }}/{{ (replace .Name "#" "%23") | urlize }}/' class="post_tag button button_translucent" title="{{ .Name }}">
{{ upper .Name }}
<span class="button_tally">{{ .Count }}</span>
</a>
{{ end }}
{{ if gt (len $value.ByCount) $tagsLimit }}
<br>
<div class="post_tags_toggle button">{{ T (printf "all_%s" (lower $key)) }}</div>
{{- $tagsInfo := newScratch }}
<div class="post_tags">
<div class="tags_list">
{{- range $value.Alphabetical }}
{{ $tagsInfo.Add "tagsInfo" (slice .Name .Count)}}
<a href='{{ absLangURL $key }}/{{ (replace .Name "#" "%23") | urlize }}/' class=" post_tag button button_translucent" data-position={{ .Count }} title="{{ .Name }}">
{{- upper .Name -}}
<span class="button_tally">{{ .Count }}</span>
</a>
{{ end }}
<div class="tags_sort"><span title="sort alphabetically">[A~Z]</span><span title="sort by count">[0~9]</span>
</div>
<span class="tags_hide"><svg class="icon">
<use xlink:href="#closeme"></use>
</svg></span>
</div>
</div>
{{- end }}
</nav>
</div>
{{- end }}
{{- end }}
</section>
</aside>