2019-10-21 17:12:36 +03:00
|
|
|
<!--
|
2020-06-04 17:01:59 +03:00
|
|
|
Recommend the other 3 posts according to the tags and categories of the current post,
|
|
|
|
if the number is not enough, use the other latest posts to supplement.
|
|
|
|
|
2020-01-02 16:17:49 +03:00
|
|
|
v2.0
|
|
|
|
https://github.com/cotes2020/jekyll-theme-chirpy
|
2019-10-21 17:12:36 +03:00
|
|
|
© 2019 Cotes Chung
|
|
|
|
Published under the MIT License
|
|
|
|
-->
|
|
|
|
|
2020-06-04 17:01:59 +03:00
|
|
|
<!-- The total size of related posts -->
|
|
|
|
{% assign TOTAL_SIZE = 3 %}
|
|
|
|
|
|
|
|
<!-- An random integer that bigger than 0 -->
|
2019-10-21 17:12:36 +03:00
|
|
|
{% assign TAG_SCORE = 1 %}
|
2020-06-04 17:01:59 +03:00
|
|
|
|
|
|
|
<!-- Equals to TAG_SCORE / {max_categories_hierarchy} -->
|
2019-10-21 17:12:36 +03:00
|
|
|
{% assign CATEGORY_SCORE = 0.5 %}
|
|
|
|
|
2020-06-04 17:01:59 +03:00
|
|
|
{% assign SEPARATOR = ":" %}
|
|
|
|
|
2019-10-21 17:12:36 +03:00
|
|
|
{% assign score_list = "" | split: "" %}
|
2020-06-04 17:01:59 +03:00
|
|
|
{% assign last_index = site.posts.size | minus: 1 %}
|
2019-10-21 17:12:36 +03:00
|
|
|
|
2020-06-04 17:01:59 +03:00
|
|
|
{% for i in (0..last_index) %}
|
|
|
|
{% assign post = site.posts[i] %}
|
2019-10-21 17:12:36 +03:00
|
|
|
|
2020-06-04 17:01:59 +03:00
|
|
|
{% if post.url == page.url %}
|
|
|
|
{% continue %}
|
|
|
|
{% endif %}
|
2019-10-21 17:12:36 +03:00
|
|
|
|
2020-06-04 17:01:59 +03:00
|
|
|
{% assign score = 0 %}
|
2019-10-21 17:12:36 +03:00
|
|
|
|
2020-06-04 17:01:59 +03:00
|
|
|
{% for tag in post.tags %}
|
|
|
|
{% if page.tags contains tag %}
|
|
|
|
{% assign score = score | plus: TAG_SCORE %}
|
2019-10-21 17:12:36 +03:00
|
|
|
{% endif %}
|
2020-06-04 17:01:59 +03:00
|
|
|
{% endfor %}
|
2019-10-21 17:12:36 +03:00
|
|
|
|
2020-06-04 17:01:59 +03:00
|
|
|
{% 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 %}
|
2019-10-21 17:12:36 +03:00
|
|
|
{% endif %}
|
2020-06-04 17:01:59 +03:00
|
|
|
|
2019-10-21 17:12:36 +03:00
|
|
|
{% endfor %}
|
|
|
|
|
2020-06-04 17:01:59 +03:00
|
|
|
|
|
|
|
{% assign index_list = "" | split: "" %}
|
|
|
|
|
2019-10-26 08:21:58 +03:00
|
|
|
{% if score_list.size > 0 %}
|
2019-10-21 17:12:36 +03:00
|
|
|
{% assign score_list = score_list | sort | reverse %}
|
2020-06-04 17:01:59 +03:00
|
|
|
{% 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 %}
|
2020-07-29 23:58:05 +03:00
|
|
|
<div id="related-posts" class="mt-5 mb-2 mb-sm-4">
|
2020-02-16 22:01:48 +03:00
|
|
|
<h3 class="pt-2 mt-1 mb-4 ml-1" data-toc-skip>{{ site.data.label.post.relate_posts }}</h3>
|
2019-10-21 17:12:36 +03:00
|
|
|
<div class="card-deck mb-4">
|
2020-06-04 17:01:59 +03:00
|
|
|
{% for entry in index_list %}
|
|
|
|
{% assign index = entry | plus: 0 %}
|
2019-10-21 17:12:36 +03:00
|
|
|
{% assign post = site.posts[index] %}
|
2019-10-26 08:21:58 +03:00
|
|
|
<div class="card">
|
2020-02-02 22:26:20 +03:00
|
|
|
<a href="{{ post.url | relative_url }}">
|
2019-10-21 17:12:36 +03:00
|
|
|
<div class="card-body">
|
2020-07-23 17:23:31 +03:00
|
|
|
{% include timeago.html date=post.date class="small" %}
|
2020-01-02 15:58:00 +03:00
|
|
|
<h3 class="pt-0 mt-1 mb-3" data-toc-skip>{{ post.title }}</h3>
|
2019-10-21 17:12:36 +03:00
|
|
|
<div class="text-muted small">
|
2020-07-22 16:41:49 +03:00
|
|
|
<p>
|
|
|
|
{% include no-linenos.html content=post.content %}
|
|
|
|
{{ content | markdownify | strip_html | truncate: 200 }}
|
|
|
|
</p>
|
2019-10-21 17:12:36 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
{% endfor %}
|
2019-10-26 08:21:58 +03:00
|
|
|
</div> <!-- .card-deck -->
|
2020-06-04 17:01:59 +03:00
|
|
|
</div> <!-- #related-posts -->
|
|
|
|
{% endif %}
|