From 57bcfce4029d60e9b81eb8b8ad94b32ded63d300 Mon Sep 17 00:00:00 2001 From: Benjamin Kostenbader Date: Tue, 12 Jan 2021 21:25:11 -0500 Subject: [PATCH 1/5] add @graph functionality and pass it back for review --- lib/jekyll-seo-tag/json_ld_drop.rb | 20 +++++++++++++- spec/jekyll_seo_tag_integration_spec.rb | 35 +++++++++++++------------ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/lib/jekyll-seo-tag/json_ld_drop.rb b/lib/jekyll-seo-tag/json_ld_drop.rb index 88bb0e0..eaa9d73 100644 --- a/lib/jekyll-seo-tag/json_ld_drop.rb +++ b/lib/jekyll-seo-tag/json_ld_drop.rb @@ -84,7 +84,25 @@ module Jekyll private :main_entity def to_json - to_h.reject { |_k, v| v.nil? }.to_json + # this was what happened before + graph = to_h.reject { |_k, v| v.nil? } + + # assign publisher and remove it from the array + # (because I don't know the meta-programming involved in instantiating it) + publisher = graph["publisher"] || nil + graph.delete("publisher") + + updated_graph = { + "@context" => "https://schema.org", + "@graph" => [graph], + } + if publisher + # .push({"something" => blah}) === << {"something" => blah} + updated_graph["@graph"] << publisher + end + + # .to_json for the win + updated_graph.to_json end private diff --git a/spec/jekyll_seo_tag_integration_spec.rb b/spec/jekyll_seo_tag_integration_spec.rb index df2ebac..cd12c20 100755 --- a/spec/jekyll_seo_tag_integration_spec.rb +++ b/spec/jekyll_seo_tag_integration_spec.rb @@ -236,7 +236,7 @@ RSpec.describe Jekyll::SeoTag do let(:site) { make_site("logo" => "/logo.png", "url" => "http://example.invalid") } it "outputs the logo" do - expect(json_data["publisher"]["logo"]["url"]).to eql("http://example.invalid/logo.png") + expect(json_data["@graph"][1]["logo"]["url"]).to eql("http://example.invalid/logo.png") end end @@ -244,7 +244,7 @@ RSpec.describe Jekyll::SeoTag do let(:site) { make_site("logo" => "http://cdn.example.invalid/logo.png", "url" => "http://example.invalid") } it "outputs the logo" do - expect(json_data["publisher"]["logo"]["url"]).to eql("http://cdn.example.invalid/logo.png") + expect(json_data["@graph"][1]["logo"]["url"]).to eql("http://cdn.example.invalid/logo.png") end end @@ -252,7 +252,7 @@ RSpec.describe Jekyll::SeoTag do let(:site) { make_site("logo" => "http://cdn.example.invalid/logo.png", "url" => "http://example.invalid", "author" => "Mr. Foo") } it "outputs the author" do - expect(json_data["publisher"]["name"]).to eql("Mr. Foo") + expect(json_data["@graph"][1]["name"]).to eql("Mr. Foo") end end @@ -261,12 +261,12 @@ RSpec.describe Jekyll::SeoTag do let(:page) { make_post("author" => "Mr. Foo") } it "outputs the author" do - expect(json_data["author"]["@type"]).to eql("Person") - expect(json_data["author"]["name"]).to eql("Mr. Foo") + expect(json_data["@graph"][0]["author"]["@type"]).to eql("Person") + expect(json_data["@graph"][0]["author"]["name"]).to eql("Mr. Foo") end it "outputs the publisher author" do - expect(json_data["publisher"]["name"]).to eql("Mr. Foo") + expect(json_data["@graph"][1]["name"]).to eql("Mr. Foo") end end @@ -275,8 +275,8 @@ RSpec.describe Jekyll::SeoTag do let(:page) { make_post("seo" => { "type" => "BlogPosting" }, "permalink" => "/foo/") } it "outputs the mainEntityOfPage" do - expect(json_data["mainEntityOfPage"]["@type"]).to eql("WebPage") - expect(json_data["mainEntityOfPage"]["@id"]).to eql("http://example.invalid/foo/") + expect(json_data["@graph"][0]["mainEntityOfPage"]["@type"]).to eql("WebPage") + expect(json_data["@graph"][0]["mainEntityOfPage"]["@id"]).to eql("http://example.invalid/foo/") end end @@ -285,8 +285,8 @@ RSpec.describe Jekyll::SeoTag do let(:page) { make_post("seo" => { "type" => "CreativeWork" }, "permalink" => "/foo/") } it "outputs the mainEntityOfPage" do - expect(json_data["mainEntityOfPage"]["@type"]).to eql("WebPage") - expect(json_data["mainEntityOfPage"]["@id"]).to eql("http://example.invalid/foo/") + expect(json_data["@graph"][0]["mainEntityOfPage"]["@type"]).to eql("WebPage") + expect(json_data["@graph"][0]["mainEntityOfPage"]["@id"]).to eql("http://example.invalid/foo/") end end @@ -330,9 +330,9 @@ RSpec.describe Jekyll::SeoTag do expected = %r!! expect(output).to match(expected) - expect(json_data["headline"]).to eql("post") - expect(json_data["description"]).to eql("description") - expect(json_data["image"]).to eql("http://example.invalid/img.png") + expect(json_data["@graph"][0]["headline"]).to eql("post") + expect(json_data["@graph"][0]["description"]).to eql("description") + expect(json_data["@graph"][0]["image"]).to eql("http://example.invalid/img.png") end it "minifies JSON-LD" do @@ -551,9 +551,10 @@ RSpec.describe Jekyll::SeoTag do end it "outputs social meta" do - expect(json_data["@type"]).to eql("person") - expect(json_data["name"]).to eql("Ben") - expect(json_data["sameAs"]).to eql(links) + # binding.pry + expect(json_data["@graph"][0]["@type"]).to eql("person") + expect(json_data["@graph"][0]["name"]).to eql("Ben") + expect(json_data["@graph"][0]["sameAs"]).to eql(links) end end @@ -561,7 +562,7 @@ RSpec.describe Jekyll::SeoTag do let(:meta) { { "permalink" => "/about/" } } it "outputs sameAs links" do - expect(json_data["sameAs"]).to eql(links) + expect(json_data["@graph"][0]["sameAs"]).to eql(links) end end From 63fd8e262de40bfbadaba3b4984960314e73776a Mon Sep 17 00:00:00 2001 From: Benjamin Kostenbader Date: Tue, 12 Jan 2021 21:56:10 -0500 Subject: [PATCH 2/5] add canonical_url in there too, test against Rich Results, pass --- lib/jekyll-seo-tag/json_ld_drop.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/jekyll-seo-tag/json_ld_drop.rb b/lib/jekyll-seo-tag/json_ld_drop.rb index eaa9d73..3b96b30 100644 --- a/lib/jekyll-seo-tag/json_ld_drop.rb +++ b/lib/jekyll-seo-tag/json_ld_drop.rb @@ -63,6 +63,7 @@ module Jekyll output = { "@type" => "Organization", + "url" => page_drop.canonical_url, "logo" => { "@type" => "ImageObject", "url" => logo, From ea5a3ef6404ddd0bce1493a2c18bc256cc65f017 Mon Sep 17 00:00:00 2001 From: Benji Date: Tue, 30 Mar 2021 07:08:13 -0400 Subject: [PATCH 3/5] Update lib/jekyll-seo-tag/json_ld_drop.rb Yep, let's remove these comments! Co-authored-by: Ashwin Maroli --- lib/jekyll-seo-tag/json_ld_drop.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/jekyll-seo-tag/json_ld_drop.rb b/lib/jekyll-seo-tag/json_ld_drop.rb index 3b96b30..4a806b2 100644 --- a/lib/jekyll-seo-tag/json_ld_drop.rb +++ b/lib/jekyll-seo-tag/json_ld_drop.rb @@ -85,7 +85,6 @@ module Jekyll private :main_entity def to_json - # this was what happened before graph = to_h.reject { |_k, v| v.nil? } # assign publisher and remove it from the array From b14dac451549d21b940cd1e3fdd93bc85b36e319 Mon Sep 17 00:00:00 2001 From: Benji Date: Tue, 30 Mar 2021 07:08:25 -0400 Subject: [PATCH 4/5] Update lib/jekyll-seo-tag/json_ld_drop.rb Yep, let's remove these comments! Co-authored-by: Ashwin Maroli --- lib/jekyll-seo-tag/json_ld_drop.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/jekyll-seo-tag/json_ld_drop.rb b/lib/jekyll-seo-tag/json_ld_drop.rb index 4a806b2..2db7231 100644 --- a/lib/jekyll-seo-tag/json_ld_drop.rb +++ b/lib/jekyll-seo-tag/json_ld_drop.rb @@ -88,7 +88,6 @@ module Jekyll graph = to_h.reject { |_k, v| v.nil? } # assign publisher and remove it from the array - # (because I don't know the meta-programming involved in instantiating it) publisher = graph["publisher"] || nil graph.delete("publisher") From 4cf27220cc86f55e235133d01f7f2cfdb527ecb9 Mon Sep 17 00:00:00 2001 From: Benji Date: Tue, 30 Mar 2021 07:08:54 -0400 Subject: [PATCH 5/5] Update lib/jekyll-seo-tag/json_ld_drop.rb Yep, let's remove these comments! Co-authored-by: Ashwin Maroli --- lib/jekyll-seo-tag/json_ld_drop.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/jekyll-seo-tag/json_ld_drop.rb b/lib/jekyll-seo-tag/json_ld_drop.rb index 2db7231..14f8ba3 100644 --- a/lib/jekyll-seo-tag/json_ld_drop.rb +++ b/lib/jekyll-seo-tag/json_ld_drop.rb @@ -96,7 +96,6 @@ module Jekyll "@graph" => [graph], } if publisher - # .push({"something" => blah}) === << {"something" => blah} updated_graph["@graph"] << publisher end