Skip to content

Commit

Permalink
Merge pull request #227 from jekyll/site-authors-array
Browse files Browse the repository at this point in the history
Ensure site.data.authors is properly formatted before attempting to retrieve author meta
  • Loading branch information
benbalter authored Aug 22, 2017
2 parents 3fca8b4 + 7b3820f commit 9fbeded
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/jekyll-seo-tag/drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,24 @@ def author_string_or_hash
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
Expand Down
18 changes: 18 additions & 0 deletions spec/jekyll_seo_tag/drop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@
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
Expand Down

0 comments on commit 9fbeded

Please sign in to comment.