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

Allow to exclude certain categories from paginated posts #6 #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ Or install it yourself as:

Once the gem is installed on your system, Jekyll will auto-require it. Just set the following configuration

# this enables pagination
paginate: 5

# set the url pattern for pagination
# if not set, the default is **/page:num**
paginate_path: "blog/page:num/"

# this removes a category from pagination
# eg: any post with *category: 'toto'* or * categories: ['toto']
# in front matter

not_paginated_categories:
- 'toto'

# for multiple categories

not_paginated_categories:
- 'toto'
- 'titi'

See also [Pagination documentation on Jekyll site](http://jekyllrb.com/docs/pagination/) for use in templates.

## Contributing

1. Fork it ( http://github.com/jekyll/jekyll-paginate/fork )
Expand Down
37 changes: 37 additions & 0 deletions lib/jekyll-paginate/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ def generate(site)
# "next_page" => <Number> }}
def paginate(site, page)
all_posts = site.site_payload['site']['posts'].reject { |post| post['hidden'] }

# can be moved to Jekyll:Configuration:fix_common_issues
if !site.config['not_paginated_categories'].nil? && !site.config['not_paginated_categories'].is_a?(Array)
Jekyll.logger.warn "Config Warning:", "The `not_paginated_categories` key must be an array" +
" It's currently set to '#{config['not_paginated_categories'].inspect}'."
config['not_paginated_categories'] = nil
end

if !site.config['not_paginated_categories'].nil?
all_posts = filter(site, all_posts)
end

pages = Pager.calculate_pages(all_posts, site.config['paginate'].to_i)
(1..pages).each do |num_page|
pager = Pager.new(site, num_page, all_posts, pages)
Expand Down Expand Up @@ -80,6 +92,31 @@ def self.template_page(site)
end.first
end

# Public: Filter posts list depending on categories
#
# site - the Jekyll::Site object
# posts - array of site.posts
#
# Returns array of filtered posts that are not in a **not_paginated_categories**
def filter(site, posts)
posts.reject{ |post| excluded?(site, post) }
end

# Public: Vote if a post is from an excluded category
#
# site - the Jekyll::Site object
# post - a Jekyll::Post object
#
# Returns boolean true if excluded - false if not
def excluded?(site, post)
post.categories.each do |c|
if site.config['not_paginated_categories'].index(c)
return true
end
end
return false
end

end
end
end
44 changes: 44 additions & 0 deletions spec/pagination_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'spec_helper'

RSpec.describe(Jekyll::Paginate::Pagination) do

context "with the default posts filtering behavior **hidden: true** variable in front matter" do
let(:site) { build_site }

# this test needs a post with **hidden: true** in front matter
it "filters posts with **hidden: false** in front matter" do
expect(pagination_contains_hidden_post?(site)).to eql(false)
end
end

context "with posts filtering by categories disabled" do
let(:site) { build_site }

# this test needs a post with **category: 'toto'** in front matter
it "doesn't filter posts with **category: 'toto'** in front matter" do
expect(pagination_contains_category_post?(site, 'toto')).to eql(true)
end
end

context "with posts filtering by categories enabled for toto" do
let(:site) { build_site({'not_paginated_categories' => ['toto']}) }

# this test needs a post with **category: 'toto'** in front matter
it "filters posts with **category: 'toto'** in front matter" do
expect(pagination_contains_category_post?(site, 'toto')).to eql(false)
end
end

context "with posts filtering by categories enabled for toto and titi" do
let(:site) { build_site({'not_paginated_categories' => ['toto', 'titi']}) }

# this test needs a post with **category: 'toto'** in front matter
# this test needs a post with **category: 'titi'** in front matter
it "filters posts with **category: 'toto'** and **category: 'titi'** in front matter" do
expect(pagination_contains_category_post?(site, 'toto')).to eql(false)
expect(pagination_contains_category_post?(site, 'titi')).to eql(false)
end
end


end
4 changes: 4 additions & 0 deletions spec/source/_posts/2015-01-20-hidden.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
hidden: true
title: "Hidden post"
---
5 changes: 5 additions & 0 deletions spec/source/_posts/2015-01-21-toto-category.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
hidden: false
title: "Toto category post"
category: 'toto'
---
5 changes: 5 additions & 0 deletions spec/source/_posts/2025-01-21-titi-category.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
hidden: false
title: "Titi category post"
category: 'titi'
---
27 changes: 27 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ def build_site(config = {})
site.process
site
end

def pagination_contains_hidden_post?(site)
site.pages.each do |p|
if !p.pager.nil?
p.pager.posts.each do |post|
if !post.data['hidden'].nil? && post.data['hidden'] === true
return true
end
end
end
end
return false
end

def pagination_contains_category_post?(site, category)
site.pages.each do |p|
if !p.pager.nil?
p.pager.posts.each do |post|
if !post['categories'].nil? && post['categories'].index(category)
return true
end
end
end
end
return false
end

end

RSpec.configure do |config|
Expand Down