{{ $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 }}