Recently found this article
and was able to generate a much better archives page. Content is grouped by year instead of a flat list.
Could be easily extented to Year-Month based categorization- but this works.
Here:
$.Scratch acts as temp storage for specific page. Hugo Link
$.Scratch.Set "count" a temp per page variable to neglect iteration thru entire range
<divclass="row"><divclass="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1"><divid="blog-archives"class="category"><!-- https://www.thedroneely.com/posts/generating-archive-pages-with-hugo/ --><!-- we are interested in posts, and set initialize count to 1 upfront
else the latest year will not be printed
--> {{ $type := "post" }}
{{ $.Scratch.Set "count" 1 }}
<!-- for each post that we have, see if we have them in current year,
then only print year tag, else skip it.
results in descending list by year
--> {{ range (.Site.RegularPages.GroupByDate "2006") }}
<!--
This generates year link/tag only if there are some posts
above range takes care of it `gt .Key 1`
--> {{ if and (gt .Key 1) (gt (where .Pages "Type" $type) 0) }}
<!--
I will try to find if there is a better of breaking loop early
rather than unncessarily iterating over entire range
--> {{ range (where .Pages "Type" $type) }}
{{ if (eq ($.Scratch.Get "count") 1) }}
{{ $.Scratch.Set "count" 0 }}
<h1>{{ .Date.Format "2006" }}</h1> {{ end }}
{{ end }}
{{ end }}
<!-- reset for next iteration/year --> {{ $.Scratch.Set "count" 1 }}
<!-- now for all the pages/posts in specific year --> {{ range (where .Pages "Type" $type) }}
{{ if (ne .Params.hidden true) }}
<h4><ahref="{{ .RelPermalink }}"><span>{{ .Date.Format "02 Jan" }}</span> — {{ .Title }}
</a></h4> {{ end }}
{{ end }}
{{ end }}
</div></div></div>