From 9cb1ad0bd6ef941089de01c282c1771096bc65e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20Fr=C3=B6hner?= Date: Mon, 11 Jan 2021 17:13:50 +0100 Subject: [PATCH] Allow to set type for author (#427) Merge pull request 427 --- lib/jekyll-seo-tag/json_ld_drop.rb | 8 ++++++-- spec/jekyll_seo_tag/json_ld_drop_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/jekyll-seo-tag/json_ld_drop.rb b/lib/jekyll-seo-tag/json_ld_drop.rb index 3bd1d46..88bb0e0 100644 --- a/lib/jekyll-seo-tag/json_ld_drop.rb +++ b/lib/jekyll-seo-tag/json_ld_drop.rb @@ -21,7 +21,8 @@ module Jekyll private :logo VALID_ENTITY_TYPES = %w(BlogPosting CreativeWork).freeze - private_constant :VALID_ENTITY_TYPES + VALID_AUTHOR_TYPES = %w(Organization Person).freeze + private_constant :VALID_ENTITY_TYPES, :VALID_AUTHOR_TYPES # page_drop should be an instance of Jekyll::SeoTag::Drop def initialize(page_drop) @@ -38,8 +39,11 @@ module Jekyll def author return unless page_drop.author["name"] + author_type = page_drop.author["type"] + return if author_type && !VALID_AUTHOR_TYPES.include?(author_type) + { - "@type" => "Person", + "@type" => author_type || "Person", "name" => page_drop.author["name"], } end diff --git a/spec/jekyll_seo_tag/json_ld_drop_spec.rb b/spec/jekyll_seo_tag/json_ld_drop_spec.rb index 68bd275..c40f4ed 100644 --- a/spec/jekyll_seo_tag/json_ld_drop_spec.rb +++ b/spec/jekyll_seo_tag/json_ld_drop_spec.rb @@ -72,6 +72,29 @@ RSpec.describe Jekyll::SeoTag::JSONLDDrop do end end end + + context "when type Organization" do + let(:author) { { "name" => "organization", "type" => "Organization" } } + + it "returns the author with type" do + expect(subject).to have_key("author") + expect(subject["author"]).to be_a(Hash) + expect(subject["author"]).to have_key("@type") + expect(subject["author"]["@type"]).to eql("Organization") + expect(subject["author"]).to have_key("name") + expect(subject["author"]["name"]).to be_a(String) + expect(subject["author"]["name"]).to eql("organization") + end + end + + context "when invalid type" do + let(:author) { { "name" => "organization", "type" => "Invalid" } } + + it "returns the author with type" do + expect(subject).to have_key("author") + expect(subject["author"]).to be nil + end + end end context "image" do