Enhanced the related posts.

If the number of related posts is less than 3, use the latest posts to supplement.
This commit is contained in:
Cotes Chung 2020-06-04 22:01:59 +08:00
parent 3caafbd50d
commit eff2a24f4c

View file

@ -1,21 +1,34 @@
<!-- <!--
The related posts of current post. Recommend the other 3 posts according to the tags and categories of the current post,
Placed in the bottom of every single post. if the number is not enough, use the other latest posts to supplement.
v2.0 v2.0
https://github.com/cotes2020/jekyll-theme-chirpy https://github.com/cotes2020/jekyll-theme-chirpy
© 2019 Cotes Chung © 2019 Cotes Chung
Published under the MIT License Published under the MIT License
--> -->
{% assign MAX_SIZE = 3 %} <!-- The total size of related posts -->
{% assign TOTAL_SIZE = 3 %}
<!-- An random integer that bigger than 0 -->
{% assign TAG_SCORE = 1 %} {% assign TAG_SCORE = 1 %}
<!-- Equals to TAG_SCORE / {max_categories_hierarchy} -->
{% assign CATEGORY_SCORE = 0.5 %} {% assign CATEGORY_SCORE = 0.5 %}
{% assign score_list = "" | split: "" %} {% assign SEPARATOR = ":" %}
{% assign post_index = 0 %}
{% assign score_list = "" | split: "" %}
{% assign last_index = site.posts.size | minus: 1 %}
{% for i in (0..last_index) %}
{% assign post = site.posts[i] %}
{% if post.url == page.url %}
{% continue %}
{% endif %}
{% for post in site.posts %}
{% if post.url != page.url %}
{% assign score = 0 %} {% assign score = 0 %}
{% for tag in post.tags %} {% for tag in post.tags %}
@ -31,23 +44,51 @@
{% endfor %} {% endfor %}
{% if score > 0 %} {% if score > 0 %}
{% capture score_item %}{{ score }}:{{ post_index }}{% endcapture %} {% capture score_item %}{{ score }}{{ SEPARATOR }}{{ i }}{% endcapture %}
{% assign score_list = score_list | push: score_item %} {% assign score_list = score_list | push: score_item %}
{% endif %} {% endif %}
{% endif %}
{% assign post_index = post_index | plus: 1 %}
{% endfor %} {% endfor %}
{% assign index_list = "" | split: "" %}
{% if score_list.size > 0 %} {% if score_list.size > 0 %}
{% assign score_list = score_list | sort | reverse %} {% 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 %}
<!-- Fill with the other newlest posts -->
{% 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 %}
<div id="related-posts" class="mt-4 mb-2 mb-sm-4 pb-2"> <div id="related-posts" class="mt-4 mb-2 mb-sm-4 pb-2">
<h3 class="pt-2 mt-1 mb-4 ml-1" data-toc-skip>{{ site.data.label.post.relate_posts }}</h3> <h3 class="pt-2 mt-1 mb-4 ml-1" data-toc-skip>{{ site.data.label.post.relate_posts }}</h3>
<div class="card-deck mb-4"> <div class="card-deck mb-4">
{% for score_item in score_list %} {% for entry in index_list %}
{% assign data = score_item | split: ":" %} {% assign index = entry | plus: 0 %}
{% assign index = data[1] | plus: 0 %}
{% assign post = site.posts[index] %} {% assign post = site.posts[index] %}
<div class="card"> <div class="card">
<a href="{{ post.url | relative_url }}"> <a href="{{ post.url | relative_url }}">
@ -65,11 +106,7 @@
</div> </div>
</a> </a>
</div> </div>
{% assign count = count | plus: 1 %}
{% if count >= MAX_SIZE %}
{% break %}
{% endif %}
{% endfor %} {% endfor %}
</div> <!-- .card-deck --> </div> <!-- .card-deck -->
</div> </div> <!-- #related-posts -->
{% endif %} {% endif %}