Skip to content

Commit

Permalink
feat(example): auto page for authors with no posts
Browse files Browse the repository at this point in the history
  • Loading branch information
gouravkhunger committed Oct 31, 2024
1 parent 158488e commit 51666fc
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 11 deletions.
13 changes: 11 additions & 2 deletions example/_data/authors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ janedoe:
test1:
exclude: true # testing the exclude attribute
name: "test1"
bio: "Test is a demo author 1"
bio: "This is demo author 1"
email: "[email protected]"
socials:
github: "test1"
Expand All @@ -26,8 +26,17 @@ test1:
# test2 is excluded via exclude attribute for authors autopage config in _config.yml
test2:
name: "test2"
bio: "Test is a demo author 2"
bio: "This is demo author 2"
email: "[email protected]"
socials:
github: "test2"
twitter: "test2"

# test3 is not excluded so will get rendered but with a "No posts yet." message
test3:
name: "test3"
bio: "This is demo author 3"
email: "[email protected]"
socials:
github: "test3"
twitter: "test3"
12 changes: 8 additions & 4 deletions example/_layouts/author.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ <h1>{{ author.name }}</h1>
<p>{{ author.bio }}</p>

{% assign links = author.socials %}
<a href="{{ link.twitter }}">Twitter</a>
<a href="{{ link.github }}">GitHub</a>
<a href="https://x.com/{{ links.twitter }}" target="_blank">Twitter</a>
<a href="https://github.com/{{ links.github }}" target="_blank">GitHub</a>

<br /><br />

<h2>{{ page.title }}</h2>

{%- assign date_format = site.minima.date_format | default: "%b %-d, %Y" -%}

{% assign arrSize = paginator.posts | size %}
{% if arrSize > 0 %}
<h2>{{ page.title }}</h2>
{% for post in paginator.posts %}
<ul class="post-list">
<li>
Expand All @@ -32,6 +33,9 @@ <h3>
</li>
</ul>
{% endfor %}
{% else %}
<h2>No posts yet.</h2>
{% endif %}

{% if paginator.total_pages > 1 %}
<div class="pager">
Expand Down
2 changes: 1 addition & 1 deletion example/_posts/janedoe/2023-03-31-jane-eight.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: post
title: "Jane Doe #8"
date: 2023-03-30 09:07:12 +0530
date: 2023-03-30 10:07:12 +0530
author: janedoe
---

Expand Down
2 changes: 1 addition & 1 deletion example/_posts/janedoe/2023-03-31-jane-four.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: post
title: "Jane Doe #4"
date: 2023-03-28 09:07:12 +0530
date: 2023-03-28 10:07:12 +0530
author: janedoe
---

Expand Down
2 changes: 1 addition & 1 deletion example/_posts/janedoe/2023-03-31-jane-six.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: post
title: "Jane Doe #6"
date: 2023-03-29 09:07:12 +0530
date: 2023-03-29 10:07:12 +0530
author: janedoe
---

Expand Down
2 changes: 1 addition & 1 deletion example/_posts/janedoe/2023-03-31-jane-two.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: post
title: "Jane Doe #2"
date: 2023-03-27 09:07:12 +0530
date: 2023-03-27 10:07:12 +0530
author: janedoe
---

Expand Down
2 changes: 1 addition & 1 deletion example/_posts/johndoe/2023-03-31-john-six.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: post
title: "John Doe #6"
date: 2023-03-26 09:07:12 +0530
date: 2023-03-27 09:07:12 +0530
author: johndoe
---

Expand Down
7 changes: 7 additions & 0 deletions example/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
layout: home
---

[![Gem Version](https://img.shields.io/gem/v/jekyll-auto-authors)][ruby-gems]
[![Gem Total Downloads](https://img.shields.io/gem/dt/jekyll-auto-authors)][ruby-gems]

[ruby-gems]: https://rubygems.org/gems/jekyll-auto-authors

This example jekyll site has {{ site.data.authors | size }} authors:
{% for author in site.data.authors %}<a href="/author/{{author[0] }}">{{ author[1].name }}</a>{% if forloop.last != true %}, {% endif %}{% endfor %}.
{% for author in site.data.authors %}
{% assign posts = site.posts | where_exp:"item", "item.author == author[0]" | size %}
{{ author[1].name }} has {{ posts }} posts.
{% endfor %}

test1 & test2 have been excluded for automatic author page generation while test3's page is auto generated without having any posts on their name yet. This can be verified by clicking the link on their names above.

The pagination setting in [`_config.yml`](https://github.com/gouravkhunger/jekyll-auto-authors/tree/main/example/_config.yml) is set to 5 posts per page. Hence, the author pages show their respective posts and when the number exceeds the per page limit, a pagination trail is shown for navigation.

This is done using the [`jekyll-auto-authors`](https://github.com/gouravkhunger/jekyll-auto-authors) plugin. Please read the [setup article](http://genicsblog.com/gouravkhunger/adding-multiple-authors-to-a-jekyll-blog-got-easier#2-using-my-plugin-jekyll-auto-authors) to learn more.
Expand Down

0 comments on commit 51666fc

Please sign in to comment.