1+ <!--
2+ The related posts of current post.
3+ Placed in the bottom of every single post.
4+ © 2019 Cotes Chung
5+ Published under the MIT License
6+ -->
7+
8+ {% assign MAX_SIZE = 3 %}
9+ {% assign TAG_SCORE = 1 %}
10+ {% assign CATEGORY_SCORE = 0.5 %}
11+
12+ {% assign score_list = "" | split: "" %}
13+ {% assign post_index = 0 %}
14+
15+ {% for post in site.posts %}
16+ {% if post.url != page.url %}
17+ {% assign score = 0 %}
18+
19+ {% for tag in post.tags %}
20+ {% if page.tags contains tag %}
21+ {% assign score = score | plus: TAG_SCORE %}
22+ {% endif %}
23+ {% endfor %}
24+
25+ {% for category in post.categories %}
26+ {% if page.categories contains category %}
27+ {% assign score = score | plus: CATEGORY_SCORE %}
28+ {% endif %}
29+ {% endfor %}
30+
31+ {% if score > 0 %}
32+ {% capture score_item %}{{ score }}:{{ post_index }}{% endcapture %}
33+ {% assign score_list = score_list | push: score_item %}
34+ {% endif %}
35+
36+ {% endif %}
37+ {% assign post_index = post_index | plus: 1 %}
38+ {% endfor %}
39+
40+ {% unless score_list.size == 0 %}
41+ {% assign score_list = score_list | sort | reverse %}
42+ {% assign count = 0 %}
43+ < div id ="related-posts " class ="mt-4 mb-4 pb-3 ">
44+ < h3 class ="pt-2 mt-1 mb-4 " data-toc-skip > Related Posts</ h3 >
45+ < div class ="card-deck mb-4 ">
46+ {% for score_item in score_list %}
47+ {% assign data = score_item | split: ":" %}
48+ {% assign index = data[1] | plus: 0 %}
49+ {% assign post = site.posts[index] %}
50+ < div class ="card btn-box-shadow ">
51+ < a href ="{{ post.url }} ">
52+ < div class ="card-body ">
53+ < span class ="timeago text-muted small ">
54+ {{ post.date | date: POST_DATE }}
55+ < i class ="hidden "> {{ page.date | date_to_xmlschema }}</ i >
56+ </ span >
57+ < h3 class ="pt-0 mt-2 mb-3 " data-toc-skip > {{ post.title }}</ h3 >
58+ < div class ="text-muted small ">
59+ < p > {{ post.content | markdownify | strip_html | truncate: 100 }}</ p >
60+ </ div >
61+ </ div >
62+ </ a >
63+ </ div >
64+ {% assign count = count | plus: 1 %}
65+ {% if count > = MAX_SIZE %}
66+ {% break %}
67+ {% endif %}
68+ {% endfor %}
69+ </ div > <!-- .card-deck -->
70+ </ div >
71+ {% endunless %}
0 commit comments