diff --git a/README.md b/README.md index 96e2042..9a8feb0 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. diff --git a/lib/template.html b/lib/template.html index 45d3aae..a6b3122 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 %} @@ -73,14 +104,6 @@ {% if seo_site_title %} - {% endif %} {% if page.image %} @@ -98,17 +121,6 @@ {% if page.previous.url %} {% endif %} - - {% endif %} {% if site.twitter %} @@ -140,27 +152,44 @@ {% endif %} -{% if site.logo %} - + +{% if seo_name %} + "name" : {{ seo_name | 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 seo_description %} + "description": {{ seo_description | jsonify }}, +{% endif %} + +{% if site.logo %} + "logo": {{ site.logo | prepend: "/" | prepend: seo_url | jsonify }}, +{% endif %} + +{% if seo_links %} + "sameAs" : {{ seo_links | jsonify }}, +{% endif %} + + "url" : "{{ page.url | prepend: seo_url | replace:'/index.html','/' }}" + } + + diff --git a/spec/jekyll_seo_tag_spec.rb b/spec/jekyll_seo_tag_spec.rb index 880a5f8..49c6121 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 @@ -264,11 +261,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(: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