Skip to content

Commit

Permalink
Add support for tags and related posts
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmanc committed Sep 17, 2018
1 parent 145e822 commit cb71d11
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 5 deletions.
1 change: 1 addition & 0 deletions _includes/back_to_home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div style="float:right;"><a href="{{ site.github.url }}/">< Back to home</a></div>
32 changes: 32 additions & 0 deletions _includes/find_tags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% comment %}
Borrowed from https://codinfox.github.io/dev/2015/03/06/use-tags-and-categories-in-your-jekyll-based-github-pages/
{% endcomment %}

{% comment %}
=======================
The following part extracts all the tags from your posts and sort tags, so that you do not need to manually collect your tags to a place.
=======================
{% endcomment %}
{% assign rawtags = "" %}
{% for post in site.posts %}
{% assign ttags = post.tags | join:'|' | append:'|' %}
{% assign rawtags = rawtags | append:ttags %}
{% endfor %}
{% assign rawtags = rawtags | split:'|' | sort %}

{% comment %}
=======================
The following part removes dulpicated tags and invalid tags like blank tag.
=======================
{% endcomment %}
{% assign tags = "" %}
{% for tag in rawtags %}
{% if tag != "" %}
{% if tags == "" %}
{% assign tags = tag | split:'|' %}
{% endif %}
{% unless tags contains tag %}
{% assign tags = tags | join:'|' | append:'|' | append:tag | split:'|' %}
{% endunless %}
{% endif %}
{% endfor %}
53 changes: 52 additions & 1 deletion _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
<div class="post">

<header class="post-header">
<div style="float:right;"><a href="/">< Back to home</a></div>
{% include back_to_home.html %}
<h1 class="post-title">{{ page.title }}</h1>
<div>
<span class="post-meta">
{{ page.date | date: "%b %-d, %Y" }}{% if page.author %} • {{ page.author }}{% endif %}{% if page.meta %} • {{ page.meta }}{% endif %}
</span>
{% include social_share.html %}
</div>
{% assign sortedTags = page.tags | sort %}
{% for tag in sortedTags %}
<span class="tag"><a href="/tags#{{ tag | slugify }}">#{{ tag }}</a></span>
{% endfor %}
</header>

<article class="post-content">
Expand All @@ -22,3 +26,50 @@ <h1 class="post-title">{{ page.title }}</h1>
</article>

</div>

{% assign relatedPosts = 0 %}
{% for post in site.posts %}
{% if post.url != page.url %}
{% for tag in post.tags %}
{% if page.tags contains tag %}
{% assign relatedPosts = relatedPosts | plus: 1 %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% if relatedPosts > 0 %}
<div class="related">
<h3>Related Posts</h3>
{% endif %}
<div class="related-flex">
{% assign relatedPosts = "" | split: "," %}
{% for post in site.posts %}
{% if post.url != page.url %}
{% assign sameTagCount = 0 %}
{% for tag in post.tags %}
{% if page.tags contains tag %}
{% assign sameTagCount = sameTagCount | plus: 1 %}
{% endif %}
{% endfor %}
{% if sameTagCount > 0 %}
<div class="related-post">
<a href="{{ post.url }}">
<strong>{{ post.title }}</strong><br>
{{ post.date | date_to_string }}—{{ post.author }}
</a>
{% assign sortedTags = post.tags | sort %}
<ul>
{% for tag in sortedTags %}
<li><a href="/tags#{{ tag | slugify }}" class="related-post-tag">#{{ tag }}</a></li>
{% endfor %}
</ul>
<p>{{ post.excerpt | truncatewords: 50 }}</p>
<p><a class="btn btn-sm btn-primary" href="{{ site.github.url }}{{ post.url }}" role="button">Read more… <i class="fa fa-arrow-circle-right"></i></a>
</p>
</div>
{% endif %}
{% endif %}
{% endfor %}
</div>
</div>
32 changes: 32 additions & 0 deletions _layouts/tags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
layout: default
---

<div class="post">
{% include back_to_home.html %}
<h1 class="page-heading">Posts by Tag</h1>

<ul class="post-list">

{% include find_tags.html %}

{% for tag in tags %}
<h2 id="{{ tag | slugify }}">{{ tag }}</h2>
<ul>
{% for post in site.posts %}
{% if post.tags contains tag %}
<li>
<h3>
<a href="{{ post.url }}">
{{ post.title }}
<br><small>{{ post.date | date_to_string }}—{{ post.author }}</small>
</a>
</h3>
</li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</ul>

</div>
8 changes: 4 additions & 4 deletions _sass/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,17 @@ pre {
* Wrapper
*/
.wrapper {
max-width: -webkit-calc(980px - (#{$spacing-unit}*8));
max-width: calc(980px - (#{$spacing-unit}*8));
max-width: -webkit-calc(#{$page-width} - (#{$spacing-unit}*8));
max-width: calc(#{$page-width} - (#{$spacing-unit}*8));
margin-right: auto;
margin-left: auto;
padding-right: $spacing-unit*4;
padding-left: $spacing-unit*4;
@extend %clearfix;

@include media-query($on-laptop) {
max-width: -webkit-calc(980px - (#{$spacing-unit*4}));
max-width: calc(980px - (#{$spacing-unit*4}));
max-width: -webkit-calc(#{$page-width} - (#{$spacing-unit*4}));
max-width: calc(#{$page-width} - (#{$spacing-unit*4}));
padding-right: $spacing-unit*2;
padding-left: $spacing-unit*2;
}
Expand Down
70 changes: 70 additions & 0 deletions _sass/_related.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
.related {
max-width: -webkit-calc(980px - (#{$spacing-unit}*7));
max-width: calc(980px - (#{$spacing-unit}*7));
h3 {
margin: #{$spacing-unit} 0 #{$spacing-unit} calc(#{$spacing-unit}*4);
}
}

.related-flex {
margin: 0 calc(-#{$spacing-unit} / 2) 0 calc(-#{$spacing-unit} / 2);
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.related-post {
background: $at-white;
flex-basis: -webkit-calc(((#{$page-width} - #{$spacing-unit}*9)/2) - 8*#{$spacing-unit});
flex-basis: calc(((#{$page-width} - #{$spacing-unit}*9)/2) - 8*#{$spacing-unit});
flex-grow: 1;
display: inline-block;

margin: -webkit-calc(#{$spacing-unit} / 2);
margin: calc(#{$spacing-unit} / 2);

padding: -webkit-calc(#{$spacing-unit}*4) -webkit-calc(#{$spacing-unit}*4) -webkit-calc(#{$spacing-unit}*2) -webkit-calc(#{$spacing-unit}*4);
padding: calc(#{$spacing-unit}*4) calc(#{$spacing-unit}*4) calc(#{$spacing-unit}*2) calc(#{$spacing-unit}*4);
ul {
margin: 0;
li {
display: inline;
}
}
}
.tag {
margin-right: #{$spacing-unit};
}

/*
.related {
max-width: -webkit-calc(980px - (#{$spacing-unit}*7));
max-width: calc(980px - (#{$spacing-unit}*7));
margin-top: #{$spacing-unit};
}
.related-flex {
margin: calc(-#{$spacing-unit} / 2);
}
.related-post {
background: $at-white;
max-width: -webkit-calc(((#{$page-width} - #{$spacing-unit}*9)/2) - 2*#{$spacing-unit});
max-width: calc(((#{$page-width} - #{$spacing-unit}*9)/2) - 2*#{$spacing-unit});
margin: -webkit-calc(#{$spacing-unit} / 2);
margin: calc(#{$spacing-unit} / 2);
padding: #{$spacing-unit};
float: left;
ul {
margin: 0;
li {
display: inline;
}
}
}
.tag {
margin-right: #{$spacing-unit};
}
*/
2 changes: 2 additions & 0 deletions _sass/autotrader-blog-jekyll-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ $base-font-size: 16px;
$small-font-size: $base-font-size * 0.875;
$base-line-height: 1.5;

$page-width: 980px;
$spacing-unit: 8px;

$text-color: #666666;
Expand Down Expand Up @@ -74,6 +75,7 @@ $on-laptop: 800px;
"layout",
"syntax-highlighting",
"header",
"related",
"table",
"social_share",
"callout"
Expand Down
3 changes: 3 additions & 0 deletions tags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
layout: tags
---

0 comments on commit cb71d11

Please sign in to comment.