2019-10-21 17:12:36 +03:00
|
|
|
<!--
|
2023-05-06 02:22:29 +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.
|
2019-10-21 17:12:36 +03:00
|
|
|
-->
|
|
|
|
|
2023-05-06 02:22:29 +03:00
|
|
|
<!-- The total size of related posts -->
|
2020-06-04 17:01:59 +03:00
|
|
|
{% assign TOTAL_SIZE = 3 %}
|
|
|
|
|
2023-05-06 02:22:29 +03:00
|
|
|
<!-- 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
|
|
|
|
2023-05-06 02:22:29 +03:00
|
|
|
<!-- Equals to TAG_SCORE / {max_categories_hierarchy} -->
|
2019-10-21 17:12:36 +03:00
|
|
|
{% assign CATEGORY_SCORE = 0.5 %}
|
|
|
|
|
2023-05-06 02:22:29 +03:00
|
|
|
{% assign SEPARATOR = ':' %}
|
2020-06-04 17:01:59 +03:00
|
|
|
|
2023-05-06 02:22:29 +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 %}
|
|
|
|
{% endfor %}
|
|
|
|
|
2023-05-06 02:22:29 +03:00
|
|
|
{% assign index_list = '' | split: '' %}
|
2020-06-04 17:01:59 +03:00
|
|
|
|
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 %}
|
|
|
|
|
2023-05-06 02:22:29 +03:00
|
|
|
<!-- Fill with the other newlest posts -->
|
2020-06-04 17:01:59 +03:00
|
|
|
{% assign less = TOTAL_SIZE | minus: index_list.size %}
|
|
|
|
|
|
|
|
{% if less > 0 %}
|
|
|
|
{% for i in (0..last_index) %}
|
|
|
|
{% assign post = site.posts[i] %}
|
2023-05-06 02:22:29 +03:00
|
|
|
{% if post.url != page.url %}
|
2020-06-04 17:01:59 +03:00
|
|
|
{% 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 %}
|
2022-08-08 17:03:25 +03:00
|
|
|
<div id="related-posts" class="mb-2 mb-sm-4">
|
2023-04-10 05:54:27 +03:00
|
|
|
<h3 class="pt-2 mb-4 ms-1" data-toc-skip>
|
2023-05-06 02:22:29 +03:00
|
|
|
{{ site.data.locales[include.lang].post.relate_posts }}
|
|
|
|
</h3>
|
2023-04-10 05:54:27 +03:00
|
|
|
<div class="row row-cols-1 row-cols-md-2 row-cols-xl-3 g-4 mb-4">
|
2023-05-06 02:22:29 +03:00
|
|
|
{% for entry in index_list %}
|
|
|
|
{% assign index = entry | plus: 0 %}
|
|
|
|
{% assign post = site.posts[index] %}
|
2023-04-10 05:54:27 +03:00
|
|
|
<div class="col">
|
|
|
|
<a href="{{ post.url | relative_url }}" class="card post-preview h-100">
|
|
|
|
<div class="card-body">
|
|
|
|
{% include datetime.html date=post.date class="small" lang=include.lang %}
|
2023-05-13 04:03:13 +03:00
|
|
|
<h4 class="pt-0 my-2" data-toc-skip>{{ post.title }}</h4>
|
2023-04-10 05:54:27 +03:00
|
|
|
<div class="text-muted small">
|
|
|
|
<p>
|
|
|
|
{% include no-linenos.html content=post.content %}
|
|
|
|
{{ content | markdownify | strip_html | truncate: 200 | escape }}
|
|
|
|
</p>
|
|
|
|
</div>
|
2019-10-21 17:12:36 +03:00
|
|
|
</div>
|
2023-04-10 05:54:27 +03:00
|
|
|
</a>
|
|
|
|
</div>
|
2023-05-06 02:22:29 +03:00
|
|
|
{% endfor %}
|
|
|
|
</div>
|
|
|
|
<!-- .card-deck -->
|
|
|
|
</div>
|
|
|
|
<!-- #related-posts -->
|
2020-06-04 17:01:59 +03:00
|
|
|
{% endif %}
|