Merge pull request #4290 from jekyll/deal-with-extraneous-spaces

Merge pull request 4290
This commit is contained in:
Parker Moore 2016-01-04 14:57:22 -08:00
commit a78230ccdb
4 changed files with 25 additions and 2 deletions

View File

@ -120,7 +120,9 @@ module Jekyll
# #
# Returns true if the YAML front matter is present. # Returns true if the YAML front matter is present.
def has_yaml_header?(file) def has_yaml_header?(file)
!!(File.open(file, 'rb') { |f| f.read(5) } =~ /\A---\r?\n/) !!(File.open(file, 'rb') { |f| f.readline } =~ /\A---\s*\r?\n/)
rescue EOFError
false
end end
# Slugify a filename or title. # Slugify a filename or title.

View File

@ -0,0 +1,3 @@
---
extra: spaces
---

View File

@ -12,7 +12,7 @@ class TestGeneratedSite < JekyllUnitTest
end end
should "ensure post count is as expected" do should "ensure post count is as expected" do
assert_equal 48, @site.posts.size assert_equal 49, @site.posts.size
end end
should "insert site.posts into the index" do should "insert site.posts into the index" do

View File

@ -259,4 +259,22 @@ class TestUtils < JekyllUnitTest
end end
end end
context "The \`Utils.has_yaml_header?\` method" do
should "accept files with yaml front matter" do
file = source_dir("_posts", "2008-10-18-foo-bar.markdown")
assert_equal "---\n", File.open(file, 'rb') { |f| f.read(4) }
assert Utils.has_yaml_header?(file)
end
should "accept files with extraneous spaces after yaml front matter" do
file = source_dir("_posts", "2015-12-27-extra-spaces.markdown")
assert_equal "--- \n", File.open(file, 'rb') { |f| f.read(6) }
assert Utils.has_yaml_header?(file)
end
should "reject pgp files and the like which resemble front matter" do
file = source_dir("pgp.key")
assert_equal "-----B", File.open(file, 'rb') { |f| f.read(6) }
refute Utils.has_yaml_header?(file)
end
end
end end