Allow front matter defaults to be applied properly to documents gathered under custom `collections_dir` (#6885)

Merge pull request 6885
This commit is contained in:
Ashwin Maroli 2018-04-19 20:32:36 +05:30 committed by jekyllbot
parent 8941265837
commit bea275e4c5
3 changed files with 96 additions and 2 deletions

View File

@ -207,3 +207,79 @@ Feature: Collections Directory
And the "_site/puppies/rover.html" file should exist
And I should see "excerpt for all docs." in "_site/puppies/rover.html"
And I should see "Random Content." in "_site/2009/03/27/gathered-post.html"
Scenario: Front matter defaults and custom collections directory
Given I have a gathering/_players/managers directory
And I have a gathering/_players/recruits directory
And I have a gathering/_players/standby directory
And I have the following documents nested inside "managers" directory under the "players" collection within the "gathering" directory:
| title | content |
| Tony Stark | content for Tony. |
| Steve Rogers | content for Steve. |
And I have the following documents nested inside "recruits" directory under the "players" collection within the "gathering" directory:
| title | content |
| Peter Parker | content for Peter. |
| Wanda Maximoff | content for Wanda. |
And I have the following documents nested inside "standby" directory under the "players" collection within the "gathering" directory:
| title | content |
| Thanos | content for Thanos. |
| Loki | content for Loki. |
And I have a "_config.yml" file with content:
"""
collections_dir: gathering
collections: ["players"]
defaults:
- scope:
path: ""
type: players
values:
recruit: false
manager: false
villain: false
- scope:
path: gathering/_players/standby/thanos.md
type: players
values:
villain: true
- scope:
path: gathering/_players/managers/*
type: players
values:
manager: true
- scope:
path: gathering/_players/recruits/*
type: players
values:
recruit: true
"""
And I have a "index.md" file with content:
"""
---
---
{% for player in site.players %}
<p>{{ player.title }}: Manager: {{ player.manager }}</p>
<p>{{ player.title }}: Recruit: {{ player.recruit }}</p>
<p>{{ player.title }}: Villain: {{ player.villain }}</p>
{% endfor %}
"""
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "<p>Tony Stark: Manager: true</p>" in "_site/index.html"
And I should see "<p>Tony Stark: Recruit: false</p>" in "_site/index.html"
And I should see "<p>Tony Stark: Villain: false</p>" in "_site/index.html"
And I should see "<p>Peter Parker: Manager: false</p>" in "_site/index.html"
And I should see "<p>Peter Parker: Recruit: true</p>" in "_site/index.html"
And I should see "<p>Peter Parker: Villain: false</p>" in "_site/index.html"
And I should see "<p>Steve Rogers: Manager: true</p>" in "_site/index.html"
And I should see "<p>Steve Rogers: Recruit: false</p>" in "_site/index.html"
And I should see "<p>Steve Rogers: Villain: false</p>" in "_site/index.html"
And I should see "<p>Wanda Maximoff: Manager: false</p>" in "_site/index.html"
And I should see "<p>Wanda Maximoff: Recruit: true</p>" in "_site/index.html"
And I should see "<p>Wanda Maximoff: Villain: false</p>" in "_site/index.html"
And I should see "<p>Thanos: Manager: false</p>" in "_site/index.html"
And I should see "<p>Thanos: Recruit: false</p>" in "_site/index.html"
And I should see "<p>Thanos: Villain: true</p>" in "_site/index.html"
And I should see "<p>Loki: Manager: false</p>" in "_site/index.html"
And I should see "<p>Loki: Recruit: false</p>" in "_site/index.html"
And I should see "<p>Loki: Villain: false</p>" in "_site/index.html"

View File

@ -135,6 +135,16 @@ end
#
Given(%r!^I have the following documents? nested inside "(.*)" directory under the "(.*)" collection within the "(.*)" directory:$!) do |subdir, label, dir, table|
table.hashes.each do |input_hash|
title = slug(input_hash["title"])
path = File.join(dir, "_#{label}", subdir, "#{title}.md")
File.write(path, file_content_from_hash(input_hash))
end
end
#
Given(%r!^I have a configuration file with "(.*)" set to "(.*)"$!) do |key, value|
config = \
if source_dir.join("_config.yml").exist?

View File

@ -108,13 +108,14 @@ module Jekyll
if scope["path"].to_s.include?("*")
Dir.glob(abs_scope_path).each do |scope_path|
scope_path = Pathname.new(scope_path).relative_path_from site_path
scope_path = Pathname.new(scope_path).relative_path_from(site_path)
scope_path = strip_collections_dir(scope_path)
Jekyll.logger.debug "Globbed Scope Path:", scope_path
return true if path_is_subpath?(sanitized_path, scope_path)
end
false
else
path_is_subpath?(sanitized_path, rel_scope_path)
path_is_subpath?(sanitized_path, strip_collections_dir(rel_scope_path))
end
end
# rubocop:enable Metrics/AbcSize
@ -129,6 +130,13 @@ module Jekyll
false
end
def strip_collections_dir(path)
collections_dir = @site.config["collections_dir"]
slashed_coll_dir = "#{collections_dir}/"
return path if collections_dir.empty? || !path.to_s.start_with?(slashed_coll_dir)
path.sub(slashed_coll_dir, "")
end
# Determines whether the scope applies to type.
# The scope applies to the type if:
# 1. no 'type' is specified