diff options
| author | Eltrac <hi@guhub.cn> | 2025-07-14 20:45:00 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-07-14 20:45:00 +0800 |
| commit | 5a048dc954bdc9641b1dca9305cd26ed2b25c480 (patch) | |
| tree | d3057a1d5876f8daf37b590a0c9b9694e05c5bb5 /layouts/_partials | |
| parent | 9a261d9b5fecd4eea019ec63e300c25fdee1812e (diff) | |
refactor: combine categories and tags together and simplify templating
Former-commit-id: 5f3933751ede3845ebc3ff319986d25515501522
Diffstat (limited to 'layouts/_partials')
| -rw-r--r-- | layouts/_partials/components/postnav.html | 7 | ||||
| -rw-r--r-- | layouts/_partials/taxonomy/all-categories.html | 17 | ||||
| -rw-r--r-- | layouts/_partials/taxonomy/all-tags.html | 74 |
3 files changed, 94 insertions, 4 deletions
diff --git a/layouts/_partials/components/postnav.html b/layouts/_partials/components/postnav.html index 0174060c..55dd8a51 100644 --- a/layouts/_partials/components/postnav.html +++ b/layouts/_partials/components/postnav.html @@ -7,12 +7,11 @@ <li class="{{ if eq "/featured/" $.Page.RelPermalink }}active{{ end }}"> <a data-umami-event="🧠Click postnav link" href="{{ .Site.LanguagePrefix }}/featured">{{ T "postNav.featured" }}</a> </li> - <li class="{{ if eq "/categories/" $.Page.RelPermalink }}active{{ end }}"> + <li class="{{ if or + (strings.HasPrefix $.Page.RelPermalink "/categories/") + (strings.HasPrefix $.Page.RelPermalink "/tags/") }}active{{ end }}"> <a data-umami-event="🧠Click postnav link" href="{{ .Site.LanguagePrefix }}/categories">{{ T "postNav.categories" }}</a> </li> - <li class="{{ if eq "/tags/" $.Page.RelPermalink }}active{{ end }}"> - <a data-umami-event="🧠Click postnav link" href="{{ .Site.LanguagePrefix }}/tags">{{ T "postNav.tags" }}</a> - </li> {{ end }} <li id="search-instruction"> <span>{{ T "searchInstruction" }}</span> diff --git a/layouts/_partials/taxonomy/all-categories.html b/layouts/_partials/taxonomy/all-categories.html new file mode 100644 index 00000000..a5ceef8e --- /dev/null +++ b/layouts/_partials/taxonomy/all-categories.html @@ -0,0 +1,17 @@ +<!-- Display Categories --> +<section class="taxonomy-categories"> +{{ range sort .Site.Taxonomies.categories ".Count" "desc" }} + <a href="{{ .Page.RelPermalink }}" class="category-link" + style="view-transition-name: category-{{ .Page.Title }};"> + <div class="taxonomy-categories-item"> + <h3 class="category-title"> + {{ with .Page.Params.icon }} + <span class="category-icon">{{ . }}</span> + {{ end }} + <span class="category-title-text">{{ .Page.LinkTitle }}</span> + </h3> + <p class="category-description">{{ .Page.Params.description | markdownify }}</p> + </div> + </a> +{{ end }} +</section> diff --git a/layouts/_partials/taxonomy/all-tags.html b/layouts/_partials/taxonomy/all-tags.html new file mode 100644 index 00000000..fae9676f --- /dev/null +++ b/layouts/_partials/taxonomy/all-tags.html @@ -0,0 +1,74 @@ +<!-- Display Tags --> +<section class="taxonomy-tags"> + {{ $categorizedTags := dict }} + {{ $uncategorizedTags := slice }} + + <!-- Group tags by category --> + {{ range $tagName, $tagPages := .Site.Taxonomies.tags }} + {{ $tagPage := site.GetPage (printf "/tags/%s" $tagName) }} + {{ $tagData := dict "Name" $tagName "Count" (len $tagPages) "Page" $tagPage }} + {{ if $tagPage.Params.category }} + {{ $category := $tagPage.Params.category }} + {{ $categoryTags := index $categorizedTags $category | default slice }} + {{ $categoryTags = $categoryTags | append $tagData }} + {{ $categorizedTags = merge $categorizedTags (dict $category $categoryTags) }} + {{ else }} + {{ $uncategorizedTags = $uncategorizedTags | append $tagData }} + {{ end }} + {{ end }} + + <!-- Sort tags by count --> + {{ $uncategorizedTags = sort $uncategorizedTags "Count" "desc" }} + {{ range $category, $categoryTags := $categorizedTags }} + {{ $categorizedTags = merge $categorizedTags (dict $category (sort $categoryTags "Count" "desc")) }} + {{ end }} + + <!-- Display uncategorized tags first --> + {{ if $uncategorizedTags }} + <div class="taxonomy-tags"> + {{ range $uncategorizedTags }} + <a href="{{ .Page.RelPermalink }}" + class="taxonomy-tags-item{{ if lt .Count 4 }} dimmed{{ end }}"> + {{ if .Page.Params.icon }} + {{ if or (hasPrefix .Page.Params.icon "http://") (hasPrefix .Page.Params.icon "https://") }} + <span class="tag-icon"> + <img src="{{ .Page.Params.icon }}" alt="{{ .Page.LinkTitle }}" + style="height:1em;width:1em"> + </span> + {{ else }} + <span class="tag-icon">{{ .Page.Params.icon }}</span> + {{ end }} + {{ end }} + <span class="tag-name">{{ if not .Page.Params.icon }}#{{ end }}{{ .Page.LinkTitle }}</span> + <span class="tag-count">{{ .Count }}</span> + </a> + {{ end }} + </div> + {{ end }} + + <!-- Display categorized tags --> + {{ range $category, $categoryTags := $categorizedTags }} + <div class="taxonomy-category"> + <h3 class="taxonomy-category-title">{{ $category }}</h3> + <div class="taxonomy-tags"> + {{ range $categoryTags }} + <a href="{{ .Page.RelPermalink }}" + class="taxonomy-tags-item{{ if lt .Count 4 }} dimmed{{ end }}"> + {{ if .Page.Params.icon }} + {{ if or (hasPrefix .Page.Params.icon "http://") (hasPrefix .Page.Params.icon "https://") }} + <span class="tag-icon"> + <img src="{{ .Page.Params.icon }}" alt="{{ .Page.LinkTitle }}" + style="height:1em;width:1em"> + </span> + {{ else }} + <span class="tag-icon">{{ .Page.Params.icon }}</span> + {{ end }} + {{ end }} + <span class="tag-name">{{ if not .Page.Params.icon }}#{{ end }}{{ .Page.LinkTitle }}</span> + <span class="tag-count">{{ .Count }}</span> + </a> + {{ end }} + </div> + </div> + {{ end }} +</section> |
