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

fix: resolve issue with pagination URLs #8

Merged
merged 5 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
2. <!-- Second step -->
3. <!-- and so on... -->

**Expected behavioor:** <!-- What should happen -->
**Expected behaviour:** <!-- What should happen -->

## Additional information

Expand Down
27 changes: 16 additions & 11 deletions src/_includes/layouts/posts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
{% endblock %}

{% block content %}
{% if posts.length %}
{% for item in posts %}
<article class="entry">
<h2><a href="{{ item.url }}">{{ item.data.title }}</a></h2>
<p class="metadata"><time datetime="{{ item.data.date | isoDate }}" class="dt-published">{{ item.data.date | formatDate }}</time></p>
{% if item.data.excerpt %}
<p>{{ item.data.excerpt | safe }}</p>
{% endif %}
</article>
{% endfor %}
{% endif %}

{# In circumstances where pagination size is set to 1, the paginated content will be a single item instead of an array.
The following code ensures we are always handling an array, even if it only has one item. #}

{% set items = [] %}
{% set items = items.concat(posts) %}

{% for item in items %}
<article class="entry">
<h2><a href="{{ item.url }}">{{ item.data.title }}</a></h2>
<p class="metadata"><time datetime="{{ item.data.date | isoDate }}" class="dt-published">{{ item.data.date | formatDate }}</time></p>
{% if item.data.excerpt %}
<p>{{ item.data.excerpt | safe }}</p>
{% endif %}
</article>
{% endfor %}

{% if pagination.href.previous or pagination.href.next %}
{% include "partials/components/pagination.njk" %}
Expand Down
6 changes: 3 additions & 3 deletions src/_includes/partials/components/pagination.njk
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<nav class="pagination" aria-label="posts pagination">
<ul>
{%- if pagination.href.previous %}
<li><a href="{{ pagination.href.previous }}">&larr;<span class="visually-hidden">previous</span></a></li>
<li><a href="{{ pagination.href.previous }}"><span aria-hidden="true">&larr;</span><span class="visually-hidden">previous</span></a></li>
{%- endif %}
{%- for pageEntry in pagination.pages %}
<li><a href="{{ pagination.hrefs[ loop.index0 ] }}"{% if page.url == pagination.hrefs[ loop.index0 ] %} aria-current="page"{% endif %}>{{ loop.index }}</a></li>
<li><a href="{{ pagination.hrefs[ loop.index0 ] }}"{% if page.url == pagination.hrefs[ loop.index0 ] %} aria-current="page"{% endif %} aria-label="Page {{ loop.index }} of {{ pagination.pages.length }}">{{ loop.index }}</a></li>
{%- endfor %}
{%- if pagination.href.next %}
<li><a href="{{ pagination.href.next }}"><span class="visually-hidden">next</span>&rarr;</a></li>
<li><a href="{{ pagination.href.next }}"><span class="visually-hidden">next</span><span aria-hidden="true">&rarr;</span></a></li>
{%- endif %}
</ul>
</nav>
1 change: 1 addition & 0 deletions src/collections/pages/posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pagination:
data: collections.posts
size: 10
alias: posts
permalink: "/posts/{% if pagination.pageNumber > 0 %}page/{{ pagination.pageNumber + 1 }}/{% endif %}"
---