adds related articles
This commit is contained in:
parent
46c0a96540
commit
90994d0099
@ -15,9 +15,13 @@
|
||||
<p><strong>Tags:</strong> {{ partial "tags.html" . }}</p>
|
||||
</aside>
|
||||
<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 }}
|
||||
</div>
|
||||
</div>
|
||||
</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 }}
|
@ -1,16 +1,20 @@
|
||||
{{ $pages := .Pages }}
|
||||
{{ range $pages }}
|
||||
{{- $page := . -}}
|
||||
{{- if reflect.IsMap . -}}
|
||||
{{- $page = .page -}}
|
||||
{{- end -}}
|
||||
<li class="flex gap-4 items-center">
|
||||
<a class="flex-grow" href="{{ .RelPermalink }}">{{ partial "article-wompum.html" . }}</a>
|
||||
<time class="text-gray-800 font-iosevka" datetime="{{ .Date.Format "2006-01-02" }}">
|
||||
<p>{{ .Date.Format "Jan" }}</p>
|
||||
<p>{{ .Date.Format "02" }}</p>
|
||||
<p>{{ .Date.Format "2006" }}</p>
|
||||
<a class="flex-grow" href="{{ $page.RelPermalink }}">{{ partial "article-wompum.html" $page }}</a>
|
||||
<time class="text-gray-800 font-iosevka" datetime="{{ $page.Date.Format "2006-01-02" }}">
|
||||
<p>{{ $page.Date.Format "Jan" }}</p>
|
||||
<p>{{ $page.Date.Format "02" }}</p>
|
||||
<p>{{ $page.Date.Format "2006" }}</p>
|
||||
</time>
|
||||
<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>
|
||||
<p class="max-w-prose">{{ .Params.summary }}</p>
|
||||
{{ partial "tags.html" .}}
|
||||
<a class="text-2xl font-bold hover:text-green-900 underline" href="{{ $page.RelPermalink }}">{{ partial "article-title" $page }}</a>
|
||||
<p class="max-w-prose">{{ $page.Params.summary }}</p>
|
||||
{{ partial "tags.html" $page }}
|
||||
</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
|
3
layouts/partials/article-title.html
Normal file
3
layouts/partials/article-title.html
Normal file
@ -0,0 +1,3 @@
|
||||
{{- if and .Params.narrator .Params.subject -}}
|
||||
{{- .Params.narrator }}: {{ .Params.subject -}}
|
||||
{{- end -}}
|
37
layouts/partials/related-articles.html
Normal file
37
layouts/partials/related-articles.html
Normal 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 -}}
|
Loading…
x
Reference in New Issue
Block a user