Skip to content

Commit 8a55592

Browse files
committed
Slug tags for URL
1 parent b62fa76 commit 8a55592

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

lib/jekyll-archives/archive.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Jekyll
22
class Archive
33
include Convertible
44

5-
attr_accessor :posts, :type, :name
5+
attr_accessor :posts, :type, :name, :slug
66
attr_accessor :data, :content, :output
77
attr_accessor :path, :ext
88
attr_accessor :site
@@ -27,6 +27,9 @@ def initialize(site, name, type, posts)
2727
@type = type
2828
@name = name
2929

30+
# Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb)
31+
@slug = name.downcase.gsub(/[^\w]/, " ").strip.gsub(/\s+/, '-') if name.is_a? String
32+
3033
# Use ".html" for file extension and url for path
3134
@ext = ".html"
3235
@path = url
@@ -50,7 +53,7 @@ def url_placeholders
5053
if @name.is_a? Hash
5154
@name.merge({ :type => @type })
5255
else
53-
{ :name => @name, :type => @type }
56+
{ :name => @slug, :type => @type }
5457
end
5558
end
5659

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Tagged post
3-
tags: [test, tagged]
3+
tags: ["Test Tag", tagged]
44
---
55

66
This is a tagged post

test/test_jekyll_archives.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TestJekyllArchives < Minitest::Test
2828

2929
should "generate archive pages by tag" do
3030
@archives.generate(@site)
31-
assert archive_exists? @site, "/tag/test/"
31+
assert archive_exists? @site, "/tag/test-tag/"
3232
assert archive_exists? @site, "/tag/tagged/"
3333
assert archive_exists? @site, "/tag/new/"
3434
end
@@ -40,7 +40,7 @@ class TestJekyllArchives < Minitest::Test
4040

4141
should "generate archive pages with a layout" do
4242
@site.process
43-
assert_equal "Test", read_file("/tag/test/index.html")
43+
assert_equal "Test", read_file("/tag/test-tag/index.html")
4444
end
4545
end
4646

@@ -57,7 +57,7 @@ class TestJekyllArchives < Minitest::Test
5757

5858
should "use custom layout" do
5959
@site.process
60-
assert_equal "Test too", read_file("/tag/test/index.html")
60+
assert_equal "Test too", read_file("/tag/test-tag/index.html")
6161
end
6262
end
6363

@@ -78,7 +78,7 @@ class TestJekyllArchives < Minitest::Test
7878
should "use the right permalink" do
7979
assert archive_exists? @site, "/year/2014/"
8080
assert archive_exists? @site, "/year/2013/"
81-
assert archive_exists? @site, "/tag-test.html"
81+
assert archive_exists? @site, "/tag-test-tag.html"
8282
assert archive_exists? @site, "/tag-new.html"
8383
assert archive_exists? @site, "/category-plugins.html"
8484
end

0 commit comments

Comments
 (0)