Skip to content

Remove dashes from archive tags #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions lib/jekyll-archives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
Expand Down
8 changes: 8 additions & 0 deletions test/source/_posts/2016-11-09-post-with-dashed-tags.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 8 additions & 0 deletions test/source/_posts/2016-11-09-post-with-spaced-tags.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 10 additions & 1 deletion test/test_jekyll_archives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@ 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
@archives.generate(@site)
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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down