{{- define "title" }}{{ .Title }} - {{ .Site.Title }}{{ end -}}

{{- define "content" -}}
  {{- $params := .Scratch.Get "params" -}}
  {{- $toc := .Scratch.Get "toc" -}}

  <aside class="toc" id="toc-auto">
    {{- /* Auto TOC */ -}}
    {{- $tocEmpty := eq .TableOfContents `<nav id="TableOfContents"></nav>` -}}
    {{- if (ne $toc.enable false) | and (ne $tocEmpty true) -}}
      <h2 class="toc-title{{ with $params.password }} d-none{{ end }}">
        {{- T "contents" -}}&nbsp;
        {{- dict "Class" "toc-icon fa-solid fa-angle-down fa-fw" | partial "plugin/icon.html" -}}
      </h2>
      <div class="toc-content{{ if eq $toc.auto false }} always-active{{ end }}{{ with $params.password }} d-none{{ end }}" id="toc-content-auto"></div>
    {{- end -}}
  </aside>

  <aside class="aside-custom">
    <!-- put aside for later use -->
    {{- partial (.Scratch.Get "customFilePath").aside . -}}
  </aside>

  <article class="page single">
    <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 fa-fw" -}}
            {{- $title := cond (hasPrefix .Url "http") (printf "%v -> %v" (T "repost") .Url ) (T "repost") -}}
            {{- if hasPrefix .Url "http" -}}
              {{ dict "Destination" .Url "Icon" $icon "Class" "icon-repost" "Title" $title | partial "plugin/link.html" -}}
            {{- else -}}
              <span title="{{ $title }}" 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">{{ . }}</p>{{- end -}}
    </div>

    {{- /* Meta */ -}}
    <div class="post-meta">
      <div class="post-meta-line">
        {{- partial "single/post-author.html" . -}}

        {{- $categories := slice -}}
        {{- range .Params.categories -}}
          {{- $category := partialCached "function/path.html" . . | printf "/categories/%v" | $.Site.GetPage -}}
          {{- $categories = $categories | append (printf `<a href="%v">%v %v</a>` (replace $category.RelPermalink "#" "%23") (dict "Class" "fa-regular fa-folder fa-fw" | partial "plugin/icon.html") $category.Title) -}}
        {{- end -}}
        {{- with delimit $categories "&ensp;" }}
          <span class="post-category">
            {{- dict "Categories" . | T "includedInCategories" | safeHTML -}}
          </span>
        {{- end -}}
      </div>
      <div class="post-meta-line">
        {{- with .Site.Params.dateformat | default "2006-01-02" | .PublishDate.Format -}}
          <span title={{ "2006-01-02 15:04:05" | $.PublishDate.Format }}>
            {{- dict "Class" "fa-regular fa-calendar-alt fa-fw" | partial "plugin/icon.html" -}}&nbsp;
            {{- printf `<time datetime="%v">%v</time>` . . | safeHTML -}}
          </span>&nbsp;
        {{- end -}}
        {{- if $params.wordCount -}}
          <span>
            {{- dict "Class" "fa-solid fa-pencil-alt fa-fw" | partial "plugin/icon.html" }} {{ T "wordCount" .WordCount -}}
          </span>&nbsp;
        {{- end -}}
        {{- if $params.readingTime -}}
          <span>
            {{- dict "Class" "fa-regular fa-clock fa-fw" | partial "plugin/icon.html" }} {{ T "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 -}}
          {{- .Scratch.Set "commentExpired" true -}}
        {{- end -}}
        {{- $comment := .Scratch.Get "comment" | default dict -}}
        {{- $visitorIcon := dict "Class" "fa-regular fa-eye fa-fw" | partial "plugin/icon.html" -}}
        {{- $commentIcon := dict "Class" "fa-regular fa-comments fa-fw" | partial "plugin/icon.html" -}}
        {{- /* Visitor Count */ -}}
        {{- if $comment.enable -}}
          {{- if $comment.artalk.enable -}}
            <span class="comment-visitors" data-flag-title="{{ .Title }}">
              {{ $visitorIcon }}&nbsp;<span id="{{ .RelPermalink }}" class="artalk-visitor-count">-</span>&nbsp;{{ T "views" }}
            </span>&nbsp;
          {{- else if ($comment.valine.enable | and $comment.valine.visitor) -}}
            <span id="{{ .RelPermalink }}" class="leancloud_visitors comment-visitors" data-flag-title="{{ .Title }}">
              {{ $visitorIcon }}&nbsp;<span class="leancloud-visitors-count">-</span>&nbsp;{{ T "views" }}
            </span>&nbsp;
          {{- else if $comment.waline.enable | and $comment.waline.pageview -}}
            <span class="comment-visitors" data-flag-title="{{ .Title }}">
              {{ $visitorIcon }}&nbsp;<span data-path="{{ .RelPermalink }}" class="waline-pageview-count">-</span>&nbsp;{{ T "views" }}
            </span>&nbsp;
          {{- else if $comment.twikoo.enable | and $comment.twikoo.visitor -}}
            <span id="{{ .RelPermalink }}" class="comment-visitors" data-flag-title="{{ .Title }}">
              {{ $visitorIcon }}&nbsp;<span id="twikoo_visitors">-</span>&nbsp;{{ T "views" }}
            </span>&nbsp;
          {{- else if .Site.Params.ibruce.enable | and .Site.Params.ibruce.enablePost -}}
            <span id="busuanzi_container_page_pv" class="busuanzi_visitors comment-visitors" data-flag-title="{{ .Title }}">
              {{ $visitorIcon }}&nbsp;<span id="busuanzi_value_page_pv">-</span>&nbsp;{{ T "views" }}
            </span>&nbsp;
          {{- end -}}
        {{- else if .Site.Params.ibruce.enable | and .Site.Params.ibruce.enablePost -}}
          <span id="busuanzi_container_page_pv" class="busuanzi_visitors comment-visitors" data-flag-title="{{ .Title }}">
            {{ $visitorIcon }}&nbsp;<span id="busuanzi_value_page_pv">-</span>&nbsp;{{ T "views" }}
          </span>&nbsp;
        {{- end -}}
        {{- /* Comment Count */ -}}
        {{- if $comment.enable -}}
          {{- if $comment.artalk.enable -}}
            <span class="comment-count" data-flag-title="{{ .Title }}">
              {{ $commentIcon }}&nbsp;<span data-xid="{{ .RelPermalink }}" class="artalk-comment-count">-</span>&nbsp;{{ T "comments" }}
            </span>&nbsp;
          {{- else if $comment.valine.enable | and $comment.valine.commentCount -}}
            <span class="comment-count" data-flag-title="{{ .Title }}">
              {{ $commentIcon }}&nbsp;<span data-xid="{{ .RelPermalink }}" class="valine-comment-count">-</span>&nbsp;{{ T "comments" }}
            </span>&nbsp;
          {{- else if $comment.waline.enable | and $comment.waline.comment -}}
            <span class="comment-count" data-flag-title="{{ .Title }}">
              {{ $commentIcon }}&nbsp;<span data-path="{{ .RelPermalink }}" class="waline-comment-count">-</span>&nbsp;{{ T "comments" }}
            </span>&nbsp;
          {{- else if $comment.twikoo.enable | and $comment.twikoo.commentCount -}}
            <span id="{{ .RelPermalink }}" class="comment-count" data-flag-title="{{ .Title }}">
              {{ $commentIcon }}&nbsp;<span id="twikoo-comment-count">-</span>&nbsp;{{ T "comments" }}
            </span>&nbsp;
          {{- end -}}
        {{- end -}}
      </div>
    </div>

    {{- /* Featured image */ -}}
    {{- $image := $params.featuredimage -}}
    {{- with .Resources.GetMatch "featured-image" -}}
      {{- $image = .RelPermalink -}}
    {{- end -}}
    {{- with $image -}}
      <div class="featured-image">
        {{- dict "Src" . "Title" $.Description "Resources" $.Resources | partial "plugin/image.html" -}}
      </div>
    {{- end -}}

    {{- /* Static TOC */ -}}
    {{- if (ne $toc.enable false) | and (ne $tocEmpty true) -}}
      <div class="details toc{{ with $params.password }} d-none{{ end }}" id="toc-static" data-kept="{{ if $toc.keepStatic }}true{{ else }}false{{ end }}">
        <div class="details-summary toc-title">
          <span>{{ T "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">
          {{- dict "Content" .TableOfContents "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
        </div>
      </div>
    {{- end -}}

    {{- /* Content */ -}}
    {{- $content := dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
    {{- if $params.password -}}
      {{- $saltLen := strings.RuneCount (trim $params.password "") -}}
      {{- $saltLen = cond (eq (mod $saltLen 2) 0) (add $saltLen 1) $saltLen -}}
      {{- $base64EncodeContent := $content | base64Encode -}}
      {{- $content = printf "%v%v%v"
        (substr $base64EncodeContent 0 $saltLen)
        (substr (sha256 $params.password) $saltLen)
        (substr $base64EncodeContent $saltLen)
      -}}
    {{- end -}}
    <div class="content" id="content"
      {{- with $params.endFlag }} data-end-flag="{{ . }}"{{- end -}}
      {{- with $params.password }} data-password="{{ md5 . }}"{{- end -}}
      {{- with $params.password }} data-content="{{ $content }}"{{- end -}}
    >
      {{- if not $params.password -}}
        {{- /* Expiration Reminder */ -}}
        {{- partial "single/expiration-reminder.html" . -}}
        {{- $content -}}
      {{- else -}}
        {{- partial "single/fixit-decryptor.html" . -}}
      {{- end -}}
    </div>

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

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