jekyll / jekyll-archives

:books: Archive pages for your Jekyll tags and categories.
https://jekyll.github.io/jekyll-archives/
MIT License
436 stars 99 forks source link

Read-in site's tags and categories attributes #137

Closed ashmaroli closed 5 years ago

ashmaroli commented 5 years ago

Jekyll::Site#tags and Jekyll::Site#categories (introduced in Jekyll 2.0 via https://github.com/jekyll/jekyll/commit/8c0e5d8d98b2713339d4c73359017e8303c684af) already encapsulate the exact same logic:

# As of Jekyll 3.8.5

    def post_attr_hash(post_attr)
      # Build a hash map based on the specified post attribute ( post attr =>
      # array of posts ) then sort each array in reverse order.
      hash = Hash.new { |h, key| h[key] = [] }
      posts.docs.each do |p|
        p.data[post_attr].each { |t| hash[t] << p } if p.data[post_attr]
      end
      hash.each_value { |posts| posts.sort!.reverse! }
      hash
    end

    def tags
      post_attr_hash("tags")
    end

    def categories
      post_attr_hash("categories")
    end

So, we might as well just delegate to the methods in Jekyll::Site. However, it's not much of a concern since Archives#tags and Archives#categories are called at most just once per call to the Archives#generate method.

--

*Notes:

mattr- commented 5 years ago

@jekyllbot: merge +dev