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

Add proper support for sverrirs' Jekyll Paginate V2 plugin (excl. AutoPages) #2636

Merged
merged 9 commits into from
May 4, 2024

Conversation

iBug
Copy link
Collaborator

@iBug iBug commented Aug 1, 2020

Add full support for the Jekyll Paginate V2 plugin.

The original paginator.html has been renamed to paginator-v1.html, and a new "switcher" is placed with that name. The switcher detects whether the V1 template or the V2 template should be used, by first looking for site.paginate (V1 and V2 in compatible mode), and then site.pagination.enabled (true V2).

The new V2 template fully utilizes extra attributes available for the paginator object. There's no site.xxx accessed except for UI text (l10n) which was left intact. The starting, ending, first page (and the ellipsis after), last page (and the ellipsis before), previous page, next page are all implemented using the V2 paginator attributes, making it super compatible to user configuration.

There's a special note for the page trail. In my local testing, JPV2 adds index.html to whatever specified in site.pagination.permalink, so I had to strip it off, or else it'll produce page2index.html for a specified value of page:num, causing 404. This is, however, error-prone, should any user choose to have index.html somewhere in the middle of their pagination URL. I chose to ignore this as it's rather an anti-pattern.

The V1 template only underwent a few small changes where paginator.previous_page_path and paginator.next_page_path were employed wherever possible. These two attributes have existed for long.

@mmistakes
Copy link
Owner

mmistakes commented Aug 4, 2020

Are there any test files you could add to /test so I can easily see the results of a jekyll-paginate index.html vs. a fully supported jekyll-paginate-v2 index.html?

Messing with this makes me leary as there's no easy way for me to verify it won't break someones site. The old Jekyll paginate plugin has its quirks, but it's reliable. In my experience v2 has even more quirks that can be hard to work thru sometimes and want to be careful how we support it. Since this theme is so popular I'm already having nightmares about the GH issues rolling in due to misconfiguring the v2 plugin...

@iBug
Copy link
Collaborator Author

iBug commented Aug 4, 2020

I don't think there's anything to add. The current code (paginator.html) detects and adapts the plugins automatically as described. All you need to change in order to preview the V2 paginator is _config.yml.

To use V2, remove paginate and paginate_path and add the following config:

pagination:
  enabled: true
  collection: 'posts'
  per_page: 5
  permalink: '/page/:num/' # Pages are index.html inside this folder (default)
  title: ':title - page :num'
  limit: 0
  sort_field: 'date'
  sort_reverse: true
  trail:
    # Just to show it works
    before: 1
    after: 3

That's what I adapted for. For those who are using V1, they should not even see any visible change.

@mmistakes
Copy link
Owner

OK. Could you amend the docs to include your preferred configs above for v2 pagination. I'll test v1 and v2 off this branch when I get a chance just to make sure everything looks good.

@iBug
Copy link
Collaborator Author

iBug commented Aug 4, 2020

I absolutely will do. I held off from updating the docs just in case you dislike this idea.

Also I just noticed that I missed two configuration keys from V2:

pagination:

  # Optional, the default file extension for generated pages (e.g html, json, xml).
  # Internally this is set to html by default
  extension: html

  # Optional, the default name of the index file for generated pages (e.g. 'index.html')
  # Without file extension
  indexpage: 'index'

I haven't tested yet, but it seems easy to break if anyone sets them to anything other than the default values. Again this is like the farthest corner case that's highly impractical to me. (Shall we try to cover it?)

iBug added a commit to iBug/iBug-source that referenced this pull request Aug 10, 2020
iBug pushed a commit to iBugOne/iBugOne.github.io that referenced this pull request Aug 10, 2020
@stale

This comment was marked as outdated.

@stale stale bot added the Status: Stale label Sep 5, 2020
@iBug

This comment was marked as outdated.

@stale stale bot removed the Status: Stale label Sep 6, 2020
@stale

This comment was marked as outdated.

iBug added a commit to iBug/iBug-source that referenced this pull request Dec 18, 2020
@stale

This comment was marked as outdated.

@iBug
Copy link
Collaborator Author

iBug commented Feb 7, 2021

@mmistakes If you find it hard to test this out on the docs site, feel free to drop by ibug.io where this very code has been employed for years (not literally, just half a year) and hasn't broken for once.

In fact, it's been running perfectly in two places:

I'll amend the documentation once I get the word that this is accepted.

Jekyll Paginate V2 tries to keep the page trail length consistent for all pages,
making the first and last few pages have a longer after/before trail
than it normally would have.
This breaks the previous calculation-based approach for determining
the presense of an ellipsis.

The new approach just checks if there are missing numbers between 1 and
the first item in the page trail, and inserts an ellipsis if there is any.
It now works perfectly for these cases.
@github-actions

This comment was marked as outdated.

@iBug

This comment was marked as outdated.

@FavorMylikes
Copy link
Contributor

As you can see at here

{% for index in (page_start..page_end) %}
{% if index == paginator.page %}
<li><a href="{{ site.paginate_path | replace: ':num', index | replace: '//', '/' | relative_url }}" class="disabled current">{{ index }}</a></li>
{% else %}
{% comment %} Distance from current page and this link {% endcomment %}
{% assign dist = paginator.page | minus: index %}
{% if dist < 0 %}
{% comment %} Distance must be a positive value {% endcomment %}
{% assign dist = 0 | minus: dist %}
{% endif %}
<li><a href="{{ site.paginate_path | replace: ':num', index | relative_url }}">{{ index }}</a></li>
{% endif %}
{% endfor %}

The property site.paginate_path is still used in paginator.

I believe that we still need add that even in V2 if I didn't miss something else.

@iBug
Copy link
Collaborator Author

iBug commented Aug 1, 2021

@FavorMylikes I didn't get your point. This PR was never merged so what's in the repo still targets only Jekyll Paginate (no V2). Could you be a bit more detailed?

@FavorMylikes
Copy link
Contributor

I found that the page url is point to 404

I have finish this by using _config.yml below

# jykell Paginate 
paginate_path: '/blog/:num/'
pagination: # jekyll-paginate-v2
  enabled: true
  debug: true  # open debug
  per_page: 5
  permalink: '/blog/:num/'
  sort_field: 'date'
  sort_reverse: true
  trail:
    before: 2
    after: 2

@iBug
Copy link
Collaborator Author

iBug commented Aug 1, 2021

@FavorMylikes

This PR was never merged so what's in the repo still targets only Jekyll Paginate (no V2).

To clarify: If you're using this theme with stock setup, you're not getting any Paginate V2 support. You'll have to incorporate what's presented in this PR into your repo by yourself. If you need an example, my website repo could give some insights.

@FavorMylikes
Copy link
Contributor

Ok. Thank you for your example, I'll try to implement a version.

@iBug
Copy link
Collaborator Author

iBug commented Aug 1, 2021

@mmistakes Any chance this PR gets another look? There seems like a non-trivial number of potential audience.

Also I've updated the docs in 3a065dc.

@iBug
Copy link
Collaborator Author

iBug commented May 29, 2022

Perhaps we should think this in another way. Nothing comes out perfect, and it's a norm to have trial-and-error to a certain extent. I promise I'll be after this feature (Paginate V2 support) since it's my implementation and contribution, as well as that I'm a Paginate V2 user myself. You got me.

Consider merging it in first and see what responses users have?

@iBug
Copy link
Collaborator Author

iBug commented Apr 22, 2024

Two years later I'm now in charge of this theme (thanks Michael!). This PR was (and still is) one of my most ambitious so it's definitely got attention.

@iBug iBug merged commit 9ac9602 into mmistakes:master May 4, 2024
1 check passed
@iBug iBug deleted the paginate-v2 branch May 4, 2024 16:05
@asarkar
Copy link

asarkar commented Sep 17, 2024

I can't even get the basic pagination working by following the instructions for paginate v2 in the document. I updated _config.yml and added a _posts/index.html. Nothing happens - the home page displays all the posts I ever created, and /posts returns 404.
The collections pagination instructions are confusing and I've opened #4982 to get it addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants