Skip to content

Commit 86dff5d

Browse files
committed
Add related posts component
1 parent 3b95d24 commit 86dff5d

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

_includes/related-posts.html

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{% assign maxRelated = 4 %} <!-- Maximum number of related posts to output -->
2+
{% assign minCommonTags = 1 %} <!-- Minimum number of common tags that have to match -->
3+
{% assign maxRelatedCounter = 0 %}
4+
5+
{% capture related_posts %}
6+
{% for post in site.posts %}
7+
{% assign sameTagCount = 0 %}
8+
{% assign commonTags = '' %}
9+
10+
{% for tag in post.tags %}
11+
{% if post.url != page.url %}
12+
{% if page.tags contains tag %}
13+
{% assign sameTagCount = sameTagCount | plus: 1 %}
14+
{% assign commonTags = commonTags %}
15+
{% endif %}
16+
{% endif %}
17+
{% endfor %}
18+
19+
{% if sameTagCount >= minCommonTags %}
20+
21+
<!-- Post item template -->
22+
{% include post-list-item.html %}
23+
{% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}
24+
25+
{% if maxRelatedCounter >= maxRelated %}
26+
{% break %}
27+
{% endif %}
28+
29+
{% endif %}
30+
{% endfor %}
31+
{% endcapture %}
32+
33+
<!-- Render component if there are one or more related posts -->
34+
{% if maxRelatedCounter >= 1 %}
35+
<div class="related-posts bg-slate-100">
36+
<div class="related-posts__wrapper section-container">
37+
<h1 class="related-posts__title">Keep<br>Reading</h1>
38+
39+
<!-- Show related posts -->
40+
<ul class="post-list">
41+
{{ related_posts }}
42+
</ul>
43+
44+
<!-- Decorative arrows -->
45+
<svg class="related-posts__arrow svg-icon"><use xlink:href="{{ '/assets/images/icons/icon-sprite.svg#arrow-right' | relative_url }}"></use></svg>
46+
<svg class="related-posts__arrow svg-icon"><use xlink:href="{{ '/assets/images/icons/icon-sprite.svg#arrow-right' | relative_url }}"></use></svg>
47+
<svg class="related-posts__arrow svg-icon"><use xlink:href="{{ '/assets/images/icons/icon-sprite.svg#arrow-right' | relative_url }}"></use></svg>
48+
</div>
49+
</div>
50+
{% endif %}

_layouts/post.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222

2323
<!-- Related jobs -->
2424
{% include related-jobs.html %}
25+
2526
</div>
2627
</div>
2728

29+
<!-- Related posts -->
30+
{% include related-posts.html %}
31+
2832
</article>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
////////////////////
2+
// Related Posts
3+
////////////////////
4+
5+
.related-posts {
6+
position: relative;
7+
overflow: hidden;
8+
z-index: 1;
9+
}
10+
11+
.related-posts__wrapper {
12+
@media (min-width: $post-bp) {
13+
display: grid;
14+
grid-template-columns: 320px 1fr 5%;
15+
}
16+
}
17+
18+
.related-posts__title {
19+
margin-bottom: 0;
20+
line-height: 1;
21+
@extend .monospace;
22+
23+
@media (min-width: $bp-md) {
24+
font-size: rem-calc(40px);
25+
}
26+
27+
@media (min-width: $post-bp) {
28+
font-size: rem-calc(56px);
29+
}
30+
}
31+
32+
////////////////////
33+
// Post List Tweaks
34+
////////////////////
35+
36+
.related-posts .post-list {
37+
@media (min-width: $bp-md) {
38+
display: grid;
39+
grid-gap: 0 5%;
40+
grid-template-columns: 1fr 1fr;
41+
align-self: center;
42+
}
43+
}
44+
45+
.related-posts .post-list__item {
46+
padding-right: 0;
47+
48+
@media (max-width: $bp-md) {
49+
&:first-of-type {
50+
margin-top: rem-calc(15px);
51+
border-top: 1px solid $border-color;
52+
}
53+
54+
&:last-of-type {
55+
padding-bottom: 0;
56+
border-bottom: none;
57+
}
58+
}
59+
60+
@media (min-width: $bp-md) {
61+
&:nth-of-type(3),
62+
&:nth-of-type(4) {
63+
border-bottom: none;
64+
}
65+
}
66+
}
67+
68+
////////////////////
69+
// Arrows
70+
////////////////////
71+
72+
.related-posts__arrow {
73+
position: absolute;
74+
color: $slate-200;
75+
z-index: -1;
76+
@extend .hidden-md-down;
77+
78+
&:nth-of-type(1) {
79+
top: -.35em;
80+
left: .5em;
81+
font-size: 30vmin;
82+
}
83+
84+
&:nth-of-type(2) {
85+
bottom: -.35em;
86+
left: -.35em;
87+
font-size: 34vmin;;
88+
}
89+
90+
&:nth-of-type(3) {
91+
bottom: 0;
92+
right: -.25em;
93+
font-size: 24vmin;;
94+
}
95+
}

assets/css/main.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
@import "component/post-content";
4545
@import "component/featured-post-hero";
4646
@import "component/pagination";
47+
@import "component/related-posts";
4748

4849
// Import the syntax highlighting theme, see:
4950
// https://richleland.github.io/pygments-css/

0 commit comments

Comments
 (0)