diff options
| author | yingyu5658 <i@yingyu5658.me> | 2025-12-13 08:33:08 +0800 |
|---|---|---|
| committer | yingyu5658 <i@yingyu5658.me> | 2025-12-13 08:33:08 +0800 |
| commit | 1e5f8eb33bc41cb59faf059e83701152785cabea (patch) | |
| tree | 45867273ac2178285be840764f7962d2b55556c6 /layouts/archives/single.html | |
| download | blog-1e5f8eb33bc41cb59faf059e83701152785cabea.tar.gz blog-1e5f8eb33bc41cb59faf059e83701152785cabea.zip | |
Initial commit
Diffstat (limited to 'layouts/archives/single.html')
| -rw-r--r-- | layouts/archives/single.html | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/layouts/archives/single.html b/layouts/archives/single.html new file mode 100644 index 0000000..6c43aac --- /dev/null +++ b/layouts/archives/single.html @@ -0,0 +1,94 @@ +{{ define "main" }} +<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 + "往昔") }} {{ $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 }} |
