From a9c749931992b65f6d17735372db34b44c35f5eb Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Mon, 22 Feb 2016 10:35:55 -0800 Subject: [PATCH 1/6] Consolidate JSON-LD blocks --- lib/template.html | 86 ++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/lib/template.html b/lib/template.html index e21d56b..7768721 100644 --- a/lib/template.html +++ b/lib/template.html @@ -73,14 +73,6 @@ {% if seo_site_title %} - {% endif %} {% if page.image %} @@ -98,17 +90,6 @@ {% if page.previous.url %} {% endif %} - - {% endif %} {% if site.twitter %} @@ -140,27 +121,56 @@ {% endif %} -{% if site.logo %} - + +{% if page.seo and page.seo.name %} + "name" : {{ page.seo.name | jsonify }}, +{% elsif page.url == "/" and site.social and site.social.name %} + "name" : {{ site.social.name | jsonify }}, +{% elsif page.url == "/" and seo_site_title %} + "name" : {{ seo_site_title | jsonify }}, {% endif %} +{% if seo_page_title %} + "headline": {{ seo_page_title | jsonify }}, +{% endif %} + +{% if page.image %} + "image": {{ page.image | jsonify }}, +{% endif %} + +{% if page.date %} + "datePublished": {{ page.date | date_to_xmlschema | jsonify }}, +{% endif %} + +{% if page.description %} + "description": {{ page.description | jsonify }}, +{% endif %} + +{% if site.logo %} + "logo": {{ site.logo | prepend: "/" | prepend: seo_url | jsonify }}, +{% endif %} + +{% if page.seo and page.seo.links %} + "sameAs" : {{ page.seo.links | jsonify }}, +{% elsif page.url == "/" and site.social and site.social.links %} + "sameAs" : {{ site.social.links | jsonify }}, +{% endif %} + + "url" : "{{ page.url | prepend: seo_url | replace:'/index.html','/' }}" + } + + From c51ef4db836be54c9aef9f7cff0fb75a8c5e9b30 Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Mon, 22 Feb 2016 10:36:46 -0800 Subject: [PATCH 2/6] Fix JSON-LD tests There is no reason to test for `site` variables each time we check JSON-LD. Let's only test what we say we are going to test at each stage. --- spec/jekyll_seo_tag_spec.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/spec/jekyll_seo_tag_spec.rb b/spec/jekyll_seo_tag_spec.rb index 1753804..cef607e 100644 --- a/spec/jekyll_seo_tag_spec.rb +++ b/spec/jekyll_seo_tag_spec.rb @@ -124,7 +124,6 @@ describe Jekyll::SeoTag do it 'outputs the logo' do expect(json_data['logo']).to eql('http://example.invalid/logo.png') - expect(json_data['url']).to eql('http://example.invalid') end end @@ -133,8 +132,6 @@ describe Jekyll::SeoTag do it 'outputs the site title meta' do expect(output).to match(%r{}) - expect(json_data['name']).to eql('Foo') - expect(json_data['url']).to eql('http://example.invalid') end end end @@ -254,6 +251,15 @@ describe Jekyll::SeoTag do let(:links) { ['http://foo.invalid', 'http://bar.invalid'] } let(:social_namespace) { { 'name' => 'Ben', 'links' => links } } let(:site) { make_site('social' => social_namespace) } + let(:meta) do + { + 'permalink' => '/', + 'seo' => { + 'type' => 'person' + } + } + end + let(:page) { make_post(meta) } it 'outputs social meta' do expect(json_data['@type']).to eql('person') From 09924c03ffd49f51ed74717ceff3be7c0a3023d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Garc=C3=A9s?= Date: Mon, 22 Feb 2016 10:37:50 -0800 Subject: [PATCH 3/6] Use seo_description instead of page.description seo_description is used in the meta tags but not in the ld+json If page.description is not set, the the description at ld+json will be null. --- lib/template.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/template.html b/lib/template.html index 7768721..dca838e 100644 --- a/lib/template.html +++ b/lib/template.html @@ -155,8 +155,8 @@ "datePublished": {{ page.date | date_to_xmlschema | jsonify }}, {% endif %} -{% if page.description %} - "description": {{ page.description | jsonify }}, +{% if seo_description %} + "description": {{ seo_description | jsonify }}, {% endif %} {% if site.logo %} From f193c6fb3926e6d93403cb76cdd295446250f27e Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Mon, 22 Feb 2016 11:22:34 -0800 Subject: [PATCH 4/6] Document advanced options --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f713ce8..242e1e6 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,6 @@ The SEO tag will respect any of the following if included in your site's `_confi * `logo` - Relative URL to a site-wide logo (e.g., `assets/your-company-logo.png`) * `social` - For [specifying social profiles](https://developers.google.com/structured-data/customize/social-profiles). The following properties are available: - * `type` - Either `person` or `organization` (defaults to `person`) * `name` - If the user or organization name differs from the site's name * `links` - An array of links to social media profiles. * `google_site_verification` for verifying ownership via Google webmaster tools @@ -145,3 +144,12 @@ There are several ways to convey this author-specific information. Author inform ```yml author: benbalter ``` + +### Customizing JSON-LD output + +The following options can be set for any particular page. While the default options are meant to serve most users in the most common circumstances, there may be situations where more precise control is necessary. + +* `seo` + * `name` - If the name of the thing that the page represents is different from the page title. (i.e.: "Frank's Café" vs "Welcome to Frank's Café") + * `type` - The type of things that the page represents. This must be a [Schema.org type](http://schema.org/docs/schemas.html), and will probably usually be something like [`BlogPosting`](http://schema.org/BlogPosting), [`NewsArticle`](http://schema.org/NewsArticle), [`Person`](http://schema.org/Person), [`Organization`](http://schema.org/Organization), etc. + * `links` - An array of other URLs that represent the same thing that this page represents. For instance, Jane's bio page might include links to Jane's GitHub and Twitter profiles. From f3e0e78993940ddddc00492fedb5240d519e0710 Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Mon, 22 Feb 2016 18:13:43 -0800 Subject: [PATCH 5/6] Include sameAs links on About page --- lib/template.html | 2 +- spec/jekyll_seo_tag_spec.rb | 43 ++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/template.html b/lib/template.html index dca838e..fb14581 100644 --- a/lib/template.html +++ b/lib/template.html @@ -165,7 +165,7 @@ {% if page.seo and page.seo.links %} "sameAs" : {{ page.seo.links | jsonify }}, -{% elsif page.url == "/" and site.social and site.social.links %} +{% elsif (page.url == "/" or page.url == "/about/") and site.social and site.social.links %} "sameAs" : {{ site.social.links | jsonify }}, {% endif %} diff --git a/spec/jekyll_seo_tag_spec.rb b/spec/jekyll_seo_tag_spec.rb index cef607e..301d024 100644 --- a/spec/jekyll_seo_tag_spec.rb +++ b/spec/jekyll_seo_tag_spec.rb @@ -251,20 +251,39 @@ describe Jekyll::SeoTag do let(:links) { ['http://foo.invalid', 'http://bar.invalid'] } let(:social_namespace) { { 'name' => 'Ben', 'links' => links } } let(:site) { make_site('social' => social_namespace) } - let(:meta) do - { - 'permalink' => '/', - 'seo' => { - 'type' => 'person' - } - } - end let(:page) { make_post(meta) } - 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) + context 'on homepage' do + let(:meta) do + { + 'permalink' => '/', + 'seo' => { + 'type' => 'person' + } + } + 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) + end + end + + context 'on about page' do + let(:meta) { { 'permalink' => '/about/' } } + + it 'outputs sameAs links' do + expect(json_data['sameAs']).to eql(links) + end + end + + context 'on other pages' do + let(:meta) { { 'permalink' => '/2/' } } + + it 'does not output sameAs links' do + expect(json_data['sameAs']).to be_nil + end end end From 11dea25ac8ff961118e2f2f350e71d6add6297ff Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Mon, 22 Feb 2016 18:31:59 -0800 Subject: [PATCH 6/6] DRY up JSON-LD --- lib/template.html | 55 +++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/lib/template.html b/lib/template.html index fb14581..cc50943 100644 --- a/lib/template.html +++ b/lib/template.html @@ -1,5 +1,9 @@ +{% if page.url == "/" or page.url == "/about/" %} + {% assign seo_homepage_or_about = true %} +{% endif %} + {% if site.url %} {% assign seo_url = site.url | append: site.baseurl %} {% endif %} @@ -22,6 +26,17 @@ {% endif %} {% endif %} +{% if page.seo and page.seo.name %} + {% assign seo_name = page.seo.name %} +{% elsif seo_homepage_or_about and site.social and site.social.name %} + {% assign seo_name = site.social.name %} +{% elsif seo_homepage_or_about and seo_site_title %} + {% assign seo_name = seo_site_title %} +{% endif %} +{% if seo_name %} + {% assign seo_name = seo_name | markdownify | strip_html | strip_newlines | escape_once %} +{% endif %} + {% if seo_title %} {% assign seo_title = seo_title | markdownify | strip_html | strip_newlines | escape_once %} {% endif %} @@ -53,6 +68,22 @@ {% assign seo_author_twitter = seo_author_twitter | replace:"@","" %} {% endif %} +{% if page.seo and page.seo.type %} + {% assign seo_type = page.seo.type %} +{% elsif seo_homepage_or_about %} + {% assign seo_type = "WebSite" %} +{% elsif page.date %} + {% assign seo_type = "BlogPosting" %} +{% else %} + {% assign seo_type = "WebPage" %} +{% endif %} + +{% if page.seo and page.seo.links %} + {% assign seo_links = page.seo.links %} +{% elsif seo_homepage_or_about and site.social and site.social.links %} + {% assign seo_links = site.social.links %} +{% endif %} + {% if seo_tag.title and seo_title %} {{ seo_title }} {% endif %} @@ -125,22 +156,12 @@ { "@context" : "http://schema.org", -{% if page.seo and page.seo.type %} - "@type" : {{ page.seo.type | jsonify }}, -{% elsif page.url == "/" %} - "@type" : "WebSite", -{% elsif page.date %} - "@type" : "BlogPosting", -{% else %} - "@type" : "WebPage", +{% if seo_type %} + "@type" : {{ seo_type | jsonify }}, {% endif %} -{% if page.seo and page.seo.name %} - "name" : {{ page.seo.name | jsonify }}, -{% elsif page.url == "/" and site.social and site.social.name %} - "name" : {{ site.social.name | jsonify }}, -{% elsif page.url == "/" and seo_site_title %} - "name" : {{ seo_site_title | jsonify }}, +{% if seo_name %} + "name" : {{ seo_name | jsonify }}, {% endif %} {% if seo_page_title %} @@ -163,10 +184,8 @@ "logo": {{ site.logo | prepend: "/" | prepend: seo_url | jsonify }}, {% endif %} -{% if page.seo and page.seo.links %} - "sameAs" : {{ page.seo.links | jsonify }}, -{% elsif (page.url == "/" or page.url == "/about/") and site.social and site.social.links %} - "sameAs" : {{ site.social.links | jsonify }}, +{% if seo_links %} + "sameAs" : {{ seo_links | jsonify }}, {% endif %} "url" : "{{ page.url | prepend: seo_url | replace:'/index.html','/' }}"