Skip to content

Expose front matter defaults to Liquid templates #183

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 3 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
12 changes: 10 additions & 2 deletions lib/jekyll-archives/page_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ class PageDrop < Jekyll::Drops::Drop

mutable false

def_delegators :@obj, :posts, :type, :title, :date, :name, :path, :url, :permalink
private def_delegator :@obj, :data, :fallback_data
def_delegators :@obj, :posts, :type, :title, :date, :name, :path, :url, :permalink,
:site, :relative_path, :data

def fallback_data
Jekyll::Utils.deep_merge_hashes(
data, site.frontmatter_defaults.all(relative_path, type)
)
end

private :site, :relative_path, :data, :fallback_data
end
end
end
71 changes: 71 additions & 0 deletions test/test_jekyll_archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,75 @@ class TestJekyllArchive < Minitest::Test
assert_equal expected, archive.to_liquid.to_h
end
end

context "the generated archive page preconfigured with front matter defaults" do
setup do
site = fixture_site(
"jekyll-archives" => {
"enabled" => true,
},
"defaults" => [
{
"scope" => {
"type" => "category",
},
"values" => {
"author_profile" => true,
"sidebar" => false,
},
},
{
"scope" => {
"type" => "day",
},
"values" => {
"author_profile" => false,
"sidebar" => true,
},
},
]
)
site.read
Jekyll::Archives::Archives.new(site.config).generate(site)
@archives = site.config["archives"]
end

should "expose all attributes to Liquid templates" do
archive = @archives.find { |a| a.type == "category" }
archive.posts = []
expected = {
"author_profile" => true,
"sidebar" => false,
"layout" => "archive",
"posts" => [],
"type" => "category",
"title" => "plugins",
"date" => nil,
"name" => "index",
"path" => "category/plugins/index.html",
"url" => "/category/plugins/",
"permalink" => nil,
}

assert_equal expected, archive.to_liquid.to_h

archive = @archives.find { |a| a.type == "day" }
archive.posts = []
expected = {
"author_profile" => false,
"sidebar" => true,
"layout" => "archive",
"posts" => [],
"type" => "day",
"title" => nil,
"date" => archive.date,
"name" => "index",
"path" => "2013/08/16/index.html",
"url" => "/2013/08/16/",
"permalink" => nil,
}

assert_equal expected, archive.to_liquid.to_h
end
end
end