Skip to content

Instantly share code, notes, and snippets.

@andrewconnell
Created May 1, 2020 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewconnell/defb9c74bd1c24e77cbdb071942cdb31 to your computer and use it in GitHub Desktop.
Save andrewconnell/defb9c74bd1c24e77cbdb071942cdb31 to your computer and use it in GitHub Desktop.
{{ $s := newScratch }}
{{/* ARCHIVE */}}
{{ $s.Set "archive" slice }}
{{ $s.Set "years" slice }}
{{ range sort (index .Site.Taxonomies "blog/archive") "Page.Title" "desc" }}
{{ $splitted_date := split .Page.Title "/" }}
{{ $year := index $splitted_date 0 }}
{{ $month := index $splitted_date 1 }}
{{ if not (in ($s.Get "years") $year) }}
{{ $s.Add "years" $year }}
{{ $s.Set $year slice }}
{{ end }}
{{ $s.Add $year (dict "month" $month "count" .Count) }}
{{ end }}
{{/* IF POST PAGE|TAXONOMY, GET CURRENT YEAR */}}
{{ $s.Set "current_page_year" 1 }}
{{ if eq .Page.Kind "page" }}
{{/* IF IT IS A CONTENT PAGE, GET IT'S ARCHIVE YEAR INDICATOR */}}
{{ $s.Set "current_page_year" (index (split ($.Page.Param "blog/archive") "/") 0) }}
{{ else if eq .Page.Kind "taxonomy" }}
{{/* IF IT IS A TAXONOMY ROLLUP PAGE FOR PAGES, GET IT'S YEAR */}}
{{ if eq .Page.Data.Singular "blog/archive" }}
{{ $s.Set "current_page_year" (index (split .Page.Data.Term "/") 0)}}
{{ end }}
{{ end }}
<div class="widget">
<h1 class="mb-0">BLOG POST ARCHIVES</h1>
<div class="accordion" id="accordion">
{{ $s.Set "is_blog_archive_open" false }}
{{ range $index, $year := $s.Get "years" }}
{{ if eq ($s.Get "current_page_year") $year }}
{{ $s.Set "is_blog_archive_open" true }}
{{ else if and (eq ($s.Get "current_page_year") 1) (eq $index 0) }}
{{ $s.Set "is_blog_archive_open" true }}
{{ else }}
{{ $s.Set "is_blog_archive_open" false }}
{{ end }}
{{ $isOpen := $s.Get "is_blog_archive_open" }}
<div>
<div class="header" id="heading-{{.}}">
<button class="btn btn-link {{if not $isOpen}}collapsed{{end}}" type="button" data-toggle="collapse" data-target="#collapse-{{$year}}" aria-expanded="{{ $isOpen }}" aria-controls="collapse-{{$year}}">
{{ $year }}
</button>
</div>
<div class="body {{if not $isOpen}}collapse{{else}}show{{end}}" id="collapse-{{$year}}" aria-labelledby="heading-{{$year}}" data-parent="#accordion">
<ul class="archive-month-list">
{{ range sort ($s.Get $year) "month" "desc" }}
{{ $month := cond (lt .month 10) (print 0 .month) .month }}
<li><a href="/blog/archive/{{$year}}/{{.month}}">{{ dateFormat "January" (print "2019-" $month "-01") }}</a></li>
{{ end }}
</ul>
</div>
</div>
{{end}}
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment