From cec8a299a36ea029f8fe2469f68071313ebdde1f Mon Sep 17 00:00:00 2001 From: BigCoke233 Date: Sat, 6 Dec 2025 16:10:43 +0800 Subject: refactor: library section template - use Context to get pages, instead of filter the whole site's pages - use one tempalte `section.html` for both library home and year lists Former-commit-id: e7e01fedce0c4a5b7e6e5eec41ed5e97a1bf1979 --- content/library/2024/_index.md | 1 - content/library/2025/_index.md | 1 - layouts/library/section.html | 45 ++++++++++++++++++++++++++++++------------ layouts/library/yearlist.html | 18 ----------------- 4 files changed, 32 insertions(+), 33 deletions(-) delete mode 100644 layouts/library/yearlist.html diff --git a/content/library/2024/_index.md b/content/library/2024/_index.md index bf9b49a6..5321fd33 100644 --- a/content/library/2024/_index.md +++ b/content/library/2024/_index.md @@ -1,4 +1,3 @@ --- title: 2024 -layout: yearlist --- diff --git a/content/library/2025/_index.md b/content/library/2025/_index.md index ba82b8c7..c141090a 100644 --- a/content/library/2025/_index.md +++ b/content/library/2025/_index.md @@ -1,4 +1,3 @@ --- title: 2025 -layout: yearlist --- diff --git a/layouts/library/section.html b/layouts/library/section.html index a7df419a..cab61e51 100644 --- a/layouts/library/section.html +++ b/layouts/library/section.html @@ -1,17 +1,34 @@ {{ define "main" }} + +{{ $isLibraryHome := eq .RelPermalink "/library/" }} +{{ $books := .RegularPages }} +{{ if $isLibraryHome }} + {{ range .Sections }} + {{ $books = $books | append .RegularPages }} + {{ end }} +{{ end }} + +{{ $years := slice }} +{{ range .Sections }} + {{ $years = $years | append .Title }} +{{ end }}
{{ partial "header.html" . }}
- {{ $booksThisYear := where (where .Site.RegularPages "Section" "library") "Date.Year" now.Year }} + {{ if $isLibraryHome }} {{ partial "components/section-title" (dict - "Left" now.Year - "Right" (printf "共 %d 篇书评" (len $booksThisYear)) + "Left" "最近读的书" + "Right" (printf "共 %d 篇书评" (len $books)) )}} + {{ end }}
- {{ $first18BooksThisYear := first 18 $booksThisYear }} - {{ range $i, $book := $first18BooksThisYear }} + {{ $booksToShow := $books }} + {{ if $isLibraryHome }} + {{ $booksToShow = first 18 $books }} + {{ end }} + {{ range $i, $book := $booksToShow }}
{{ partial "items/book" (dict @@ -21,14 +38,11 @@ {{ end }}
- {{ $years := slice }} - {{ range where .Site.RegularPages "Section" "library" }} - {{ $year := string .Date.Year }} - {{ if not (in $years $year) }} - {{ $years = $years | append $year }} - {{ end }} - {{ end }} + + {{ if $isLibraryHome }} + + - {{ $goodBooks := where (where .Site.RegularPages "Section" "library") ".Params.rating" "==" 5 }} + + {{ $goodBooks := where $books ".Params.rating" "==" 5 }} {{ partial "components/section-title" (dict "Left" "推荐好书" "Right" (printf "共 %d 本" (len $goodBooks)) @@ -57,6 +72,10 @@
{{ end }}
+ + {{ end }} + +
{{ partial "footer.html" . }} diff --git a/layouts/library/yearlist.html b/layouts/library/yearlist.html deleted file mode 100644 index 1405cb27..00000000 --- a/layouts/library/yearlist.html +++ /dev/null @@ -1,18 +0,0 @@ -{{ define "main" }} - {{ partial "header.html" . }} -
-
-

{{ .Title }}

-
- {{ $year := .Title }} - {{ range where .Site.RegularPages "Section" "library" }} - {{ if eq (printf "%d" .Date.Year) $year }} -
- {{ partial "items/book" (dict "ctx" .) }}
- {{ end }} - {{ end }} -
-
-
- {{ partial "footer.html" . }} -{{ end }} -- cgit v1.2.3