Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added related posts section #1168

Merged
merged 8 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added config variables for related posts
Signed-off-by: George Araújo <[email protected]>
  • Loading branch information
george-gca committed Feb 14, 2023
commit 56b7d20be21503b55cb884b20e67844fdbc96506
5 changes: 5 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ permalink: /blog/:year/:title/
pagination:
enabled: true

related_blog_posts:
enabled: true
max_related: 5
min_common_tags: 1

# Giscus comments (RECOMMENDED)
# Follow instructions on https://giscus.app/ to setup for your repo to fill out
# the information below.
Expand Down
22 changes: 10 additions & 12 deletions _includes/related_posts.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
{% assign maxRelated = 5 %}
{% assign minCommonTags = 1 %}
{% assign maxRelatedCounter = 0 %}
{% assign haveRelatedPosts = false %}
{% assign max_related_counter = 0 %}
{% assign have_related_posts = false %}

{% for post in site.posts %}
{% assign sameTagCount = 0 %}
{% assign same_tag_count = 0 %}

{% for tag in post.tags %}
{% if post.url != page.url %}
{% if page.tags contains tag %}
{% assign sameTagCount = sameTagCount | plus: 1 %}
{% assign same_tag_count = same_tag_count | plus: 1 %}
{% endif %}
{% endif %}
{% endfor %}

{% if sameTagCount >= minCommonTags %}
{% unless haveRelatedPosts %}
{% assign haveRelatedPosts = true %}
{% if same_tag_count >= site.related_blog_posts.min_common_tags %}
{% unless have_related_posts %}
{% assign have_related_posts = true %}

<br>
<hr>
Expand All @@ -32,15 +30,15 @@ <h2 class="text-3xl font-semibold mb-4 mt-12">Enjoy Reading This Article?</h2>
<a class="text-pink-700 underline font-semibold hover:text-pink-800" href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a>
</li>

{% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}
{% assign max_related_counter = max_related_counter | plus: 1 %}

{% if maxRelatedCounter >= maxRelated %}
{% if max_related_counter >= site.related_blog_posts.max_related %}
{% break %}
{% endif %}

{% endif %}
{% endfor %}

{% if maxRelatedCounter > 0 %}
{% if max_related_counter > 0 %}
</ul>
{% endif %}
6 changes: 4 additions & 2 deletions _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ <h1 class="post-title">{{ page.title }}</h1>
{{ content }}
</article>

{%- if page.related_posts == null or page.related_posts -%}
{% include related_posts.html %}
{%- if site.related_blog_posts.enabled -%}
{%- if page.related_posts == null or page.related_posts -%}
{% include related_posts.html %}
{%- endif %}
{%- endif %}

{%- if site.disqus_shortname and page.disqus_comments -%}
Expand Down