Skip to content

Commit

Permalink
feat(taxonomy): enhance category name handling and language support
Browse files Browse the repository at this point in the history
- Add support for both single and multilingual category names
- Improve fallback mechanism for language display
- Add pageRef support for custom category URLs
- Standardize category name handling across templates
- Optimize code structure and readability
- Add comprehensive English comments

Changes in:
- layouts/_default/taxonomy.html
- layouts/partials/widget/category_nav.html
- data/categories.yaml

Breaking: No
  • Loading branch information
xianmin committed Nov 11, 2024
1 parent 6cd8351 commit ea18c6a
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 34 deletions.
15 changes: 15 additions & 0 deletions data/authors/ted.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
description = "Writer, Traveler, Food Lover."

[name]
display = "Ted Williams"

# [image]
# url = ""
# width = 128
# height = 128

# [en]
# description = ""

# [en.name]
# display = "Ted"
25 changes: 25 additions & 0 deletions data/categories.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# # Single language format
# index:
# name: "Computer Technology" # Direct name for single language
# weight: 10

# docs:
# name: "Programming"
# weight: 2

# Multilingual format
index:
name:
zh: "自定义归类"
en: "Custom Category"
fr: "Custom Category"
default: "Custom Category" # Fallback when language not found
weight: 1

docs:
name:
zh: "文档"
en: "Documents"
fr: "Documents"
default: "Documents"
weight: 2
96 changes: 67 additions & 29 deletions layouts/_default/taxonomy.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,76 @@
{{ define "title" -}}
{{ if eq .Data.Singular "author" -}}
{{- $author := (index (.Site.Data.authors | default dict) .Title) -}}
{{- $author_lang := (index ($author | default dict) .Site.Language.Lang) -}}
{{- $author_name := $author_lang.name.display | default $author.name.display | default .Title -}}
{{ $author_name }} · {{ .Site.Title }}
{{- else -}}
{{ .Title }} · {{ .Site.Title }}
{{- end }}
{{/* Get display name based on taxonomy type */}}
{{- $displayName := .Title -}}

{{- if eq .Data.Singular "author" -}}
{{/* Handle author display name */}}
{{- $author := (index (.Site.Data.authors | default dict) .Title) -}}
{{- $author_lang := (index ($author | default dict) .Site.Language.Lang) -}}
{{- $displayName = $author_lang.name.display | default $author.name.display | default .Title -}}
{{- else if eq .Data.Singular "category" -}}
{{/* Handle category display name */}}
{{- $category_key := lower .Title -}}
{{- if index .Site.Data "categories" -}}
{{- with (index .Site.Data.categories $category_key) -}}
{{/* Handle both single language and multilingual cases */}}
{{- if reflect.IsMap .name -}}
{{/* For multilingual: try current language first, then fallback to default */}}
{{- $displayName = index .name $.Site.Language.Lang | default .name.default | default $category_key -}}
{{- else -}}
{{/* For single language: use direct name value */}}
{{- $displayName = .name | default $category_key -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $displayName = printf "%s%s" (i18n "category") $displayName -}}
{{- end -}}

{{/* Output the final title */}}
{{ $displayName }} · {{ .Site.Title }}
{{- end }}

{{ define "content"}}
{{ $paginator := .Paginate (where .Data.Pages.ByDate.Reverse "Type" "post") (index .Site.Params "archive-paginate" | default 10) }}
{{/* Setup pagination */}}
{{ $paginator := .Paginate (where .Data.Pages.ByDate.Reverse "Type" "post") (index .Site.Params "archive-paginate" | default 10) }}

{{ $title_block := "" }}
{{ $show_category_nav := false }}
{{/* Initialize variables */}}
{{ $title_block := "" }}
{{ $show_category_nav := false }}

{{ if not $paginator.HasPrev }}
{{ if eq .Data.Plural "tags" }}
{{ $title_block = printf `<div class="archive-title tag"><h2 class="archive-name">%s%s</h2></div>` (i18n "tag") .Title }}
{{ else if eq .Data.Plural "categories" }}
{{ $title_block = printf `<div class="archive-title category"><h2 class="archive-name">%s%s</h2></div>` (i18n "category") .Title }}
{{ $show_category_nav = true }}
{{ else if eq .Data.Singular "author" }}
{{- $author := (index (.Site.Data.authors | default dict) .Title) -}}
{{- $author_lang := (index ($author | default dict) .Site.Language.Lang) -}}
{{- $author_name := $author_lang.name.display | default $author.name.display | default .Title -}}
{{ $title_block = printf `<div class="archive-title author"><h2 class="archive-name">%s%s</h2></div>` (i18n "author_item") $author_name }}
{{/* Generate title block for first page */}}
{{ if not $paginator.HasPrev }}
{{ if eq .Data.Plural "tags" }}
{{ $title_block = printf `<div class="archive-title tag"><h2 class="archive-name">%s%s</h2></div>` (i18n "tag") .Title }}
{{ else if eq .Data.Plural "categories" }}
{{/* Get category display name */}}
{{- $category_key := lower .Title -}}
{{- $displayName := $category_key -}}
{{ if index .Site.Data "categories" }}
{{ with (index .Site.Data.categories $category_key) }}
{{/* Handle both single language and multilingual cases */}}
{{ if reflect.IsMap .name }}
{{ $displayName = index .name $.Site.Language.Lang | default .name.default | default $category_key }}
{{ else }}
{{ $displayName = .name | default $category_key }}
{{ end }}
{{ end }}
{{ end }}
{{ $title_block = printf `<div class="archive-title category"><h2 class="archive-name">%s%s</h2></div>` (i18n "category") $displayName }}
{{ $show_category_nav = true }}
{{ else if eq .Data.Singular "author" }}
{{/* Get author display name */}}
{{- $author := (index (.Site.Data.authors | default dict) .Title) -}}
{{- $author_lang := (index ($author | default dict) .Site.Language.Lang) -}}
{{- $displayName := $author_lang.name.display | default $author.name.display | default .Title -}}
{{ $title_block = printf `<div class="archive-title author"><h2 class="archive-name">%s%s</h2></div>` (i18n "author_item") $displayName }}
{{ end }}
{{ end }}
{{ end }}

{{ partial "archive-list.html" (dict
"paginator" $paginator
"title_block" $title_block
"show_category_nav" $show_category_nav
"context" .
) }}
{{/* Render the archive list */}}
{{ partial "archive-list.html" (dict
"paginator" $paginator
"title_block" $title_block
"show_category_nav" $show_category_nav
"context" .
) }}
{{ end }}
53 changes: 48 additions & 5 deletions layouts/partials/widget/category_nav.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
<div class="archive-categories">
{{ $postURL := printf "%s/" (`post` | relLangURL) }}
{{ $allPosts := where .Site.RegularPages "Type" "post" }}
{{ $lang := .Site.Language.Lang }}

<a role="button" href="{{ $postURL }}"
class="{{ if ne .RelPermalink $postURL }}outline{{ end }}">
{{ i18n "archive" }} ({{ len $allPosts }})
</a>

{{/* Create a slice to store category data */}}
{{ $categories := slice }}
{{ range $name, $taxonomy := .Site.Taxonomies.categories }}
{{ with $.Site.GetPage (printf "/categories/%s" $name) }}
<a role="button" href="{{ .Permalink }}"
class="{{ if ne $.RelPermalink .RelPermalink }}outline{{ end }}">
{{ $name }} ({{ len $taxonomy }})
</a>
{{ with $.Site.GetPage (printf "/categories/%s" $name) }}
{{ $weight := 100 }} {{/* Default weight */}}
{{ $displayName := $name }}
{{ $categoryURL := .Permalink }} {{/* Default URL */}}

{{ if index $.Site.Data "categories" }}
{{ with (index $.Site.Data.categories $name) }}
{{ $weight = .weight | default 100 }}

{{/* Handle both single language and multilingual cases */}}
{{ if reflect.IsMap .name }}
{{/* For multilingual: try current language first, then fallback to default name */}}
{{ $displayName = index .name $lang | default .name.default | default $name }}
{{ else }}
{{/* For single language: use direct name value */}}
{{ $displayName = .name | default $name }}
{{ end }}

{{/* Handle custom URL if pageRef is set */}}
{{ if .pageRef }}
{{ with $.Site.GetPage (printf "/categories/%s" .pageRef) }}
{{ $categoryURL = .Permalink }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}

{{ $categories = $categories | append (dict
"name" $name
"displayName" $displayName
"weight" $weight
"permalink" $categoryURL
"relPermalink" .RelPermalink
"count" (len $taxonomy)
) }}
{{ end }}
{{ end }}

{{/* Sort categories by weight, then by count (descending) */}}
{{ range sort (sort $categories "count" "desc") "weight" }}
<a role="button" href="{{ .permalink }}"
class="{{ if ne $.RelPermalink .relPermalink }}outline{{ end }}">
{{ .displayName }}
({{ .count }})
</a>
{{ end }}
</div>

0 comments on commit ea18c6a

Please sign in to comment.