{{ define "main" }}

    {{ $oldYear := "" }}

    {{ $sortedPages := sort .Data.Pages "Params.release" "desc" }}
    {{ $paginator := .Paginate $sortedPages }}

    {{- /* $sortedPages := .Data.Pages.ByParam "release" */ -}}
    {{- /* $paginator := .Paginate $sortedPages */ -}}

    {{- /* $customPages := slice */ -}}
    {{- /* range .Data.Pages */ -}}
        {{- /* $orderDate := .Params.release | default .Date */ -}}
        {{- /* print $orderDate */ -}}
        {{- /* $customPages = $customPages | append (dict "Page" . "orderDate" $orderDate) */ -}}
    {{- /* end */ -}}
    {{- /* $sortedPages := sort $customPages "Params.orderDate" "desc" */ -}}
    {{- /* $paginator := .Paginate $sortedPages */ -}}

    <main class="posts">
        <h1>{{ .Title }}</h1>
        {{ if .Params.subtitle }}
        <strong>{{ .Params.subtitle | markdownify }}</strong>
        {{ end }}

        {{ if .Content }}
            <div class="content">{{ .Content }}</div>
        {{ end }}

        {{ range $paginator.Pages }}

            {{/* Show the Release attribute if later than Date. */}}

            {{ $date := .Date }}
            {{ if and (.Params.release) (ge (time.AsTime .Params.release) (time.AsTime .Date)) }}
                {{ $date = .Params.release }}
            {{ end }}
            {{ $date = time.AsTime $date }}

            {{/* Custom year grouping in order to keep paginator order. */}}

            {{ $year := $date.Year }}
            {{ if not (eq $oldYear $year) }}
                {{ if not (eq $oldYear "") }}
            </div>
                {{ end }}
            <div class="posts-group">
                <div class="post-year">{{ time.Format "2006" $date }}</div>
            {{ end }}
            {{ $oldYear = $year }}

                <ul class="posts-list">

                    {{/* If marked Hidden or Release date is in the future, skip. */}}

                    {{ if .Params.hidden }}
                        {{ continue }}
                    {{ end }}

                    {{ if and (.Params.release) (ge (time.AsTime .Params.release) (time now)) }}
                        {{ continue }}
                    {{ end }}

                    <li class="post-item">
                        <a href="{{.Permalink}}" class="post-item-inner">
                            <span class="post-title">
                                {{ if .Params.draft }}
                                    {{ print "(DRAFT) "}}
                                {{ end }}

                                {{ if .Params.pinned }}
                                    <img style="display: inline-block; vertical-align: middle; margin-right: 4px;"
                                        src="/feathericons/16px-a9a9b3/paperclip.svg" alt="Pinned:"
                                    >
                                {{ end }}

                                {{ if .Params.video }}
                                    Video:
                                {{ else if .Params.audio }}
                                    Audio:
                                {{ else if .Params.tags }}
                                    {{ $prefix := (index .Params.tags 0 | title) }}
                                    {{ if eq $prefix "Ebook" }}
                                        eBook:
                                    {{ else }}
                                        {{ $prefix }}:
                                    {{ end }}
                                {{ end }}

                                {{ .Title }}{{ if and (.Params.subtitle) (and (.Params.author) (.Params.date)) }}
                                    {{ "--" | markdownify }} {{ .Params.subtitle}}
                                {{ end }}

                                {{ if .Params.feathericon }}
                                    <img style="display: inline-block; vertical-align: middle; margin-right: 4px;"
                                        src="/feathericons/16px-a9a9b3/{{ print .Params.feathericon }}.svg" alt=""
                                    >
                                {{ else if .Params.video }}
                                    <img style="display: inline-block; vertical-align: middle; margin-right: 4px;"
                                        src="/feathericons/16px-a9a9b3/film.svg" alt="(VIDEO)"
                                    >
                                {{ else if .Params.audio }}
                                    <img style="display: inline-block; vertical-align: middle; margin-right: 4px;"
                                        src="/feathericons/16px-a9a9b3/music.svg" alt="(AUDIO)"
                                    >
                                {{ end }}
                            </span>
                            <span class="post-day">
                                {{/*
                                {{ if .Site.Params.dateformShort }}
                                    {{ time.Format .Site.Params.dateformShort $date }}
                                {{ else }}
                                    {{ time.Format "Jan 2" $date }}
                                {{ end }}
                                */}}

                                {{ if .Site.Params.dateformShort }}
                                    {{ time.Format .Site.Params.dateformShort .Date }}
                                {{ else }}
                                    {{ time.Format "Jan 2" .Date }}
                                {{ end }}

                                {{ if .Params.Release }}
                                    for
                                    {{ if .Site.Params.dateformShort }}
                                         {{ time.Format .Site.Params.dateformShort .Params.release }}
                                    {{ else }}
                                        {{ time.Format "Jan 2" .Params.release }}
                                    {{ end }}
                                {{ end }}
                            </span>
                        </a>
                    </li>
                {{ end }}
            </ul>
        </div>
    {{ partial "pagination-list.html" . }}
    </main>
{{ end }}
