Forked from stravid/category_aware_next_generator.rb
Last active
May 10, 2016 11:35
-
-
Save kobapan/933cbe76c4109408755a774f0a21e5c2 to your computer and use it in GitHub Desktop.
Tag aware previous next attribute for a Jekyll post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#how to use in template | |
# | |
#{{ page.tag }}'s post' | |
#<a href="{{site.url}}{{ page.tag_previous.url }}">« {{ page.tag_previous.title }}</a> | |
#<a href="{{site.url}}{{ page.tag_next.url }}">{{ page.tag_next.title }} »</a> | |
# | |
module Jekyll | |
class TagAwarePreviousNextGenerator < Generator | |
safe true | |
priority :high | |
def generate(site) | |
site.tags.each_pair do |tag_name, posts| | |
posts.sort! { |a, b| b <=> a } | |
posts.each do |post| | |
position = posts.index post | |
if position && position < posts.length - 1 | |
tag_previous = posts[position + 1] | |
else | |
tag_previous = nil | |
end | |
if position && position > 0 | |
tag_next = posts[position - 1] | |
else | |
tag_next = nil | |
end | |
post.data["tag_previous"] = tag_previous unless tag_previous.nil? | |
post.data["tag_next"] = tag_next unless tag_next.nil? | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment