Skip to content

Commit 873a5aa

Browse files
committed
Merge pull request #10 from jekyll/enabled-archives
Add `enabled` key to config
2 parents 24d3148 + 1ad3e03 commit 873a5aa

File tree

3 files changed

+99
-15
lines changed

3 files changed

+99
-15
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,24 @@ gems:
1919

2020
```yml
2121
jekyll-archives:
22-
layout: archive # The layout to use for archive pages.
23-
permalinks:
24-
year: '/archive/:name' # The permalink to use for year-based archives.
25-
tag: '/tag/:name' # The permalink to use for tag archives.
26-
category: '/category/:name' # The permalink to use for category archives.
22+
layout: archive # The default layout to use for archive pages.
23+
enabled: # Specifies which archives are enabled.
24+
- year
25+
- month
26+
- tags
27+
layouts: # (Optional) Specifies type-specific layouts.
28+
year: year-archive
29+
month: month-archive
30+
permalinks: # (Optional) The permalinks to use for each archive.
31+
year: '/:year/'
32+
month: '/:year/:month/'
33+
day: '/:year/:month/:day'
34+
tag: '/tag/:name/'
35+
category: '/category/:name/'
2736
```
37+
38+
### The `enabled` setting
39+
Archives are enabled based on the following configuration rules:
40+
- All archives are disabled by default.
41+
- All archives can be enabled by setting `enabled: true` or `enabled: all`.
42+
- Individual archives can be enabled by setting `enabled` to an array (see above example).

lib/jekyll-archives.rb

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Archives < Jekyll::Generator
99

1010
DEFAULTS = {
1111
'layout' => 'archive',
12+
'enabled' => [],
1213
'permalinks' => {
1314
'year' => '/:year/',
1415
'month' => '/:year/:month/',
@@ -46,23 +47,46 @@ def generate(site)
4647

4748
# Read archive data from posts
4849
def read
49-
tags.each do |name, posts|
50-
@archives << Archive.new(@site, name, "tag", posts)
50+
read_tags
51+
read_categories
52+
read_dates
53+
end
54+
55+
def read_tags
56+
if enabled? "tags"
57+
tags.each do |name, posts|
58+
@archives << Archive.new(@site, name, "tag", posts)
59+
end
5160
end
52-
categories.each do |name, posts|
53-
@archives << Archive.new(@site, name, "category", posts)
61+
end
62+
63+
def read_categories
64+
if enabled? "categories"
65+
categories.each do |name, posts|
66+
@archives << Archive.new(@site, name, "category", posts)
67+
end
5468
end
69+
end
70+
71+
def read_dates
5572
years.each do |year, posts|
56-
@archives << Archive.new(@site, { :year => year }, "year", posts)
73+
@archives << Archive.new(@site, { :year => year }, "year", posts) if enabled? "year"
5774
months(posts).each do |month, posts|
58-
@archives << Archive.new(@site, { :year => year, :month => month }, "month", posts)
75+
@archives << Archive.new(@site, { :year => year, :month => month }, "month", posts) if enabled? "month"
5976
days(posts).each do |day, posts|
60-
@archives << Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts)
77+
@archives << Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts) if enabled? "day"
6178
end
6279
end
6380
end
6481
end
6582

83+
# Checks if archive type is enabled in config
84+
def enabled?(archive)
85+
@config["enabled"] == true || @config["enabled"] == "all" || if @config["enabled"].is_a? Array
86+
@config["enabled"].include? archive
87+
end
88+
end
89+
6690
# Renders the archives into the layouts
6791
def render
6892
payload = @site.site_payload

test/test_jekyll_archives.rb

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
class TestJekyllArchives < Minitest::Test
44
context "the jekyll-archives plugin" do
55
setup do
6-
@site = fixture_site
6+
@site = fixture_site({
7+
"jekyll-archives" => {
8+
"enabled" => true
9+
}
10+
})
711
@site.read
812
@archives = Jekyll::Archives.new(@site.config)
913
end
@@ -48,7 +52,8 @@ class TestJekyllArchives < Minitest::Test
4852
setup do
4953
@site = fixture_site({
5054
"jekyll-archives" => {
51-
"layout" => "archive-too"
55+
"layout" => "archive-too",
56+
"enabled" => true
5257
}
5358
})
5459
@site.read
@@ -65,6 +70,7 @@ class TestJekyllArchives < Minitest::Test
6570
setup do
6671
@site = fixture_site({
6772
"jekyll-archives" => {
73+
"enabled" => true,
6874
"permalinks" => {
6975
"year" => "/year/:year/",
7076
"tag" => "/tag-:name.html",
@@ -86,12 +92,51 @@ class TestJekyllArchives < Minitest::Test
8692

8793
context "the archives" do
8894
setup do
89-
@site = fixture_site
95+
@site = fixture_site({
96+
"jekyll-archives" => {
97+
"enabled" => true
98+
}
99+
})
90100
@site.process
91101
end
92102

93103
should "populate the {{ site.archives }} tag in Liquid" do
94104
assert_equal 12, read_file("/length.html").to_i
95105
end
96106
end
107+
108+
context "the jekyll-archives plugin with default config" do
109+
setup do
110+
@site = fixture_site
111+
@site.process
112+
end
113+
114+
should "not generate any archives" do
115+
assert_equal 0, read_file("/length.html").to_i
116+
end
117+
end
118+
119+
context "the jekyll-archives plugin with enabled array" do
120+
setup do
121+
@site = fixture_site({
122+
"jekyll-archives" => {
123+
"enabled" => ["tags"]
124+
}
125+
})
126+
@site.process
127+
end
128+
129+
should "generate the enabled archives" do
130+
assert archive_exists? @site, "/tag/test-tag/"
131+
assert archive_exists? @site, "/tag/tagged/"
132+
assert archive_exists? @site, "/tag/new/"
133+
end
134+
135+
should "not generate the disabled archives" do
136+
assert !archive_exists?(@site, "/2014/")
137+
assert !archive_exists?(@site, "/2014/08/")
138+
assert !archive_exists?(@site, "/2013/08/16/")
139+
assert !archive_exists?(@site, "/category/plugins/")
140+
end
141+
end
97142
end

0 commit comments

Comments
 (0)