From 094b3da714f6bf925931c77384a5976c84c41981 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Mon, 21 Oct 2019 22:12:36 +0800 Subject: [PATCH] Add Related-Posts in the bottom of each posts. --- _includes/related-posts.html | 71 ++++++++++++++++++++++++++++++++++ _layouts/post.html | 5 ++- assets/css/_src/main.scss | 75 +++++++++++++++++++++++++++++------- tabs/tags.md | 4 +- 4 files changed, 137 insertions(+), 18 deletions(-) create mode 100644 _includes/related-posts.html diff --git a/_includes/related-posts.html b/_includes/related-posts.html new file mode 100644 index 0000000..c270f81 --- /dev/null +++ b/_includes/related-posts.html @@ -0,0 +1,71 @@ + + +{% assign MAX_SIZE = 3 %} +{% assign TAG_SCORE = 1 %} +{% assign CATEGORY_SCORE = 0.5 %} + +{% assign score_list = "" | split: "" %} +{% assign post_index = 0 %} + +{% 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 %} + + {% endif %} + {% assign post_index = post_index | plus: 1 %} +{% endfor %} + +{% unless score_list.size == 0 %} + {% assign score_list = score_list | sort | reverse %} + {% assign count = 0 %} +
+{% endunless %} \ No newline at end of file diff --git a/_layouts/post.html b/_layouts/post.html index 7398f4e..8a5819c 100644 --- a/_layouts/post.html +++ b/_layouts/post.html @@ -52,7 +52,7 @@ layout: default