Browse Source

Add custom title / translation of categories (#755)

### Prerequisites

Put an `x` into the box(es) that apply:

- [X] This pull request fixes a bug.
- [X] This pull request adds a feature.
- [ ] This pull request introduces breaking change.

### Description

This PR is similar to #754, but updates the categories to utilize Hugos
taxonomy system, instead of the tags .

This PR edits the posts/single layout to use `.GetTerms "categories"` to
fetch a richer model of the categories, and the taxonomy/categories
partial to use this model, instead of just urlizing the category name,
so categories with a custom title, renders correctly on a post.

This also enables translation of the categories shown on a post, while
enabling switching languages on the category page without losing
context.

To show this in action, I've added a translation of the "themes" and
"syntax" categories in the exampleSite into PT-BR, as "temas" and
"sintaxe", respectively, and switched the pt-br page to use the english
category ids so they are seen as the same category regardless of
translation.


![image](https://user-images.githubusercontent.com/2725205/210677278-908bd3fc-5edb-4c40-ab53-270d148dd061.png)

Also, this enables customizing/translating the category url, by
specifying a slug and/or url in the category frontmatter.


![image](https://user-images.githubusercontent.com/2725205/210677524-9eece29d-e110-406b-9db0-5d8b4406b112.png)

If you switch languages here on the category page, you'll see the
translated category and corresponding posts, instead of dropping out to
the front page like before.

### Issues Resolved

If you wanted to use Hugo taxonomy system for the page categories, only
the category id showed up on the post itself. This PR fixes that.

### Checklist

Put an `x` into the box(es) that apply:

#### General

- [X] Describe what changes are being made
- [X] Explain why and how the changes were necessary and implemented
respectively
- [ ] Reference issue with `#<ISSUE_NO>` if applicable

#### Resources

- [ ] If you have changed any SCSS code, run `make release` to
regenerate all CSS files

#### Contributors

- [ ] Add yourself to `CONTRIBUTORS.md` if you aren't on it already

Co-authored-by: Luiz F. A. de Prá <luizdepra@users.noreply.github.com>
Martin Hellspong 3 years ago
parent
commit
473ff712bd

+ 6 - 0
exampleSite/content/categories/syntax/_index.pt-br.md

@@ -0,0 +1,6 @@
++++
+draft = false
+date = 2023-01-04T23:21:06+01:00
+title = "sintaxe"
+url = "categoria/sintaxe"
++++

+ 6 - 0
exampleSite/content/categories/themes/_index.pt-br.md

@@ -0,0 +1,6 @@
++++
+draft = false
+date = 2023-01-04T23:21:18+01:00
+title = "temas"
+url = "categoria/temas"
++++

+ 2 - 2
exampleSite/content/posts/markdown-syntax.pt-br.md

@@ -9,8 +9,8 @@ tags = [
     "html",
     "html",
 ]
 ]
 categories = [
 categories = [
-    "temas",
-    "sintaxe",
+    "themes",
+    "syntax",
 ]
 ]
 series = ["Guia do Thema"]
 series = ["Guia do Thema"]
 aliases = ["migrate-from-jekyl"]
 aliases = ["migrate-from-jekyl"]

+ 1 - 1
layouts/partials/taxonomy/categories.html

@@ -4,6 +4,6 @@
     {{- if gt $index 0 }}
     {{- if gt $index 0 }}
       <span class="separator">•</span>
       <span class="separator">•</span>
     {{- end }}
     {{- end }}
-    <a href="{{ ( printf "categories/%s/" ( . | urlize ) ) | relLangURL }}">{{ . }}</a>
+    <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
   {{- end -}}
   {{- end -}}
 </div>
 </div>

+ 1 - 1
layouts/posts/single.html

@@ -26,7 +26,7 @@
             </span>
             </span>
           </div>
           </div>
           {{ with .GetTerms "authors" }}{{ partial "taxonomy/authors.html" . }}{{ end }}
           {{ with .GetTerms "authors" }}{{ partial "taxonomy/authors.html" . }}{{ end }}
-          {{ with .Page.Params.Categories }}{{ partial "taxonomy/categories.html" . }}{{ end }}
+          {{ with .GetTerms "categories" }}{{ partial "taxonomy/categories.html" . }}{{ end }}
           {{ with .GetTerms "tags" }}{{ partial "taxonomy/tags.html" . }}{{ end }}
           {{ with .GetTerms "tags" }}{{ partial "taxonomy/tags.html" . }}{{ end }}
         </div>
         </div>
       </header>
       </header>