diff --git a/lib/jekyll-archives.rb b/lib/jekyll-archives.rb index 94e59a9..cb6c458 100644 --- a/lib/jekyll-archives.rb +++ b/lib/jekyll-archives.rb @@ -92,8 +92,40 @@ def enabled?(archive) end end + # Helper method for tags + # Receives an array of strings + # Returns an array of strings without dashes + def remove_dashes(tags) + cleaned_tags = [] + tags.each do |tag| + cleaned_tags << tag.gsub(/-/, ' ') + end + cleaned_tags + end + + # Helper method for tags + # Receives a post, and an external hash + # Assigns posts associated with particular tags to the provided hash. + def post_attr_tags(post, hash) + post.data['tags'] ||= [] + post.data['tags'] = remove_dashes(post.data['tags']) + post.data['tags'].each { |t| hash[t] << post } if post.data['tags'] + end + + # Custom `post_attr_hash` method for tags def tags - @site.post_attr_hash('tags') + hash = Hash.new { |h, key| h[key] = [] } + + # In Jekyll 3, Collection#each should be called on the #docs array directly. + if Jekyll::VERSION >= '3.0.0' + @posts.docs.each do |p| + post_attr_tags(p, hash) + end + else + @posts.each { |p| post_attr_tags(p, hash) } + end + hash.values.each { |posts| posts.sort!.reverse! } + hash end def categories @@ -105,7 +137,7 @@ def years hash = Hash.new { |h, key| h[key] = [] } # In Jekyll 3, Collection#each should be called on the #docs array directly. - if Jekyll::VERSION >= '3.0.0' + if Jekyll::VERSION >= '3.0.0' @posts.docs.each { |p| hash[p.date.strftime("%Y")] << p } else @posts.each { |p| hash[p.date.strftime("%Y")] << p } diff --git a/test/source/_posts/2016-11-09-post-with-dashed-tags.md b/test/source/_posts/2016-11-09-post-with-dashed-tags.md new file mode 100644 index 0000000..8e07514 --- /dev/null +++ b/test/source/_posts/2016-11-09-post-with-dashed-tags.md @@ -0,0 +1,8 @@ +--- +title: Post with dashes +tags: + - words-with-dashes + - words-with-spaces +--- + +Tags in this post have text delimited by a dash. diff --git a/test/source/_posts/2016-11-09-post-with-spaced-tags.md b/test/source/_posts/2016-11-09-post-with-spaced-tags.md new file mode 100644 index 0000000..de5d7e0 --- /dev/null +++ b/test/source/_posts/2016-11-09-post-with-spaced-tags.md @@ -0,0 +1,8 @@ +--- +title: Post with spaces +tags: + - words with spaces + - words with dashes +--- + +Tags in this post have text delimited by a space. diff --git a/test/test_jekyll_archives.rb b/test/test_jekyll_archives.rb index 01eabda..3046711 100644 --- a/test/test_jekyll_archives.rb +++ b/test/test_jekyll_archives.rb @@ -16,18 +16,21 @@ class TestJekyllArchives < Minitest::Test @archives.generate(@site) assert archive_exists? @site, "2014/index.html" assert archive_exists? @site, "2013/index.html" + assert archive_exists? @site, "2016/index.html" end should "generate archive pages by month" do @archives.generate(@site) assert archive_exists? @site, "2014/08/index.html" assert archive_exists? @site, "2014/03/index.html" + assert archive_exists? @site, "2016/11/index.html" end should "generate archive pages by day" do @archives.generate(@site) assert archive_exists? @site, "2014/08/17/index.html" assert archive_exists? @site, "2013/08/16/index.html" + assert archive_exists? @site, "2016/11/09/index.html" end should "generate archive pages by tag" do @@ -35,6 +38,8 @@ class TestJekyllArchives < Minitest::Test assert archive_exists? @site, "tag/test-tag/index.html" assert archive_exists? @site, "tag/tagged/index.html" assert archive_exists? @site, "tag/new/index.html" + assert archive_exists? @site, "tag/words-with-dashes/index.html" + assert archive_exists? @site, "tag/words-with-spaces/index.html" end should "generate archive pages by category" do @@ -120,7 +125,7 @@ class TestJekyllArchives < Minitest::Test end should "populate the {{ site.archives }} tag in Liquid" do - assert_equal 12, read_file("length.html").to_i + assert_equal 17, read_file("length.html").to_i end end @@ -175,6 +180,10 @@ class TestJekyllArchives < Minitest::Test @day_archive = @archives.detect {|a| a.type == "day"} end + should "populate the {{ site.tags }} with tags that do not contain dashes" do + !@tag_archive.title.include? '-' + end + should "populate the title field in case of category or tag" do assert @tag_archive.title.is_a? String assert @category_archive.title.is_a? String