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

Get path for first page of paginated pages #219

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
9 changes: 8 additions & 1 deletion lib/jekyll-github-metadata/edit-link-tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def self.def_hash_delegator(hash_method, key, method, default = nil)
private def_hash_delegator :site_github, :source, :source, {}
private def_hash_delegator :source, :branch, :branch
private def_hash_delegator :source, :path, :source_path
private def_hash_delegator :page, :path, :page_path

def render(context)
@context = context
Expand Down Expand Up @@ -72,6 +71,14 @@ def parts
memoize_conditionally { [repository_url, "edit/", branch, source_path, page_path] }
end

def page_path
return page["path"] unless page["paginated"]

site.pages.each do |page|
return page["path"] if page.pager && page.pager.page == 1
end
end

def parts_normalized
memoize_conditionally do
parts.map.with_index do |part, index|
Expand Down
18 changes: 17 additions & 1 deletion spec/edit_link_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
let(:source) { { "branch" => branch, "path" => path } }
let(:github) { { "repository_url" => repository_url, "source" => source } }
let(:config) { { "github" => github, "plugins" => ["jekyll-github-metadata"] } }
let(:page) { make_page }
let(:pager) { nil }
let(:page) { make_page({}, pager) }
let(:site) { make_site(config) }
let(:render_context) { make_context(:page => page, :site => site) }
let(:tag_name) { "github_edit_link" }
Expand Down Expand Up @@ -114,6 +115,21 @@
end
end

context "paginated path" do
let(:pager) { make_pager(2) }
before do
page.dir = "page/2"
subject.send(:site).pages << page
first_page = make_page({}, make_pager(1))
subject.send(:site).pages << first_page
end

it "outputs the proper link for subsequent pages" do
expect(page.path).to eql("page/2/page.md")
expect(subject.send(:page_path)).to eql("page.md")
end
end

context "parts" do
it "builds the parts" do
expected = [github["repository_url"], "edit/", branch.to_s, "/", "page.md"]
Expand Down
19 changes: 17 additions & 2 deletions spec/spec_helpers/fixture_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ def config_defaults
}
end

def make_page(data = {})
Jekyll::Page.new(site, config_defaults["source"], "", "page.md").tap { |page| page.data = data }
def make_page(data = {}, pager = nil)
data["paginated"] = true unless pager == nil
page = Jekyll::Page.new(site, config_defaults["source"], "", "page.md").tap { |page| page.data = data }
parkr marked this conversation as resolved.
Show resolved Hide resolved
page.pager = pager
page
end

def make_pager(page)
Pager.new(page)
end

def make_site(options = {})
Expand All @@ -35,4 +42,12 @@ def make_context(registers = {}, environments = {})
context = { :site => make_site, :page => make_page }.merge(registers)
Liquid::Context.new(environments, {}, context)
end

class Pager
attr_accessor :page

def initialize(page)
@page = page
end
end
end