adds related articles

This commit is contained in:
Drew 2025-03-31 11:43:25 -06:00
parent 46c0a96540
commit 90994d0099
4 changed files with 57 additions and 9 deletions

View File

@ -15,9 +15,13 @@
<p><strong>Tags:</strong> {{ partial "tags.html" . }}</p> <p><strong>Tags:</strong> {{ partial "tags.html" . }}</p>
</aside> </aside>
<div class="prose lg:prose-xl lg:w-2/3 p-4"> <div class="prose lg:prose-xl lg:w-2/3 p-4">
<p class="font-bold text-4xl">{{ .Params.narrator }}: {{ .Params.subject }}</p> <p class="font-bold text-4xl">{{ partial "article-title" . }}</p>
{{ .Content }} {{ .Content }}
</div> </div>
</div> </div>
</article> </article>
<aside>
{{ partial "related-articles" (dict "page" . "tags" .Params.tags "limit" 3) }}
</aside>
<div class="text-center my-12"><a href="/">Go Home</a></div>
{{ end }} {{ end }}

View File

@ -1,16 +1,20 @@
{{ $pages := .Pages }} {{ $pages := .Pages }}
{{ range $pages }} {{ range $pages }}
{{- $page := . -}}
{{- if reflect.IsMap . -}}
{{- $page = .page -}}
{{- end -}}
<li class="flex gap-4 items-center"> <li class="flex gap-4 items-center">
<a class="flex-grow" href="{{ .RelPermalink }}">{{ partial "article-wompum.html" . }}</a> <a class="flex-grow" href="{{ $page.RelPermalink }}">{{ partial "article-wompum.html" $page }}</a>
<time class="text-gray-800 font-iosevka" datetime="{{ .Date.Format "2006-01-02" }}"> <time class="text-gray-800 font-iosevka" datetime="{{ $page.Date.Format "2006-01-02" }}">
<p>{{ .Date.Format "Jan" }}</p> <p>{{ $page.Date.Format "Jan" }}</p>
<p>{{ .Date.Format "02" }}</p> <p>{{ $page.Date.Format "02" }}</p>
<p>{{ .Date.Format "2006" }}</p> <p>{{ $page.Date.Format "2006" }}</p>
</time> </time>
<div class="flex-shrink-0 max-w-max"> <div class="flex-shrink-0 max-w-max">
<a class="text-2xl font-bold hover:text-green-900 underline" href="{{ .RelPermalink }}">{{ .Params.narrator }}: {{ .Params.subject }}</a> <a class="text-2xl font-bold hover:text-green-900 underline" href="{{ $page.RelPermalink }}">{{ partial "article-title" $page }}</a>
<p class="max-w-prose">{{ .Params.summary }}</p> <p class="max-w-prose">{{ $page.Params.summary }}</p>
{{ partial "tags.html" .}} {{ partial "tags.html" $page }}
</div> </div>
</li> </li>
{{ end }} {{ end }}

View File

@ -0,0 +1,3 @@
{{- if and .Params.narrator .Params.subject -}}
{{- .Params.narrator }}: {{ .Params.subject -}}
{{- end -}}

View File

@ -0,0 +1,37 @@
{{- $tags := .tags -}}
{{- $limit := default 3 .limit -}}
{{- $currentPath := .page.RelPermalink -}}
{{- $related := where (where site.RegularPages "Type" "articles") "RelPermalink" "!=" $currentPath -}}
{{- $matchingArticles := slice -}}
{{/* First try to find articles with matching tags */}}
{{- range $related -}}
{{- $matches := 0 -}}
{{- range .Params.tags -}}
{{- if in $tags . -}}
{{- $matches = add $matches 1 -}}
{{- end -}}
{{- end -}}
{{- if gt $matches 0 -}}
{{- $matchingArticles = $matchingArticles | append (dict "page" . "matches" $matches) -}}
{{- end -}}
{{- end -}}
{{/* If we found matching articles, sort by number of matching tags */}}
{{- $finalArticles := slice -}}
{{- if gt (len $matchingArticles) 0 -}}
{{- $finalArticles = first $limit (sort $matchingArticles "matches" "desc") -}}
{{- else -}}
{{/* Fallback to showing other articles sorted by date */}}
{{- $finalArticles = first $limit (sort $related "Date" "desc") -}}
{{- end -}}
{{- if gt (len $finalArticles) 0 -}}
<div class="related-articles flex flex-col gap-4">
<h2 class="title text-3xl">Related Articles</h2>
<ul class="flex flex-col gap-4 w-full">
{{ partial "article-list" (dict "Pages" $finalArticles) }}
</ul>
</div>
{{- end -}}