From 1916b6b6dc183372ad96eddb0c68914d1a7b8eca Mon Sep 17 00:00:00 2001 From: Jack Jackson Date: Sat, 2 Jul 2022 20:43:32 -0700 Subject: [PATCH] List all related pages in sidebar (not just older ones) --- blog/config.toml | 17 ++++++++++ blog/content/posts/tags-in-archetype.md | 27 +++++++++++++++ blog/layouts/partials/menu-contextual.html | 39 ++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 blog/layouts/partials/menu-contextual.html diff --git a/blog/config.toml b/blog/config.toml index 2fab8dd..9a34394 100644 --- a/blog/config.toml +++ b/blog/config.toml @@ -24,3 +24,20 @@ SectionPagesMenu = "main" # https://gohugo.io/content-management/taxonomies/#example-removing-default-taxonomies [taxonomies] tag = 'tags' + +# https://gohugo.io/content-management/related/ +# https://blog.scubbo.org/posts/tags-in-archetype/ +# `includeNewer = true` => include pages that are newer than the current page in the "Related" sidebar +[related] + includeNewer = true + threshold = 80 + toLower = false +[[related.indices]] + name = 'keywords' + weight = 100 +[[related.indices]] + name = 'tags' + weight = 80 +[[related.indices]] + name = 'date' + weight = 10 diff --git a/blog/content/posts/tags-in-archetype.md b/blog/content/posts/tags-in-archetype.md index 5bf9a33..a2707fe 100644 --- a/blog/content/posts/tags-in-archetype.md +++ b/blog/content/posts/tags-in-archetype.md @@ -33,6 +33,33 @@ On the topic of tags, I have a couple of improvements I want to introduce: * I'd like to try messing with the ordering[^4] on that tags page, too. At a glance, it looks like it's sorting by "_most recent post within that tag_", which...I guess makes sense. My first thought was that ordering by "_number of posts in tag_" might make more sense (putting "larger" tags above smaller ones), but that might lead to discoverability problems if I write a bunch of posts under one tag and then it's harder to find later ones. Then again, later posts would presumably show up on the standard page anyway? Eh - that's Future-Jack's problem when he starts thinking about this problem (if, indeed, he ever does...) * It seems like the "related" sidebar only ever shows previous content - [this post](https://blog.scubbo.org/posts/cheating-at-word-games-part-3/) shows the two preceding posts under the tag, but [this one](https://blog.scubbo.org/posts/cheating-at-word-games/) doesn't show any. That's both puzzling (the full list of "_posts that exist under a given tag_" must be available at _some_ point in the build process, and it's not like running a second iteration over all blog posts will massively affect runtime efficiency of the process) and counter-intuitive (a reader who is linked to the first blog post in a series would want to be able to easily find a link to the next one without clicking out to the tag page or the author explicitly adding the link in the body). This _should_ be pretty simple to solve by using a [Taxonomy Template](https://gohugo.io/templates/taxonomy-templates/#list-content-with-the-same-taxonomy-term), but I'd be more curious to find out why this wasn't implemented the _complete_ way to begin with. +--- + +**EDIT:** well, there's an easy-ish solution to including newer content in the Related Items sidebar. Turns out it was simple as changing a single value - `related.includeNewer` - in Hugo's configuration, as described [here](https://gohugo.io/content-management/related/). Unfortunately, "_If you add a_ `related` _config section, you need to add a complete configuration. It is not possible to just set, say,_ `includeNewer` _and use the rest from the Hugo defaults._", so I had to copy the entire default configuration and change a single value in it. The fact that that config option is specifically called out as the example suggests that this is a common non-default configuration - I am vindicated! The justification given in documentation for the default option is that including newer related content "_will mean that the output for older posts may change as new related content gets added_", which I _guess_ could be considered as unexpected behaviour if you expect a post to remain immutable once published. + +However, the resultant Related Pages are (presumably) listed in order of "most-to-least-related" (with order undefined between pages that are equally-related) - or, at least, I can say that they're not listed in a consistent date order, because during testing, the related pages for the [2nd Wordle post]({{< ref "/posts/cheating-at-word-games-part-2" >}}) listed 1 then 3, but the related pages for the 3rd post listed 2 then 1. The sort-order for the Related Pages isn't a configurable option, so, in order to change this, I had to override [Ananke's default layout for sidebar content](https://github.com/theNewDynamic/gohugo-theme-ananke/blob/master/layouts/partials/menu-contextual.html) - I changed: + +``` +{{ $related := .Site.RegularPages.Related . | first 15 }} + +{{ with $related }} +...rendering of each item of $related +``` + +to + +``` +{{ $related := .Site.RegularPages.Related . | first 15 }} +{{ $related_sorted := sort $related "Date" "desc"}} + +{{ with $related_sorted }} +...rendering of each item of $related_sorted +... +``` + +So far as I can tell, it's not possible to pipe _into_ `sort`, you have to have it return to a new variable. + + [^0]: These links are permalinks to a previous commit, because as part of the commit that introduces _this_ post, I unified those disparate tags to a single `#meta` tag. [^1]: Disclaimer: computers will almost always introduce more problems than they solve. The only code that is guaranteed to lead not to lead to a net-increase of problems is [this](https://github.com/kelseyhightower/nocode). [^2]: Confusingly, "Archetypes" do what you might naïvely expect "Templates" to do. Explicitly: Archetypes are ~~templates~~ prototypes from which new content are created, and Templates are ways to express code-like logic in a non-code context, or equivalently to provide an execution/extension point in non-code content that expresses "_execute some code, and insert the output here_". An example of a template is writing the following in a blog post (note that this is not legal template syntax!): "_If you're interested in learning more about this topic, check the following posts: `{{ getAllPostsWithTag "coolTopic" | .Title}}`_", to print the titles of all posts with tag `#coolTopic`. diff --git a/blog/layouts/partials/menu-contextual.html b/blog/layouts/partials/menu-contextual.html new file mode 100644 index 0000000..ac24b4a --- /dev/null +++ b/blog/layouts/partials/menu-contextual.html @@ -0,0 +1,39 @@ +{{/* + Copied and adapted from Ananke theme's implementation, initially in order to enforce date-ordering on Related Pages: + https://blog.scubbo.org/posts/tags-in-archetype/ +*/}} +{{/* + Use Hugo's native Table of contents feature. You must set `toc: true` in your parameters for this to show. + https://gohugo.io/content-management/toc/ +*/}} + +{{- if .Params.toc -}} + +{{- end -}} + +{{/* + Use Hugo's native related content feature to pull in content that may have similar parameters, like tags. etc. + https://gohugo.io/content-management/related/ +*/}} + +{{ $related := .Site.RegularPages.Related . | first 15 }} +{{/* So far as I can tell, it's not possible to pipe _into_ sort - it needs to return */}} +{{ $related_sorted := sort $related "Date" "desc"}} + +{{ with $related_sorted }} + +{{ end }}