Merge pull request #227 from jekyll/site-authors-array

Ensure site.data.authors is properly formatted before attempting to retrieve author meta
This commit is contained in:
Ben Balter 2017-08-22 13:11:01 -04:00 committed by GitHub
commit 9fbeded5f2
2 changed files with 35 additions and 8 deletions

View File

@ -240,15 +240,24 @@ module Jekyll
end
end
# Given a string representing the current document's author, return
# a normalized hash representing that author. Will try to pull from
# site.authors if present and in the proper format.
def author_hash(author_string)
if site.data["authors"] && site.data["authors"][author_string]
hash = site.data["authors"][author_string]
hash["name"] ||= author_string
hash["twitter"] ||= author_string
hash
else
{ "name" => author_string }
end
site_author_hash(author_string) || { "name" => author_string }
end
# Given a string representing the current document's author, attempt
# to retrieve additional metadata from site.data.authors, if present
#
# Returns the author hash
def site_author_hash(author_string)
return unless site.data["authors"] && site.data["authors"].is_a?(Hash)
author_hash = site.data["authors"][author_string]
return unless author_hash.is_a?(Hash)
author_hash["name"] ||= author_string
author_hash["twitter"] ||= author_string
author_hash
end
def seo_name

View File

@ -231,6 +231,24 @@ RSpec.describe Jekyll::SeoTag::Drop do
site
end
context "with site.authors as an array" do
let("data") { { "authors" => %w(foo bar) } }
let(:page_meta) { { "author" => "foo" } }
it "doesn't error" do
expect(subject.author).to eql({ "name" => "foo", "twitter" => "foo" })
end
end
context "with site.authors[author] as string" do
let("data") { { "authors" => { "foo" => "bar" } } }
let(:page_meta) { { "author" => "foo" } }
it "doesn't error" do
expect(subject.author).to eql({ "name" => "foo", "twitter" => "foo" })
end
end
%i[with without].each do |site_data_type|
context "#{site_data_type} site.author data" do
let(:data) do