Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This way to a passing Rich Results logo test #429

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/jekyll-seo-tag/json_ld_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def publisher

output = {
"@type" => "Organization",
"url" => page_drop.canonical_url,
"logo" => {
"@type" => "ImageObject",
"url" => logo,
Expand All @@ -88,7 +89,22 @@ def main_entity
private :main_entity

def to_json
to_h.compact.to_json
graph = to_h.compact

# assign publisher and remove it from the array
publisher = graph["publisher"] || nil
graph.delete("publisher")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could merge these two lines into one:

publisher = graph.delete("publisher")

Copy link
Author

@benjithaimmortal benjithaimmortal Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashmaroli I think I don't know enough about Ruby for this one. Sorry!

publisher should be set to the inner hash graph['publisher'], and not the full graph hash. Does the refactor do that?


updated_graph = {
"@context" => "https://schema.org",
"@graph" => [graph],
}
if publisher
updated_graph["@graph"] << publisher
end

# .to_json for the win
updated_graph.to_json
end

private
Expand Down
35 changes: 18 additions & 17 deletions spec/jekyll_seo_tag_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,23 @@
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

context "with absolute site.logo" 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

context "with site.logo and page.author" 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

Expand All @@ -262,12 +262,12 @@
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

Expand All @@ -276,8 +276,8 @@
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

Expand All @@ -286,8 +286,8 @@
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

Expand Down Expand Up @@ -331,9 +331,9 @@
expected = %r!<meta property="og:type" content="article" />!
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
Expand Down Expand Up @@ -552,17 +552,18 @@
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

context "on about page" 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

Expand Down