Skip to content
This repository was archived by the owner on Aug 28, 2019. It is now read-only.

Commit 790781b

Browse files
committed
Truncate pagination links with [...] to avoid long lists
1 parent dfaf869 commit 790781b

File tree

2 files changed

+67
-34
lines changed

2 files changed

+67
-34
lines changed

_includes/pagination.html

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{% if paginator.total_pages > 1 %}
2+
<div class="pagination">
3+
<ul class="inline-list">
4+
{% comment %} Link for previous page {% endcomment %}
5+
{% if paginator.previous_page %}
6+
{% if paginator.previous_page == 1 %}
7+
<li><a href="{{ site.url }}" class="btn">Previous</a></li>
8+
{% else %}
9+
<li><a href="{{ site.url }}/page{{ paginator.previous_page }}/" class="btn">Previous</a></li>
10+
{% endif %}
11+
{% endif %}
12+
13+
{% comment %} First page {% endcomment %}
14+
{% if paginator.page == 1 %}
15+
<li><strong class="current-page">1</strong></li>
16+
{% else %}
17+
<li><a href="{{ site.url }}">1</a></li>
18+
{% endif %}
19+
20+
{% assign page_start = 2 %}
21+
{% if paginator.page > 4 %}
22+
{% assign page_start = paginator.page | minus: 2 %}
23+
{% comment %} Ellipsis for truncated links {% endcomment %}
24+
<li></li>
25+
{% endif %}
26+
27+
{% assign page_end = paginator.total_pages | minus: 1 %}
28+
{% assign pages_to_end = paginator.total_pages | minus: paginator.page %}
29+
{% if pages_to_end > 4 %}
30+
{% assign page_end = paginator.page | plus: 2 %}
31+
{% endif %}
32+
33+
{% for index in (page_start..page_end) %}
34+
{% if index == paginator.page %}
35+
<li><strong class="current-page">{{index}}</strong></li>
36+
{% else %}
37+
{% comment %} Distance from current page and this link {% endcomment %}
38+
{% assign dist = paginator.page | minus: index %}
39+
{% if dist < 0 %}
40+
{% comment %} Distance must be a positive value {% endcomment %}
41+
{% assign dist = 0 | minus: dist %}
42+
{% endif %}
43+
<li><a href="{{ site.url }}/page{{ index }}/">{{index}}</a></li>
44+
{% endif %}
45+
{% endfor %}
46+
47+
{% comment %} Ellipsis for truncated links {% endcomment %}
48+
{% if pages_to_end > 3 %}
49+
<li></li>
50+
{% endif %}
51+
52+
{% if paginator.page == paginator.total_pages %}
53+
<li><strong class="current-page">{{ paginator.page }}</strong></li>
54+
{% else %}
55+
<li><a href="{{ site.url }}/page{{ paginator.total_pages }}/">{{ paginator.total_pages }}</a></li>
56+
{% endif %}
57+
58+
{% comment %}
59+
Link next page
60+
{% endcomment %}
61+
{% if paginator.next_page %}
62+
<li><a href="{{ site.url }}/page{{ paginator.next_page }}/" class="btn">Next</a></li>
63+
{% endif %}
64+
</ul>
65+
</div>
66+
{% endif %}

index.html

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,4 @@ <h1 class="entry-title"><a href="{{ site.url }}{{ post.url }}" rel="bookmark" ti
3939
</article><!-- /.hentry -->
4040
{% endfor %}
4141

42-
<div class="pagination">
43-
{% if paginator.previous_page %}
44-
{% if paginator.previous_page == 1 %}
45-
<a href="{{ site.url }}" class="btn">Previous</a>
46-
{% else %}
47-
<a href="{{ site.url }}/page{{ paginator.previous_page }}" class="btn">Previous</a>
48-
{% endif %}
49-
{% else %}
50-
Previous
51-
{% endif %}
52-
<ul class="inline-list">
53-
<li>
54-
{% if paginator.page == 1 %}
55-
<span class="current-page">1</span>
56-
{% else %}
57-
<a href="{{ site.url }}">1</a>
58-
{% endif %}
59-
</li>
60-
{% for count in (2..paginator.total_pages) %}
61-
<li>
62-
{% if count == paginator.page %}
63-
<span class="current-page">{{ count }}</span>
64-
{% else %}
65-
<a href="{{ site.url }}/page{{ count }}">{{ count }}</a>
66-
{% endif %}
67-
</li>
68-
{% endfor %}
69-
</ul>
70-
{% if paginator.next_page %}
71-
<a href="{{ site.url }}/page{{ paginator.next_page }}" class="btn">Next</a>
72-
{% else %}
73-
Next
74-
{% endif %}
75-
</div><!-- /.pagination -->
42+
{% include pagination.html %}

0 commit comments

Comments
 (0)