Skip to content

Commit bc45d38

Browse files
committed
Expose front matter defaults to Liquid templates
1 parent b3050ad commit bc45d38

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

lib/jekyll-archives/page_drop.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ class PageDrop < Jekyll::Drops::Drop
88
mutable false
99

1010
def_delegators :@obj, :posts, :type, :title, :date, :name, :path, :url, :permalink
11-
private def_delegator :@obj, :data, :fallback_data
11+
private def_delegators :@obj, :site, :relative_path, :data
12+
13+
def fallback_data
14+
Jekyll::Utils.deep_merge_hashes(
15+
data, site.frontmatter_defaults.all(relative_path, type)
16+
)
17+
end
18+
private :fallback_data
1219
end
1320
end
1421
end

test/test_jekyll_archive.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,75 @@ class TestJekyllArchive < Minitest::Test
9595
assert_equal expected, archive.to_liquid.to_h
9696
end
9797
end
98+
99+
context "the generated archive page preconfigured with front matter defaults" do
100+
setup do
101+
site = fixture_site(
102+
"jekyll-archives" => {
103+
"enabled" => true,
104+
},
105+
"defaults" => [
106+
{
107+
"scope" => {
108+
"type" => "category",
109+
},
110+
"values" => {
111+
"author_profile" => true,
112+
"sidebar" => false,
113+
},
114+
},
115+
{
116+
"scope" => {
117+
"type" => "day",
118+
},
119+
"values" => {
120+
"author_profile" => false,
121+
"sidebar" => true,
122+
},
123+
},
124+
]
125+
)
126+
site.read
127+
Jekyll::Archives::Archives.new(site.config).generate(site)
128+
@archives = site.config["archives"]
129+
end
130+
131+
should "expose all attributes to Liquid templates" do
132+
archive = @archives.find { |a| a.type == "category" }
133+
archive.posts = []
134+
expected = {
135+
"author_profile" => true,
136+
"sidebar" => false,
137+
"layout" => "archive",
138+
"posts" => [],
139+
"type" => "category",
140+
"title" => "plugins",
141+
"date" => nil,
142+
"name" => "index",
143+
"path" => "category/plugins/index.html",
144+
"url" => "/category/plugins/",
145+
"permalink" => nil,
146+
}
147+
148+
assert_equal expected, archive.to_liquid.to_h
149+
150+
archive = @archives.find { |a| a.type == "day" }
151+
archive.posts = []
152+
expected = {
153+
"author_profile" => false,
154+
"sidebar" => true,
155+
"layout" => "archive",
156+
"posts" => [],
157+
"type" => "day",
158+
"title" => nil,
159+
"date" => archive.date,
160+
"name" => "index",
161+
"path" => "2013/08/16/index.html",
162+
"url" => "/2013/08/16/",
163+
"permalink" => nil,
164+
}
165+
166+
assert_equal expected, archive.to_liquid.to_h
167+
end
168+
end
98169
end

0 commit comments

Comments
 (0)