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 Absolute permalinks break paginator when permalink is specified for index file #5 #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 13 additions & 4 deletions lib/jekyll-paginate/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ def paginate(site, page)
(1..pages).each do |num_page|
pager = Pager.new(site, num_page, all_posts, pages)
if num_page > 1
newpage = Page.new(site, site.source, page.dir, page.name)
newpage.pager = pager
newpage.dir = Pager.paginate_path(site, num_page)
site.pages << newpage
site.pages << build_paginated_page(site, page, pager)
else
page.pager = pager
end
Expand Down Expand Up @@ -80,6 +77,18 @@ def self.template_page(site)
end.first
end

private
def build_paginated_page(site, page, pager)
page = Page.new(site, site.source, source_to_file_path(page), page.name)
page.data.delete('permalink')
page.pager = pager
page.dir = Pager.paginate_path(site, pager.page)
page
end
def source_to_file_path(page)
page.url_placeholders[:path] # meant to use page.instance_variable_get('@dir')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

page.path?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried with page.path but test was failed since this includes filename.
File.dirname(page.path) seems to be another solution clearer than page.url_placeholders[:path]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try page.relative_path then 😄 There was a liquid inconsistency that this will bump into.

end

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

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

context "with absoulte permalink in index file" do
before(:each) do
@original_index_string = File.read(File.join(source_dir, 'index.html'))
end
after(:each) do
File.open(File.join(source_dir, 'index.html'), 'w') { |f| f.write(@original_index_string) }
end

def rewrite_index(frontmatter_string)
File.open(File.join(source_dir, 'index.html'), 'w') { |f| f.write("---\n#{frontmatter_string}\n---\nbody") }
end

let(:site) { build_site }

it "paginated pages should not use absoulte permalink from index file" do
rewrite_index("permalink: /")
expect(site.pages.select {|page| page.url == "/"}.length).to eql(1)
end

it "paginated pages should be generated from index file given parmalink not matched to source" do
rewrite_index("permalink: /posts/")
site
expect(File.read(File.join(dest_dir('page2'), 'index.html'))).to eql("body")
end
end
end