2025-04-07 15:41:29 -06:00

38 lines
1.3 KiB
HTML

{{- $topics := .topics -}}
{{- $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 topics */}}
{{- range $related -}}
{{- $matches := 0 -}}
{{- range .Params.topics -}}
{{- if in $topics . -}}
{{- $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 topics */}}
{{- $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 font-bold">Related Articles</h2>
<ul class="flex flex-col gap-4 w-full">
{{ partial "article-list" (dict "Pages" $finalArticles) }}
</ul>
</div>
{{- end -}}