summaryrefslogtreecommitdiffstats
path: root/layouts/archives/single.html
blob: 698fc466282582e81f727ccf530c4d5f1639a260 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
{{ define "main" }}
<header>{{- partial "header.html" . -}}</header>
<content>
  {{ if .Site.Params.postSearch }}
  <input
    id="search-input"
    type="text"
    placeholder="搜索文章..."
    style="margin-top: 16px"
  />
  <script>
    document.addEventListener('DOMContentLoaded', function () {
      const searchInput = document.getElementById('search-input');
      const posts = document.querySelectorAll('.blog-posts li');
      const years = document.querySelectorAll('.blog-posts h3');

      function updateSearchResults(searchTerm) {
        let visiblePosts = 0;
        const displayedYears = new Set();

        posts.forEach(function (post) {
          const titleLink = post.querySelector('a');
          const timeElement = post.querySelector('time');

          if (titleLink && timeElement) {
            const title = titleLink.textContent.toLowerCase();
            const year = timeElement.getAttribute('datetime').split('-')[0];

            if (title.includes(searchTerm)) {
              post.style.display = '';
              visiblePosts++;
              displayedYears.add(year);
            } else {
              post.style.display = 'none';
            }
          }
        });

        {{ if .Site.Params.groupByYear }}
        years.forEach(function (y) {
          const year = y.textContent;
          y.style.display = displayedYears.has(year) ? '' : 'none';
        });
        {{ end }}

        {{ if .Site.Params.showPostCount }}
        const countText = `找到 ${visiblePosts} 篇文章`;
        const countElement = document.getElementById('post-count');
        if (countElement) {
          countElement.innerHTML = countText;
        }
        {{ end }}
      }

      if (searchInput) {
        searchInput.addEventListener('input', function () {
          updateSearchResults(this.value.toLowerCase().trim());
        });
      }
    });
  </script>
  {{ end }} {{ $allPosts := where .Site.RegularPages "Type" "eq" "posts" }}
  {{ $excludePosts := where $allPosts "Params.categories" "intersect" (slice "1") }}

  {{ $postPages := $allPosts | complement $excludePosts }} {{ if
  .Site.Params.showPostCount }}
  <p id="post-count">共有 {{ len $postPages }} 篇文章</p>
  {{ end }}

  <ul class="blog-posts">
    {{ if gt (len $postPages) 0 }} {{ $pagesToShow := $postPages.ByDate.Reverse
    }} {{ $currentYear := 0 }} {{ range $pagesToShow }} {{ if .Date }} {{ $year
    := .Date.Year }} {{ if and (.Site.Params.groupByYear) (ne $year
    $currentYear) }}
    <h3>{{ $year }}</h3>
    {{ $currentYear = $year }} {{ end }} {{ end }}

    <li>
      <span
        class="{{ if .Site.Params.groupByYear }}grouped{{else}}ungrouped{{end}}"
      >
        <i>
          <time datetime='{{ .Date.Format "2006-01-02" }}' pubdate>
            {{ .Date.Format (default "2006-01-02" .Site.Params.dateFormat) }}
          </time>
        </i>
      </span>
      <a href="{{ .Permalink }}">{{ .Title }}</a>
    </li>
    {{ end }} {{ else }}
    <li>暂无文章</li>
    {{ end }}
  </ul>
</content>
<hr />
{{ end }}