Skip to content

Commit 6b65b1f

Browse files
committed
Add Related-Posts in the bottom of each posts.
1 parent dc88935 commit 6b65b1f

4 files changed

Lines changed: 137 additions & 18 deletions

File tree

_includes/related-posts.html

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 %}

_layouts/post.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ <h1 data-toc-skip>{{ page.title }}</h1>
5252
<div class="post-tail text-muted">
5353
<!-- Tags -->
5454
{% if page.tags.size > 0 %}
55-
<div class="mb-2">
55+
<div class="mb-4">
5656
{% for tag in page.tags %}
5757
<a href="{{ site.baseurl }}/tags/{{ tag | replace: ' ', '-' | downcase }}/"
5858
class="post-tag no-text-decoration" >
@@ -61,11 +61,12 @@ <h1 data-toc-skip>{{ page.title }}</h1>
6161
{% endfor %}
6262
</div>
6363
{% endif %}
64-
6564
</div><!-- endof .post-tail -->
6665

6766
</div> <!-- .post -->
6867

68+
{% include related-posts.html %}
69+
6970
<div class="post-pager d-flex justify-content-between">
7071
{% if page.previous.url %}
7172
<a href="{{ site.baseurl }}{{page.previous.url}}" class="btn btn-outline-primary">

assets/css/_src/main.scss

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -769,15 +769,15 @@ table tbody td {
769769
}
770770

771771
.post-tail {
772-
margin-top: 4rem;
773-
border-bottom: 2px solid #f2f2f2;
772+
margin-top: 3.5rem;
773+
border-bottom: 1px double #e9ecef;
774774
font-size: 0.85rem;
775775
}
776776

777777
.post-tag {
778778
background: rgba(0, 0, 0, 0.075);
779779
border-radius: .34rem;
780-
padding: .15rem .4rem;
780+
padding: .25rem .4rem;
781781
margin: 0 .1rem;
782782
color: #818182;
783783
line-height: 1.6rem;
@@ -852,19 +852,66 @@ table tbody td {
852852
padding-top: 0;
853853
}
854854

855-
/* sharing */
856-
#sharing a>i{
857-
font-size: 1.2em;
855+
856+
/*--- Related Posts ---*/
857+
858+
#related-posts {
859+
border-bottom: 1px double #e9ecef;
860+
}
861+
862+
#related-posts>h3 {
863+
color: gray;
864+
font-size: 1.1rem;
865+
font-family: 'Oswald', sans-serif;
866+
}
867+
868+
#related-posts .card {
869+
border: none;
870+
-webkit-transition: all .5s ease-in-out;;
871+
-moz-transition: all .5s ease-in-out;;
872+
transition: all .5s ease-in-out;
873+
}
874+
875+
#related-posts .card h3 {
876+
color: #353a3d;
877+
}
878+
879+
#related-posts p {
880+
font-size: .9rem;
881+
margin-bottom: .5rem;
882+
overflow: hidden;
883+
text-overflow: ellipsis;
884+
display: -webkit-box;
885+
-webkit-line-clamp: 2;
886+
-webkit-box-orient: vertical;
887+
}
888+
889+
#related-posts a:hover {
890+
text-decoration: none;
858891
}
859892

860-
#sharing .fab.fa-facebook-square {
861-
color: #3b549f;
893+
#related-posts .card:hover {
894+
transform: scale(1.05);
862895
}
863896

864-
#sharing .fab.fa-google-plus-g {
865-
color: #d42a2a;
897+
#related-posts ul {
898+
list-style-type: none;
899+
padding-inline-start: 1.5rem;
866900
}
867901

902+
#related-posts ul > li::before {
903+
background: #c2c9d4;
904+
width: 5px;
905+
height: 5px;
906+
border-radius: 1px;
907+
display: block;
908+
content: "";
909+
position: relative;
910+
top: 1rem;
911+
right: 1rem;
912+
}
913+
914+
868915
/*--- Tab Categories ---*/
869916

870917
.categories {
@@ -1085,11 +1132,11 @@ a.tag:hover {
10851132
padding: .6rem 0;
10861133
}
10871134

1088-
#page-category ul>li::before,
1089-
#page-tag ul>li::before {
1135+
#page-category ul > li::before,
1136+
#page-tag ul > li::before {
10901137
background: #999;
1091-
width: 6px;
1092-
height: 6px;
1138+
width: 5px;
1139+
height: 5px;
10931140
border-radius: 50%;
10941141
display: block;
10951142
content: "";

tabs/tags.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ title: Tags
66
# MIT License
77
---
88

9-
{%comment%}
9+
{% comment %}
1010
'site.tags' looks like a Map, e.g. site.tags.MyTag.[ Post0, Post1, ... ]
1111
Print the {{ site.tags }} will help you to understand it.
12-
{%endcomment%}
12+
{% endcomment %}
1313
<div id="tags" class="d-flex flex-wrap">
1414
{% assign tags = "" | split: "" %}
1515
{% for t in site.tags %}

0 commit comments

Comments
 (0)