From eff2a24f4c71168161846cfa14d2193ac4c56c81 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Thu, 4 Jun 2020 22:01:59 +0800 Subject: [PATCH] Enhanced the related posts. If the number of related posts is less than 3, use the latest posts to supplement. --- _includes/related-posts.html | 107 +++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 35 deletions(-) diff --git a/_includes/related-posts.html b/_includes/related-posts.html index dcb7992..d61f90f 100644 --- a/_includes/related-posts.html +++ b/_includes/related-posts.html @@ -1,53 +1,94 @@ -{% assign MAX_SIZE = 3 %} + +{% assign TOTAL_SIZE = 3 %} + + {% assign TAG_SCORE = 1 %} + + {% assign CATEGORY_SCORE = 0.5 %} +{% assign SEPARATOR = ":" %} + {% assign score_list = "" | split: "" %} -{% assign post_index = 0 %} +{% assign last_index = site.posts.size | minus: 1 %} -{% for post in site.posts %} - {% if post.url != page.url %} - {% assign score = 0 %} - - {% for tag in post.tags %} - {% if page.tags contains tag %} - {% assign score = score | plus: TAG_SCORE %} - {% endif %} - {% endfor %} - - {% for category in post.categories %} - {% if page.categories contains category %} - {% assign score = score | plus: CATEGORY_SCORE %} - {% endif %} - {% endfor %} - - {% if score > 0 %} - {% capture score_item %}{{ score }}:{{ post_index }}{% endcapture %} - {% assign score_list = score_list | push: score_item %} - {% endif %} +{% for i in (0..last_index) %} + {% assign post = site.posts[i] %} + {% if post.url == page.url %} + {% continue %} {% endif %} - {% assign post_index = post_index | plus: 1 %} + + {% assign score = 0 %} + + {% for tag in post.tags %} + {% if page.tags contains tag %} + {% assign score = score | plus: TAG_SCORE %} + {% endif %} + {% endfor %} + + {% for category in post.categories %} + {% if page.categories contains category %} + {% assign score = score | plus: CATEGORY_SCORE %} + {% endif %} + {% endfor %} + + {% if score > 0 %} + {% capture score_item %}{{ score }}{{ SEPARATOR }}{{ i }}{% endcapture %} + {% assign score_list = score_list | push: score_item %} + {% endif %} + {% endfor %} + +{% assign index_list = "" | split: "" %} + {% if score_list.size > 0 %} {% assign score_list = score_list | sort | reverse %} - {% assign count = 0 %} + {% for entry in score_list limit: TOTAL_SIZE %} + {% assign index = entry | split: SEPARATOR | last %} + {% assign index_list = index_list | push: index %} + {% endfor %} +{% endif %} + + +{% assign less = TOTAL_SIZE | minus: index_list.size %} + +{% if less > 0 %} + + {% for i in (0..last_index) %} + {% assign post = site.posts[i] %} + {% if post.url != page.url %} + {% capture cur_index %}{{ i }}{% endcapture %} + {% unless index_list contains cur_index %} + {% assign index_list = index_list | push: cur_index %} + {% assign less = less | minus: 1 %} + {% if less <= 0 %} + {% break %} + {% endif %} + {% endunless %} + {% endif %} + {% endfor %} + +{% endif %} + + +{% if index_list.size > 0 %} - -{% endif %} \ No newline at end of file + +{% endif %}