← hugo-clarity

improve series functionality 432 merged

011f95d4a6ad3658c1e7c82ced4f14d2b3d7434a · ovidiucs · Jul 13, 2023 02:02

+26 −23 · 7 files changed

  • exampleSite/config/_default/params.toml +2 −1
  • exampleSite/content/post/bundle/index.md +4 −2
  • exampleSite/content/post/markdown-syntax.md +1 −1
  • exampleSite/content/post/markdown-syntax.pt.md +1 −1
  • layouts/_default/single.html +4 −4
  • layouts/partials/related.html +5 −7
  • layouts/partials/sidebar.html +9 −7
diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml
index 6557453..c239510 100644
--- a/exampleSite/config/_default/params.toml
+++ b/exampleSite/config/_default/params.toml
@@ -129,7 +129,8 @@ languageMenuName = "🌐"
 # notice of the footer.
 # since = 2016
 
-# Show related content at the end of an article based on the 'series' taxonomy. Can be set in post front matter.
+# Show related content at the end of an article or in the sidebar, based on the 'series' taxonomy.
+# Both parameters can be overridden in the post's front matter.
 # showRelatedInArticle = false
 # showRelatedInSidebar = false
 
diff --git a/exampleSite/content/post/bundle/index.md b/exampleSite/content/post/bundle/index.md
index 6b1ab1a..84c9e49 100644
--- a/exampleSite/content/post/bundle/index.md
+++ b/exampleSite/content/post/bundle/index.md
@@ -18,8 +18,10 @@ categories:
   - syntax
 tags:
   - Hugo
-series:
-  - Themes Guide
+series: Themes Guide
+# the following overrides the settings from params.toml:
+showRelatedInArticle: true
+showRelatedInSidebar: true
 ---
 
 [Page bundles](https://gohugo.io/content-management/page-bundles/) are an optional way to [organize page resources](https://gohugo.io/content-management/page-resources/) within Hugo.
diff --git a/exampleSite/content/post/markdown-syntax.md b/exampleSite/content/post/markdown-syntax.md
index c9b8062..30e9e0b 100644
--- a/exampleSite/content/post/markdown-syntax.md
+++ b/exampleSite/content/post/markdown-syntax.md
@@ -15,7 +15,7 @@ categories = [
     "themes",
     "syntax",
 ]
-series = ["Themes Guide"]
+series = "Themes Guide"
 aliases = ["migrate-from-jekyl"]
 thumbnail = "images/building.png"
 +++
diff --git a/exampleSite/content/post/markdown-syntax.pt.md b/exampleSite/content/post/markdown-syntax.pt.md
index 695235a..0cce0e8 100644
--- a/exampleSite/content/post/markdown-syntax.pt.md
+++ b/exampleSite/content/post/markdown-syntax.pt.md
@@ -15,7 +15,7 @@ categories = [
     "temas",
     "sintaxe",
 ]
-series = ["Guia de Temas"]
+series = "Guia de Temas"
 aliases = ["migrar-de-jekyl"]
 thumbnail = "images/building.png"
 +++
diff --git a/layouts/_default/single.html b/layouts/_default/single.html
index e0299b2..22c4267 100644
--- a/layouts/_default/single.html
+++ b/layouts/_default/single.html
@@ -29,13 +29,13 @@
       {{- .Content }}
     </div>
 
-    {{- $showRelatedInArticle := true }}
-    {{- if eq $s.showRelatedInArticle false }}
-      {{- $showRelatedInArticle = false }}
+    {{- $showRelatedInArticle := $s.showRelatedInArticle }}
+    {{- if eq $p.showRelatedInArticle true }}
+      {{- $showRelatedInArticle = true }}
     {{- else if eq $p.showRelatedInArticle false }}
       {{- $showRelatedInArticle = false }}
     {{- end }}
-    {{- if ne $showRelatedInArticle false }}
+    {{- if (and ($showRelatedInArticle) (isset $p "series")) }}
       {{- partial "related" . }}
     {{- end }}
 
diff --git a/layouts/partials/related.html b/layouts/partials/related.html
index 757bcb5..41057a0 100644
--- a/layouts/partials/related.html
+++ b/layouts/partials/related.html
@@ -1,9 +1,7 @@
-{{ if isset .Params "series" }}
 {{$related := where .Site.RegularPages ".Params.series" "eq" .Params.series }}
-	<h2 class="post_related">Posts in this Series</h2>
-	<ul>
-	  {{ range $related }}
-	      <li><a href="{{ .Permalink }}" class="nav-link" title="{{ .Title }}">{{ .Title }}</a></li>
-	  {{ end }}
-	</ul>
+<h2 class="post_related">{{ T "series_posts" }}</h2>
+<ul>
+{{ range sort $related "Date" }}
+  <li><a href="{{ .Permalink }}" class="nav-link" title="{{ .Title }}">{{ .Title }}</a></li>
 {{ end }}
+</ul>
diff --git a/layouts/partials/sidebar.html b/layouts/partials/sidebar.html
index 17b18b2..eb75780 100644
--- a/layouts/partials/sidebar.html
+++ b/layouts/partials/sidebar.html
@@ -40,21 +40,23 @@
       </div>
     {{ end }}
 
-    {{- $relatedInSidebar := true }}
-    {{- if eq $s.showRelatedInSidebar false }}
-      {{ $relatedInSidebar = false }}
+    {{- $showRelatedInSidebar := $s.showRelatedInSidebar }}
+    {{- if eq .Params.showRelatedInSidebar true }}
+      {{- $showRelatedInSidebar = true }}
+    {{- else if eq .Params.showRelatedInSidebar false }}
+      {{- $showRelatedInSidebar = false }}
     {{- end }}
-    {{ if (and ($relatedInSidebar) (isset .Params "series") ) }}
+    {{- if (and ($showRelatedInSidebar) (isset .Params "series")) }}
       {{$related := where .Site.RegularPages ".Params.series" "eq" .Params.series }}
-	    <h2 class="mt-4">{{ T "series_posts" }}</h2>
+      <h2 class="mt-4">{{ T "series_posts" }}</h2>
       <ul>
-        {{ range $related }}
+        {{ range sort $related "Date" }}
         <li>
           <a href="{{ .Permalink }}" class="nav-link" title="{{ .Title }}">{{ .Title | markdownify }}</a>
         </li>
         {{ end }}
       </ul>
-    {{ end }}
+    {{- end }}
 
     {{- $posts := where .Site.RegularPages "Type" "in" $s.mainSections }}
     {{- $featured := default 8 $s.numberOfFeaturedPosts }}