From 370180de16a8d4fed853787cc2127b3ff9aa1b72 Mon Sep 17 00:00:00 2001 From: geekifan Date: Wed, 23 Apr 2025 09:21:54 +0800 Subject: [PATCH] add tags and categories --- content/archives/index.html | 6 ++ hugo.toml | 22 ++++++ layouts/_default/archives.html | 41 ++++++++++ layouts/_default/single.html | 11 ++- layouts/partials/head.html | 21 ----- layouts/partials/js-selector.html | 4 +- layouts/post/single.html | 10 ++- layouts/taxonomy/category.html | 19 +++++ layouts/taxonomy/category.terms.html | 114 +++++++++++++++++++++++++++ layouts/taxonomy/tag.html | 20 +++++ layouts/taxonomy/tag.terms.html | 28 +++++++ 11 files changed, 270 insertions(+), 26 deletions(-) create mode 100644 content/archives/index.html create mode 100644 layouts/_default/archives.html create mode 100644 layouts/taxonomy/category.html create mode 100644 layouts/taxonomy/category.terms.html create mode 100644 layouts/taxonomy/tag.html create mode 100644 layouts/taxonomy/tag.terms.html diff --git a/content/archives/index.html b/content/archives/index.html new file mode 100644 index 0000000..fae2be8 --- /dev/null +++ b/content/archives/index.html @@ -0,0 +1,6 @@ +--- +title: "Archives" +date: 2023-04-20 +draft: false +layout: archives +--- \ No newline at end of file diff --git a/hugo.toml b/hugo.toml index 38f5cb3..853180f 100644 --- a/hugo.toml +++ b/hugo.toml @@ -2,6 +2,10 @@ baseURL = 'https://example.org/' languageCode = 'en-us' title = 'Chirpy' +[taxonomies] + category = "categories" + tag = "tags" + [module] [[module.imports]] path = "github.com/gohugoio/hugo-mod-bootstrap-scss/v5" @@ -18,6 +22,24 @@ path = "github.com/gohugoio/hugo-mod-bootstrap-scss/v5" weight = 20 pre = "fa-solid fa-house" +[[menu.main]] + name = "Categories" + url = "/categories/" + weight = 30 + pre = "fa-solid fa-house" + +[[menu.main]] + name = "Tags" + url = "/tags/" + weight = 40 + pre = "fa-solid fa-house" + +[[menu.main]] + name = "Archives" + url = "/archives/" + weight = 50 + pre = "fa-solid fa-house" + [params] theme_version = "5.2.0" toc = true diff --git a/layouts/_default/archives.html b/layouts/_default/archives.html new file mode 100644 index 0000000..dc116c7 --- /dev/null +++ b/layouts/_default/archives.html @@ -0,0 +1,41 @@ +{{ define "main" }} + {{ $lang := .Site.Language.Lang }} + {{ $df_strftime_m := i18n "df.archives.strftime" | default "/ %m" }} + {{ $df_dayjs_m := i18n "df.archives.dayjs" | default "/ MM" }} +
+

+ {{ i18n "tabs.archives" }} +

+
+
+ {{ $pages := where .Site.RegularPages "Type" "post" }} + {{ $last_year := "" }} + {{ $pages_count := len $pages }} + + {{ range $index, $page := $pages }} + {{ $cur_year := .Date.Format "2006" }} + + {{ if ne $cur_year $last_year }} + {{ if ne $last_year "" }}{{ end }} + + +
    + + {{ $last_year = $cur_year }} + {{ end }} + +
  • + {{ $ts := .Date.Format "2006-01-02T15:04:05Z07:00" }} + {{ .Date.Format "02" }} + + {{ .Date.Format $df_strftime_m }} + + {{ .Title }} +
  • + + {{ if eq (add $index 1) $pages_count }}
{{ end }} + {{ end }} +
+
+
+{{ end }} \ No newline at end of file diff --git a/layouts/_default/single.html b/layouts/_default/single.html index 0fae42b..d18203f 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -123,8 +123,15 @@
{{ range $i, $category := . }} - {{ $category }} - {{ if not (eq $i (sub (len $) 1)) }},{{ end }} + {{ if eq $i 0 }} + + {{ $category }} + {{ else }} + + {{ $parentCategory := index $.Params.categories 0 }} + {{ $category }} + {{ end }} + {{ if not (eq $i (sub (len .) 1)) }},{{ end }} {{ end }}
{{ end }} diff --git a/layouts/partials/head.html b/layouts/partials/head.html index ca7d04b..73759a4 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -17,28 +17,7 @@ content="width=device-width, user-scalable=no initial-scale=1, shrink-to-fit=no, viewport-fit=cover" > - {{ template "_internal/opengraph.html" }} - {{ template "_internal/twitter_cards.html" }} - {{ template "_internal/schema.html" }} - - {{ if .Params.image }} - {{ $src := .Params.image }} - {{ $imgUrl := "" }} - - {{ if not (findRE "://" $src) }} - {{ $imgUrl = absURL $src }} - {{ $oldUrl := $src | absURL }} - {{ $newUrl := $imgUrl }} - - {{ end }} - {{ else if site.Params.social_preview_image }} - {{ $imgUrl := site.Params.social_preview_image | absURL }} - - - - {{ end }} {{ if ne .Kind "home" }} diff --git a/layouts/partials/js-selector.html b/layouts/partials/js-selector.html index a9e468a..847f1ea 100644 --- a/layouts/partials/js-selector.html +++ b/layouts/partials/js-selector.html @@ -32,7 +32,7 @@ {{ end }} {{ end }} -{{ if or $is_home (eq .Type "post") (eq .Layout "archives") (eq .Layout "category") (eq .Layout "tag") }} +{{ if or $is_home (eq .Type "post") (eq .Layout "archives") (eq .Type "categories") (eq .Type "tags") }} {{ $locale := strings.TrimSuffix (path.Ext .Lang) .Lang }} {{ with $data.dayjs.js.common }} @@ -76,7 +76,7 @@ {{ $js = "post" }} {{ else if eq .Layout "page" }} {{ $js = "page" }} -{{ else if or (eq .Layout "archives") (eq .Layout "category") (eq .Layout "tag") }} +{{ else if or (eq .Layout "archives") (eq .Type "categories") (eq .Type "tags") }} {{ $js = "misc" }} {{ end }} diff --git a/layouts/post/single.html b/layouts/post/single.html index cb59b61..15f2f63 100644 --- a/layouts/post/single.html +++ b/layouts/post/single.html @@ -124,7 +124,15 @@ <i class="far fa-folder-open fa-fw me-1"></i> {{ $len := len . }} {{ range $i, $category := . }} - <a href="{{ "/categories/" | relLangURL }}{{ $category | urlize }}/">{{ $category }}</a>{{ if not (eq $i (sub $len 1)) }},{{ end }} + {{ if eq $i 0 }} + <!-- 主分类 --> + <a href="{{ "/categories/" | relLangURL }}{{ $category | urlize }}/">{{ $category }}</a> + {{ else }} + <!-- 子分类:确保链接包含主分类路径 --> + {{ $parentCategory := index $.Params.categories 0 }} + <a href="{{ "/categories/" | relLangURL }}{{ $parentCategory | urlize }}/{{ $category | urlize }}/">{{ $category }}</a> + {{ end }} + {{ if not (eq $i (sub $len 1)) }},{{ end }} {{ end }} </div> {{ end }} diff --git a/layouts/taxonomy/category.html b/layouts/taxonomy/category.html new file mode 100644 index 0000000..9b356d1 --- /dev/null +++ b/layouts/taxonomy/category.html @@ -0,0 +1,19 @@ +{{ define "main" }} +<div id="page-category"> + <h1 class="ps-lg-2"> + <i class="far fa-folder-open fa-fw text-muted"></i> + {{ .Title }} + <span class="lead text-muted ps-2">{{ len .Pages }}</span> + </h1> + + <ul class="content ps-0"> + {{ range .Pages }} + <li class="d-flex justify-content-between px-md-3"> + <a href="{{ .RelPermalink }}">{{ .Title }}</a> + <span class="dash flex-grow-1"></span> + <span class="text-muted small text-nowrap">{{ .Date | time.Format ":date_medium" }}</span> + </li> + {{ end }} + </ul> +</div> +{{ end }} \ No newline at end of file diff --git a/layouts/taxonomy/category.terms.html b/layouts/taxonomy/category.terms.html new file mode 100644 index 0000000..d5ab62a --- /dev/null +++ b/layouts/taxonomy/category.terms.html @@ -0,0 +1,114 @@ + +{{ define "main" }} +<article class="px-1"> + <h1 class="dynamic-title"> + {{ i18n "tabs.categories" }} + </h1> + <div class="content"> + <div class="container"> + {{ $categories := .Site.Taxonomies.categories }} + + <!-- 首先收集所有唯一的父分类 --> + {{ $parentCategories := slice }} + {{ range $name, $taxonomy := $categories }} + {{ range $taxonomy.Pages }} + {{ if gt (len .Params.categories) 0 }} + {{ $parentCategories = $parentCategories | append (index .Params.categories 0) }} + {{ end }} + {{ end }} + {{ end }} + {{ $parentCategories = $parentCategories | uniq | sort }} + + <!-- 然后为每个父分类创建卡片 --> + {{ range $parentCategories }} + {{ $parentName := . }} + {{ $subcategories := slice }} + {{ $parentPages := slice }} + + <!-- 收集与此父分类相关的所有页面和子分类 --> + {{ range $name, $taxonomy := $categories }} + {{ range $taxonomy.Pages }} + {{ if gt (len .Params.categories) 0 }} + {{ if eq (index .Params.categories 0) $parentName }} + {{ $parentPages = $parentPages | append . }} + {{ if gt (len .Params.categories) 1 }} + {{ $subcategories = $subcategories | append (index .Params.categories 1) }} + {{ end }} + {{ end }} + {{ end }} + {{ end }} + {{ end }} + {{ $subcategories = $subcategories | uniq | sort }} + + <div class="card categories mb-3"> + <div class="card-header d-flex justify-content-between hide-border-bottom"> + <span class="ms-2"> + <i class="far fa-folder{{ if gt (len $subcategories) 0 }}-open{{ end }} fa-fw"></i> + <a href="{{ "/categories/" | relURL }}{{ $parentName | urlize }}" class="mx-2">{{ $parentName }}</a> + <span class="text-muted small font-weight-light"> + {{ if gt (len $subcategories) 0 }} + {{ len $subcategories }} + {{ if gt (len $subcategories) 1 }} + subcategories, + {{ else }} + subcategory, + {{ end }} + {{ end }} + {{ len $parentPages }} + {{ if gt (len $parentPages) 1 }} + posts + {{ else }} + post + {{ end }} + </span> + </span> + + {{ if gt (len $subcategories) 0 }} + <a href="#sub-{{ $parentName | anchorize }}" data-bs-toggle="collapse" aria-expanded="true"> + <i class="fas fa-fw fa-angle-down"></i> + </a> + {{ else }} + <span class="disabled"> + <i class="fas fa-fw fa-angle-right"></i> + </span> + {{ end }} + </div> + + {{ if gt (len $subcategories) 0 }} + <div id="sub-{{ $parentName | anchorize }}" class="collapse show"> + <ul class="list-group list-group-flush"> + {{ range $subcategories }} + {{ $subCategoryName := . }} + + <!-- 查找子分类的文章 --> + {{ $subPages := slice }} + {{ range $parentPages }} + {{ if gt (len .Params.categories) 1 }} + {{ if eq (index .Params.categories 1) $subCategoryName }} + {{ $subPages = $subPages | append . }} + {{ end }} + {{ end }} + {{ end }} + + <li class="list-group-item"> + <i class="far fa-folder fa-fw"></i> + <a href="{{ "/categories/" | relURL }}{{ $subCategoryName | urlize }}" class="mx-2">{{ $subCategoryName }}</a> + <span class="text-muted small font-weight-light"> + {{ len $subPages }} + {{ if gt (len $subPages) 1 }} + posts + {{ else }} + post + {{ end }} + </span> + </li> + {{ end }} + </ul> + </div> + {{ end }} + </div> + {{ end }} + </div> + </div> +</article> +{{ end }} \ No newline at end of file diff --git a/layouts/taxonomy/tag.html b/layouts/taxonomy/tag.html new file mode 100644 index 0000000..3298738 --- /dev/null +++ b/layouts/taxonomy/tag.html @@ -0,0 +1,20 @@ +{{ define "main" }} + {{ $lang := .Site.Language.Lang }} + + <div id="page-tag"> + <h1 class="ps-lg-2"> + <i class="fa fa-tag fa-fw text-muted"></i> + {{ .Data.Term }} + <span class="lead text-muted ps-2">{{ len .Pages }}</span> + </h1> + <ul class="content ps-0"> + {{ range .Pages }} + <li class="d-flex justify-content-between px-md-3"> + <a href="{{ .RelPermalink }}">{{ .Title }}</a> + <span class="dash flex-grow-1"></span> + {{ partial "datetime.html" (dict "date" .Date "lang" $lang "class" "text-muted small text-nowrap") }} + </li> + {{ end }} + </ul> + </div> +{{ end }} \ No newline at end of file diff --git a/layouts/taxonomy/tag.terms.html b/layouts/taxonomy/tag.terms.html new file mode 100644 index 0000000..d3b47a1 --- /dev/null +++ b/layouts/taxonomy/tag.terms.html @@ -0,0 +1,28 @@ +{{ define "main" }} +<article class="px-1"> + <h1 class="dynamic-title"> + {{ i18n "tabs.tags" }} + </h1> + <div class="content"> + <div id="tags" class="d-flex flex-wrap mx-xl-2"> + {{ $tags := slice }} + {{ range $name, $taxonomy := .Site.Taxonomies.tags }} + {{ $tags = $tags | append $name }} + {{ end }} + + {{ $sorted_tags := sort $tags }} + + {{ range $sorted_tags }} + <div> + <a class="tag" href="{{ "/tags/" | relLangURL }}{{ . | urlize }}/"> + {{ . -}} + <span class="text-muted">{{ len (index $.Site.Taxonomies.tags .) }}</span> + </a> + </div> + {{ end }} + </div> + </div> + +</article> + +{{ end }} \ No newline at end of file