{{- define "title" -}}
  {{- cond (.Param "capitalizeTitles") (title .Title) .Title -}}
  {{- if .Site.Params.withSiteTitle }} {{ .Site.Params.titleDelimiter }} {{ .Site.Title }}{{- end -}}
{{- end -}}

{{- define "content" -}}
  {{- $title := cond (.Param "capitalizeTitles") (title .Title) .Title -}}
  {{- $params := partial "function/params.html" -}}
  {{- $toc := .Store.Get "toc" -}}
  {{- $tableOfContents := .Fragments.ToHTML ($toc.startlevel | int) ($toc.endlevel | int) ($toc.ordered | default false) -}}
  {{- $showToc := $toc.enable | and (ne $tableOfContents `<nav id="TableOfContents"></nav>`) -}}
  {{- .Store.Set "showToc" $showToc -}}
  {{- $tableOfContents = dict "Content" $tableOfContents "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}

  <aside class="aside-collection animate__animated animate__fadeIn animate__faster" aria-label="{{ T "collections" }}">
    {{- /* Collection List */ -}}
    {{- partial "single/collection-list.html" . -}}

    {{- /* Related Content */ -}}
    {{- partial "single/related-aside.html" . -}}

    {{- /* Custom Aside */ -}}
    {{- block "custom-aside" . }}{{ end -}}
  </aside>

  <article class="page single"{{ with .Params.fromAdapters }} data-adapters="{{ . }}"{{ end }}>
    <div class="header">
      {{- /* Title */ -}}
      <h1 class="single-title animate__animated animate__flipInX">
        {{- $repost := $params.repost | default dict -}}
        {{- with $repost -}}
          {{- if eq .Enable true -}}
            {{- $icon := dict "Class" "fa-solid fa-share" -}}
            {{- $iconTitle := cond (hasPrefix .Url "http") (printf "%v -> %v" (T "single.repost") .Url ) (T "single.repost") -}}
            {{- if hasPrefix .Url "http" -}}
              {{ dict "Destination" .Url "Icon" $icon "Class" "icon-repost" "Title" $iconTitle | partial "plugin/link.html" -}}
            {{- else -}}
              <span title="{{ $iconTitle }}" class="icon-repost">{{- $icon | partial "plugin/icon.html" -}}</span>
            {{- end -}}
          {{- end -}}
        {{- end -}}
        <span>{{ $title }}</span>
      </h1>

      {{- /* Subtitle */ -}}
      {{- with $params.subtitle -}}<p class="single-subtitle animate__animated animate__fadeIn">{{ . | $.RenderString }}</p>{{- end -}}
    </div>

    {{- /* Meta */ -}}
    <div class="post-meta">
      <div class="post-meta-line">
        {{- partial "single/post-author.html" . -}}
        {{- partial "single/post-included-in.html" . -}}
      </div>
      {{- /* TODO add disclaimer or space between meta and content */ -}}
      <div class="post-meta-line">
        {{- with .PublishDate | dateFormat (.Site.Params.dateformat | default "2006-01-02") -}}
          <span title="{{ dict "Date" ("2006-01-02 15:04:05" | $.PublishDate.Format) | T "single.publishedOnDate" }}">
            {{- dict "Class" "fa-solid fa-calendar-days me-1" | partial "plugin/icon.html" -}}
            {{- printf `<time datetime="%v">%v</time>` . . | safeHTML -}}
          </span>&nbsp;
        {{- end -}}
        {{- if (ne .Lastmod.Unix .PublishDate.Unix) | and $params.showLastmod -}}
          {{- with .Lastmod | dateFormat (.Site.Params.dateformat | default "2006-01-02") -}}
            <span title="{{ dict "Date" ("2006-01-02 15:04:05" | $.Lastmod.Format) | T "single.updatedOnDate" }}">
              {{- dict "Class" "fa-regular fa-calendar-check me-1" | partial "plugin/icon.html" -}}
              {{- printf `<time datetime="%v">%v</time>` . . | safeHTML -}}
            </span>&nbsp;
          {{- end -}}
        {{- end -}}
        {{- if $params.wordCount -}}
          <span title="{{ T "single.wordCount" .WordCount }}">
            {{- dict "Class" "fa-solid fa-pencil-alt me-1" | partial "plugin/icon.html" -}}
            {{- T "single.fuzzyWordCount" .FuzzyWordCount -}}
          </span>&nbsp;
        {{- end -}}
        {{- if $params.readingTime -}}
          <span>
            {{- dict "Class" "fa-regular fa-clock me-1" | partial "plugin/icon.html" -}}
            {{- T "single.readingTime" .ReadingTime -}}
          </span>&nbsp;
        {{- end -}}

        {{- /* If the article expires, close the comment or not */ -}}
        {{- $expirationReminder := $params.expirationReminder | default dict -}}
        {{- if
          $expirationReminder.enable
          | and (gt (div (sub now.Unix .Lastmod.Unix) 86400) ($expirationReminder.warning | default 180))
          | and $expirationReminder.closeComment -}}
          {{- .Store.Set "commentExpired" true -}}
        {{- end -}}
        {{- $comment := .Store.Get "comment" | default dict -}}
        {{- $visitorIcon := dict "Class" "fa-regular fa-eye me-1" | partial "plugin/icon.html" -}}
        {{- $commentIcon := dict "Class" "fa-regular fa-comments me-1" | partial "plugin/icon.html" -}}
        {{- /* Visitor Count */ -}}
        {{- if $comment.enable | and $comment.artalk.enable -}}
          <span class="comment-visitors" data-flag-title="{{ $title }}">
            {{- $visitorIcon }}<span class="artalk-visitor-count" data-page-key="{{ .RelPermalink }}">-</span>&nbsp;{{ T "single.views" }}
          </span>&nbsp;
        {{- else if $comment.enable | and $comment.valine.enable | and $comment.valine.visitor -}}
          <span id="{{ .RelPermalink }}" class="leancloud_visitors comment-visitors" data-flag-title="{{ $title }}">
            {{- $visitorIcon }}<span class="leancloud-visitors-count">-</span>&nbsp;{{ T "single.views" }}
          </span>&nbsp;
        {{- else if $comment.enable | and $comment.waline.enable | and $comment.waline.pageview -}}
          <span class="comment-visitors" data-flag-title="{{ $title }}">
            {{- $visitorIcon }}<span data-path="{{ .RelPermalink }}" class="waline-pageview-count">-</span>&nbsp;{{ T "single.views" }}
          </span>&nbsp;
        {{- else if $comment.enable | and $comment.twikoo.enable | and $comment.twikoo.visitor -}}
          <span id="{{ .RelPermalink }}" class="comment-visitors" data-flag-title="{{ $title }}">
            {{- $visitorIcon }}<span id="twikoo_visitors">-</span>&nbsp;{{ T "single.views" }}
          </span>&nbsp;
        {{- else if .Site.Params.busuanzi.enable | and .Site.Params.busuanzi.pageViews | and hugo.IsProduction -}}
          <span id="busuanzi_container_page_pv" class="busuanzi_visitors comment-visitors" data-flag-title="{{ $title }}">
            {{- $visitorIcon }}<span id="busuanzi_value_page_pv">-</span>&nbsp;{{ T "single.views" }}
          </span>&nbsp;
        {{- end -}}
        {{- /* Comment Count */ -}}
        {{- if $comment.enable -}}
          {{- if $comment.artalk.enable -}}
            <span class="comment-count" data-flag-title="{{ $title }}">
              {{ $commentIcon }}<span class="artalk-comment-count" data-page-key="{{ .RelPermalink }}">-</span>&nbsp;{{ T "single.comments" }}
            </span>&nbsp;
          {{- else if $comment.valine.enable | and $comment.valine.commentCount -}}
            <span class="comment-count" data-flag-title="{{ $title }}">
              {{ $commentIcon }}<span data-xid="{{ .RelPermalink }}" class="valine-comment-count">-</span>&nbsp;{{ T "single.comments" }}
            </span>&nbsp;
          {{- else if $comment.waline.enable | and $comment.waline.comment -}}
            <span class="comment-count" data-flag-title="{{ $title }}">
              {{ $commentIcon }}<span data-path="{{ .RelPermalink }}" class="waline-comment-count">-</span>&nbsp;{{ T "single.comments" }}
            </span>&nbsp;
          {{- else if $comment.twikoo.enable | and $comment.twikoo.commentCount -}}
            <span id="{{ .RelPermalink }}" class="comment-count" data-flag-title="{{ $title }}">
              {{ $commentIcon }}<span id="twikoo-comment-count">-</span>&nbsp;{{ T "single.comments" }}
            </span>&nbsp;
          {{- end -}}
        {{- end -}}
      </div>
    </div>

    {{- /* Featured image */ -}}
    {{- $image := $params.featuredimage -}}
    {{- $matches := slice -}}
    {{- if .Resources.GetMatch "featured-image" -}}
      {{- $matches = $matches | append "featured-image" -}}
    {{- end -}}
    {{- if $image | or (gt (len $matches) 0) -}}
      <div class="featured-image">
        {{- $optim := slice 
          (dict "Process" "resize 800x webp" "descriptor" "800w")
          (dict "Process" "resize 1200x webp" "descriptor" "1200w")
          (dict "Process" "resize 1600x webp" "descriptor" "1600w") 
        -}}
        {{- dict "Src" $image "Title" $.Description "Resources" .Resources "Matches" $matches "Loading" "eager" "Sizes" "(max-width: 680px) 100vw, (max-width: 960px) 80vw, (max-width: 1440px) 56vw, 800px" "OptimConfig" $optim | partial "plugin/image.html" -}}
      </div>
    {{- end -}}

    {{- /* Static TOC */ -}}
    {{- if $showToc -}}
      <div class="details toc{{ with $params.password }} encrypted-hidden{{ end }}" id="toc-static" data-kept="{{ if $toc.keepstatic }}true{{ else }}false{{ end }}">
        <div class="details-summary toc-title">
          <span>{{ T "single.contents" }}</span>
          <span>{{ dict "Class" "details-icon fa-solid fa-angle-right" | partial "plugin/icon.html" }}</span>
        </div>
        <div class="details-content toc-content" id="toc-content-static">
          {{- $tableOfContents -}}
        </div>
      </div>
    {{- end -}}

    {{- /* AI Summary */ -}}
    {{- $summaryConfig := (.Params | merge .Site.Params).postSummary -}}
    {{- if $summaryConfig.enable -}}
      <div class="ai-summary"></div>
    {{- end -}}

    {{- /* Custom block before post content */ -}}
    {{- block "custom-post__content:before" . }}{{ end -}}

    {{- /* Expiration Reminder */ -}}
    {{- partial "single/expiration-reminder.html" . -}}

    {{- /* Content */ -}}
    {{- $content := dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
    <div class="content" id="content"{{ with $params.endFlag }} data-end-flag="{{ . }}"{{ end }}>
      {{- if not $params.password -}}
        {{- $content -}}
      {{- end -}}
    </div>

    {{- /* Custom block after post content */ -}}
    {{- block "custom-post__content:after" . }}{{ end -}}

    {{- /* Related Content */ -}}
    {{- partial "single/related.html" . -}}

    {{- /* Reward before Footer */ -}}
    {{- $reward := .Store.Get "reward" -}}
    {{- if eq $reward.position "before" -}}
      {{- partial "single/reward.html" . -}}
    {{- end -}}

    {{- /* Content Encryption */ -}}
    {{- dict "Content" $content "Password" $params.password "Message" $params.message "Page" . | partial "plugin/fixit-encryptor.html" -}}

    {{- /* Collection Navigation */ -}}
    {{- partial "single/collection-nav.html" . -}}

    {{- /* Custom block before post footer */ -}}
    {{- block "custom-post__footer:before" . }}{{ end -}}

    {{- /* Footer */ -}}
    {{- partial "single/footer.html" . -}}

    {{- /* Reward after post footer */ -}}
    {{- if eq $reward.position "after" -}}
      {{- partial "single/reward.html" . -}}
    {{- end -}}

    {{- /* Custom block after post footer */ -}}
    {{- block "custom-post__footer:after" . }}{{ end -}}

    {{- /* Comment */ -}}
    {{- partial "single/comment.html" . -}}
  </article>

  <aside class="toc" id="toc-auto" aria-label="{{ T "single.contents" }}">
    {{- /* Custom block before post toc */ -}}
    {{- block "custom-post__toc:before" . }}{{ end -}}

    {{- /* Auto TOC */ -}}
    {{- if $showToc -}}
      <h2 class="toc-title{{ with $params.password }} encrypted-hidden{{ end }}">
        {{- T "single.contents" -}}&nbsp;
        {{- dict "Class" "toc-icon fa-solid fa-angle-down" | partial "plugin/icon.html" -}}
      </h2>
      <div class="toc-content{{ if eq $toc.auto false }} always-active{{ end }}{{ with $params.password }} encrypted-hidden{{ end }}" id="toc-content-auto"></div>
    {{- end -}}

    {{- /* Custom block after post toc */ -}}
    {{- block "custom-post__toc:after" . }}{{ end -}}
  </aside>

  {{- /* TOC Dialog */ -}}
  {{- if $showToc -}}
    <dialog id="toc-dialog" aria-labelledby="toc-dialog-title" role="dialog">
      <div class="toc">
        <h2 class="toc-title">{{ T "single.contents" }}</h2>
        <div class="toc-content" id="toc-content-drawer">
          {{- replace $tableOfContents `id="TableOfContents"` "" | safeHTML -}}
        </div>
      </div>
    </dialog>
  {{- end -}}
{{- end -}}
