From b0c293efcdc8adfa18426995513718f83fb0e3d8 Mon Sep 17 00:00:00 2001 From: geekifan Date: Tue, 22 Apr 2025 13:46:12 +0800 Subject: [PATCH] add post-description post-nav and related-posts --- layouts/partials/post-description.html | 21 ++++++ layouts/partials/post-nav.html | 32 +++++++++ layouts/partials/related-posts.html | 95 ++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 layouts/partials/post-description.html create mode 100644 layouts/partials/post-nav.html create mode 100644 layouts/partials/related-posts.html diff --git a/layouts/partials/post-description.html b/layouts/partials/post-description.html new file mode 100644 index 0000000..db6c9b4 --- /dev/null +++ b/layouts/partials/post-description.html @@ -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 `
` $content -}} + {{- $content = replace $content ` ` `` -}} + {{- end -}} + + {{- $description = $content | markdownify | plainify | replaceRE `\s+` ` ` -}} +{{- end -}} + +{{- (trim $description " ") | truncate $max_length | safeHTML -}} \ No newline at end of file diff --git a/layouts/partials/post-nav.html b/layouts/partials/post-nav.html new file mode 100644 index 0000000..8892a90 --- /dev/null +++ b/layouts/partials/post-nav.html @@ -0,0 +1,32 @@ + \ No newline at end of file diff --git a/layouts/partials/related-posts.html b/layouts/partials/related-posts.html new file mode 100644 index 0000000..065a11b --- /dev/null +++ b/layouts/partials/related-posts.html @@ -0,0 +1,95 @@ + + +{{ $TOTAL_SIZE := 3 }} +{{ $TAG_SCORE := 1 }} +{{ $CATEGORY_SCORE := 0.5 }} +{{ $SEPARATOR := ":" }} + +{{ $currentPage := . }} +{{ $allPosts := where .Site.RegularPages "Type" "post" }} +{{ $matchPosts := slice }} + + +{{ range $currentPage.Params.categories }} + {{ $categoryPosts := where $allPosts "Params.categories" "intersect" (slice .) }} + {{ $matchPosts = $matchPosts | union $categoryPosts }} +{{ end }} + + +{{ 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 }} + + + {{ range $post.Params.tags }} + {{ if in $currentPage.Params.tags . }} + {{ $score = add $score $TAG_SCORE }} + {{ end }} + {{ end }} + + + {{ 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 }} + + +{{ end }} \ No newline at end of file