aboutsummaryrefslogtreecommitdiffstats
path: root/layouts/_default/list.html
blob: 8c1db86c790aec1c2a8b5c62cf690649f40bcebc (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
{{ define "main" }}
<content>
  {{ if .Site.Params.postSearch }}
  <input
    id="search-input"
    type="text"
    placeholder="Search..."
    style="margin-top: 16px"
  />
  <script>
    // 等待 DOM 完全加载后执行
    document.addEventListener('DOMContentLoaded', function () {
      // 缓存 DOM 元素
      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 title = post.querySelector('a').textContent.toLowerCase();
          const year = post.querySelector('time').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 }}
      }

      searchInput.addEventListener('input', function () {
        updateSearchResults(this.value.toLowerCase().trim());
      });
    });
  </script>
  {{ end }}
  <ul class="blog-posts">
    {{ $currentYear := 0 }} {{ range .Pages }} {{ if and (not .Params.hidden)
    (not (in .Params.categories "往昔")) }}

    <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>No posts yet</li>
    {{ end }}
  </ul>
</content>
{{ end }}