diff --git a/lib/jekyll-seo-tag/drop.rb b/lib/jekyll-seo-tag/drop.rb index da6cf60..1973c65 100644 --- a/lib/jekyll-seo-tag/drop.rb +++ b/lib/jekyll-seo-tag/drop.rb @@ -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 diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index 35fa38b..fe54161 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -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