Skip to content

Commit 137dacd

Browse files
committed
Merge pull request #53 from jekyll/post-collections
Merge pull request 53
2 parents 659f024 + 302a9fc commit 137dacd

File tree

1 file changed

+9
-27
lines changed

1 file changed

+9
-27
lines changed

lib/jekyll-archives.rb

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -112,42 +112,24 @@ def write
112112
end
113113
end
114114

115-
# Construct a Hash of Posts indexed by the specified Post attribute.
116-
#
117-
# post_attr - The String name of the Post attribute.
118-
#
119-
# Examples
120-
#
121-
# post_attr_hash('categories')
122-
# # => { 'tech' => [<Post A>, <Post B>],
123-
# # 'ruby' => [<Post B>] }
124-
#
125-
# Returns the Hash: { attr => posts } where
126-
# attr - One of the values for the requested attribute.
127-
# posts - The Array of Posts with the given attr value.
128-
#
129-
# Taken from jekyll/jekyll (Copyright (c) 2014 Tom Preston-Werner under the MIT).
130-
def post_attr_hash(post_attr)
131-
# Build a hash map based on the specified post attribute ( post attr =>
132-
# array of posts ) then sort each array in reverse order.
133-
hash = Hash.new { |h, key| h[key] = [] }
134-
@posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
135-
hash.values.each { |posts| posts.sort!.reverse! }
136-
hash
137-
end
138-
139115
def tags
140-
post_attr_hash('tags')
116+
@site.post_attr_hash('tags')
141117
end
142118

143119
def categories
144-
post_attr_hash('categories')
120+
@site.post_attr_hash('categories')
145121
end
146122

147123
# Custom `post_attr_hash` method for years
148124
def years
149125
hash = Hash.new { |h, key| h[key] = [] }
150-
@posts.each { |p| hash[p.date.strftime("%Y")] << p }
126+
127+
# In Jekyll 3, Collection#each should be called on the #docs array directly.
128+
if Jekyll::VERSION >= '3.0.0'
129+
@posts.docs.each { |p| hash[p.date.strftime("%Y")] << p }
130+
else
131+
@posts.each { |p| hash[p.date.strftime("%Y")] << p }
132+
end
151133
hash.values.each { |posts| posts.sort!.reverse! }
152134
hash
153135
end

0 commit comments

Comments
 (0)