Skip to content

Instantly share code, notes, and snippets.

@agragregra
Created October 10, 2016 10:08
Show Gist options
  • Save agragregra/1314f5d7a3c4c7fbff8ba95f73934a13 to your computer and use it in GitHub Desktop.
Save agragregra/1314f5d7a3c4c7fbff8ba95f73934a13 to your computer and use it in GitHub Desktop.
Jekyll Post Navigation (Prev & Next Posts)
module Jekyll
class WithinCategoryPostNavigation < Generator
def generate(site)
site.categories.each_pair do |category, posts|
posts.sort! { |a,b| b <=> a}
posts.each do |post|
index = posts.index post
next_in_category = nil
previous_in_category = nil
if index
if index < posts.length - 1
next_in_category = posts[index + 1]
end
if index > 0
previous_in_category = posts[index - 1]
end
end
post.data["next_in_category"] = next_in_category unless next_in_category.nil?
post.data["previous_in_category"] = previous_in_category unless previous_in_category.nil?
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment