From f78999819f04c89a5e9e3e0a54e94a76ce1fd892 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Tue, 22 Aug 2017 11:39:52 -0400 Subject: [PATCH] ensure site.data.authors is in the expected format before resolving author meta --- lib/jekyll-seo-tag/drop.rb | 21 +++++++++++++-------- spec/jekyll_seo_tag/drop_spec.rb | 4 ++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/jekyll-seo-tag/drop.rb b/lib/jekyll-seo-tag/drop.rb index c69f70d..e5cd5f7 100644 --- a/lib/jekyll-seo-tag/drop.rb +++ b/lib/jekyll-seo-tag/drop.rb @@ -239,15 +239,20 @@ 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 + + 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 49e683f..a47ad67 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -232,11 +232,11 @@ RSpec.describe Jekyll::SeoTag::Drop do end context "with site.authors as an array" do - let("data") { ["foo", "bar"] } + let("data") { { "authors" => ["foo", "bar"] } } let(:page_meta) { {"author" => "foo"} } it "doesn't error" do - expect(subject.author).to eql("") + expect(subject.author).to eql({"name"=>"foo", "twitter"=>"foo"}) end end