add post-description post-nav and related-posts

This commit is contained in:
geekifan
2025-04-22 13:46:12 +08:00
parent d24c640ab7
commit b0c293efcd
3 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,21 @@
{{- /* Get post description or generate it from the post content */ -}}
{{- $max_length := default 200 .Params.max_length -}}
{{- $description := "" -}}
{{- if .Description -}}
{{- $description = .Description -}}
{{- else if .Params.description -}}
{{- $description = .Params.description -}}
{{- else -}}
{{- /* Remove the line number of the code snippet */ -}}
{{- $content := .Content -}}
{{- if findRE `<td class="rouge-gutter gl"><pre class="lineno">` $content -}}
{{- $content = replace $content `<td class="rouge-gutter gl"><pre class="lineno">` `<!-- <td class="rouge-gutter gl"><pre class="lineno">` -}}
{{- $content = replace $content `</td><td class="rouge-code">` `</td> --><td class="rouge-code">` -}}
{{- end -}}
{{- $description = $content | markdownify | plainify | replaceRE `\s+` ` ` -}}
{{- end -}}
{{- (trim $description " ") | truncate $max_length | safeHTML -}}

View File

@ -0,0 +1,32 @@
<nav class="post-navigation d-flex justify-content-between" aria-label="Post Navigation">
{{ $previous := i18n "post.button.previous" }}
{{ $next := i18n "post.button.next" }}
{{ with .PrevInSection }}
<a
href="{{ .RelPermalink }}"
class="btn btn-outline-primary"
aria-label="{{ $previous }}"
>
<p>{{ .Title }}</p>
</a>
{{ else }}
<div class="btn btn-outline-primary disabled" aria-label="{{ $previous }}">
<p>-</p>
</div>
{{ end }}
{{ with .NextInSection }}
<a
href="{{ .RelPermalink }}"
class="btn btn-outline-primary"
aria-label="{{ $next }}"
>
<p>{{ .Title }}</p>
</a>
{{ else }}
<div class="btn btn-outline-primary disabled" aria-label="{{ $next }}">
<p>-</p>
</div>
{{ end }}
</nav>

View File

@ -0,0 +1,95 @@
<!-- Recommend the other 3 posts according to the tags and categories of the current post. -->
{{ $TOTAL_SIZE := 3 }}
{{ $TAG_SCORE := 1 }}
{{ $CATEGORY_SCORE := 0.5 }}
{{ $SEPARATOR := ":" }}
{{ $currentPage := . }}
{{ $allPosts := where .Site.RegularPages "Type" "post" }}
{{ $matchPosts := slice }}
<!-- Get posts with matching categories -->
{{ range $currentPage.Params.categories }}
{{ $categoryPosts := where $allPosts "Params.categories" "intersect" (slice .) }}
{{ $matchPosts = $matchPosts | union $categoryPosts }}
{{ end }}
<!-- Get posts with matching tags -->
{{ range $currentPage.Params.tags }}
{{ $tagPosts := where $allPosts "Params.tags" "intersect" (slice .) }}
{{ $matchPosts = $matchPosts | union $tagPosts }}
{{ end }}
{{ $scoreList := slice }}
{{ range $i, $post := $matchPosts }}
{{ if eq $post.RelPermalink $currentPage.RelPermalink }}
{{ continue }}
{{ end }}
{{ $score := 0 }}
<!-- Calculate tag score -->
{{ range $post.Params.tags }}
{{ if in $currentPage.Params.tags . }}
{{ $score = add $score $TAG_SCORE }}
{{ end }}
{{ end }}
<!-- Calculate category score -->
{{ range $post.Params.categories }}
{{ if in $currentPage.Params.categories . }}
{{ $score = add $score $CATEGORY_SCORE }}
{{ end }}
{{ end }}
{{ if gt $score 0 }}
{{ $scoreItem := printf "%s%s%d" (string $score) $SEPARATOR $i }}
{{ $scoreList = $scoreList | append $scoreItem }}
{{ end }}
{{ end }}
{{ $indexList := slice }}
{{ if gt (len $scoreList) 0 }}
{{ $scoreList = sort $scoreList "value" "desc" }}
{{ range first $TOTAL_SIZE $scoreList }}
{{ $parts := split . $SEPARATOR }}
{{ $index := index $parts 1 | int }}
{{ $indexList = $indexList | append $index }}
{{ end }}
{{ end }}
{{ $relatePosts := slice }}
{{ range $indexList }}
{{ $i := . }}
{{ $post := index $matchPosts $i }}
{{ $relatePosts = $relatePosts | append $post }}
{{ end }}
{{ if gt (len $relatePosts) 0 }}
<aside id="related-posts" aria-labelledby="related-label">
<h3 class="mb-4" id="related-label">
{{ i18n "post.relate_posts" }}
</h3>
<nav class="row row-cols-1 row-cols-md-2 row-cols-xl-3 g-4 mb-4">
{{ range $relatePosts }}
<article class="col">
<a href="{{ .RelPermalink }}" class="post-preview card h-100">
<div class="card-body">
{{ partial "datetime.html" (dict "date" .Date "lang" site.Language.Lang) }}
<h4 class="pt-0 my-2">{{ .Title }}</h4>
<div class="text-muted">
<p>{{ partial "post-description.html" . }}</p>
</div>
</div>
</a>
</article>
{{ end }}
</nav>
</aside>
<!-- #related-posts -->
{{ end }}