Skip to content

Commit

Permalink
fix: missing categories problem
Browse files Browse the repository at this point in the history
1. missing categories when its name is complicated(with some whitespaces or special characters)
   e.g. `SSG (Static Site Generator)` or `Raspberry Pi(树莓派)`
2. missing ancestors(top level categories) on categories page with tree structure layout
   e.g. categoryBy = "categories" in config.toml
        enableTree = true in config.toml
        categories = ["foo", "bar"] in post’s Front Matter
        then `foo` is missing on categories page
  • Loading branch information
reuixiy committed Nov 18, 2019
1 parent 5806692 commit 5c44452
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
24 changes: 17 additions & 7 deletions layouts/partials/minimal-footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,23 @@
{{ with .Params.categories }}
<div class="post-category">
{{ if eq (len $.Site.Taxonomies.categories) 2 }}
{{- $categoryNow := (delimit . " ") -}}
{{- with $.Site.GetPage (printf `/categories/%s` $categoryNow) -}}
<a href="{{ .RelPermalink }}" class="post-category-link active">{{ $categoryNow | lower }}</a>
{{- range . -}}
{{- $categoryNow := (. | urlize) -}}
{{- $.Scratch.Set "categoryNow" $categoryNow -}}
{{- end -}}
{{- $categoryNow := $.Scratch.Get "categoryNow" -}}
{{- $url := urls.Parse $categoryNow -}}
{{- $path := $url.Path -}}
{{- with $.Site.GetPage (printf `/categories/%s` $path) -}}
<a href="{{ .RelPermalink }}" class="post-category-link active">{{ .Slug | default .LinkTitle | default $path | lower | anchorize }}</a>
{{- end -}}
{{- printf `%s` " | " | safeHTML -}}
{{- range $category, $pages := $.Site.Taxonomies.categories -}}
{{- $category := urls.Parse ($category | urlize) -}}
{{- if ne (string $category) (string $categoryNow) -}}
{{- with $.Site.GetPage (printf `/categories/%s` $category) -}}
<a href="{{ .RelPermalink }}" class="post-category-link">{{ $category | lower }}</a>
{{- $path := $category.Path -}}
{{- with $.Site.GetPage (printf `/categories/%s` $path) -}}
<a href="{{ .RelPermalink }}" class="post-category-link">{{ .Slug | default .LinkTitle | default $path | lower | anchorize }}</a>
{{- end -}}
{{- end -}}
{{- end -}}
Expand All @@ -91,8 +99,10 @@
{{- $.Scratch.Set "delimiter" "" -}}
{{- end -}}
{{- $delimiter := ($.Scratch.Get "delimiter") -}}
{{- with $.Site.GetPage (printf `/categories/%s` .) -}}
{{- printf `<a href="%s" class="post-category-link">%s</a>%s` .RelPermalink (.LinkTitle | default $category) $delimiter | safeHTML -}}
{{- $url := urls.Parse ($category | urlize) -}}
{{- $path := $url.Path -}}
{{- with $.Site.GetPage (printf `/categories/%s` $path) -}}
{{- printf `<a href="%s" class="post-category-link">%s</a>%s` .RelPermalink (.Slug | default .LinkTitle | default $category | lower | anchorize) $delimiter | safeHTML -}}
{{- end -}}
{{- end -}}
{{ end }}
Expand Down
4 changes: 3 additions & 1 deletion layouts/partials/post-meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@
<span class="post-meta-item category">
{{- $icon -}}
{{- range $index, $category := . -}}
{{- $url := urls.Parse ($category | urlize) -}}
{{- $path := $url.Path -}}
{{- if lt $index $length -}}
{{- $.Scratch.Set "delimiter" ($.Site.Params.categoryDelimiter | default "/") -}}
{{- else -}}
{{- $.Scratch.Set "delimiter" "" -}}
{{- end -}}
{{- $delimiter := ($.Scratch.Get "delimiter") -}}
{{- with $.Site.GetPage (printf `/categories/%s` .) -}}
{{- with $.Site.GetPage (printf `/categories/%s` $path) -}}
{{- printf `<a href="%s" class="category-link">%s</a>%s` .RelPermalink (.LinkTitle | default $category) $delimiter | safeHTML -}}
{{- end -}}
{{- end -}}
Expand Down
24 changes: 20 additions & 4 deletions layouts/partials/tree/categories.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
{{ .Scratch.Delete "categories" }}
{{ range $.Site.RegularPages }}
{{ with .Param "categories" }}
{{ $category := (printf `/%s` (replace (delimit . " ") " " "/")) }}
{{ $.Scratch.Delete "category" }}
{{ $length := sub (len .) 1 }}
{{ range $index, $category := . }}
{{ $category := (printf `/%s` ($category | urlize)) }}
{{ $.Scratch.Add "category" $category }}
{{ if lt $index $length }}
{{ $.Scratch.Add "categories" (slice ($.Scratch.Get "category")) }}
{{ end }}
{{ end }}
{{ $category := $.Scratch.Get "category" }}
{{ $.Scratch.Add "categories" (slice $category) }}
{{ end }}
{{ end }}
Expand All @@ -18,9 +27,11 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
{{ $depth := (len $categoryTerms) }}
{{ $padding := (mul $depth 2) }}
{{ $lastTerm := (delimit (last 1 $categoryTerms) " ") }}
{{ with $.Site.GetPage (printf `/categories/%s` $lastTerm) }}
{{ $url := urls.Parse $lastTerm }}
{{ $path := $url.Path }}
{{ with $.Site.GetPage (printf `/categories/%s` $path) }}
<p style="padding-left: {{ $padding }}em">
<a href="{{ .RelPermalink }}" class="category-item">{{ .LinkTitle | default .Data.Term | default $lastTerm }}</a>
<a href="{{ .RelPermalink }}" class="category-item">{{ .LinkTitle | default .Data.Term | default $path }}</a>
{{ if $.Site.Params.displayPostsCount }}
<span class="category-count">{{ printf "(%d)" (len .Data.category) }}</span>
{{ end }}
Expand All @@ -31,7 +42,12 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
{{ range $.Site.RegularPages }}
{{ $page := . }}
{{ with .Param "categories" }}
{{ $category := (printf `/%s` (replace (delimit . " ") " " "/")) }}
{{ $.Scratch.Delete "category" }}
{{ range . }}
{{ $category := (printf `/%s` (. | urlize)) }}
{{ $.Scratch.Add "category" $category }}
{{ end }}
{{ $category := $.Scratch.Get "category" }}
{{ if eq $context $category }}
<p style="padding-left: {{ add $padding 2 }}em">
<a href="{{ $page.RelPermalink }}" class="category-post">{{ $page.LinkTitle }}</a>
Expand Down

0 comments on commit 5c44452

Please sign in to comment.