From d0160082cc3d3d12dbd6001e1798e0325448519c Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Mon, 25 Apr 2016 08:15:32 -0700 Subject: [PATCH] Add support for page images with absolute URLs --- README.md | 4 ++-- lib/template.html | 13 +++++++++++-- spec/jekyll_seo_tag_spec.rb | 23 ++++++++++++++++++++--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 226a6bf..132b651 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ The SEO tag will respect any of the following if included in your site's `_confi publisher: 1234 ``` -* `logo` - Relative URL to a site-wide logo (e.g., `/assets/your-company-logo.png`) +* `logo` - 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: * `name` - If the user or organization name differs from the site's name * `links` - An array of links to social media profiles. @@ -78,7 +78,7 @@ The SEO tag will respect the following YAML front matter if included in a post, * `title` - The title of the post, page, or document * `description` - A short description of the page's content -* `image` - Relative URL to an image associated with the post, page, or document (e.g., `/assets/page-pic.jpg`) +* `image` - URL to an image associated with the post, page, or document (e.g., `/assets/page-pic.jpg`) * `author` - Page-, post-, or document-specific author information (see below) ## Advanced usage diff --git a/lib/template.html b/lib/template.html index 809c8c6..9a625a7 100644 --- a/lib/template.html +++ b/lib/template.html @@ -85,11 +85,20 @@ {% endif %} {% if site.logo %} - {% assign seo_site_logo = site.logo | prepend: seo_url | escape %} + {% assign seo_site_logo = site.logo %} + {% unless seo_site_logo contains "://" %} + {% assign seo_site_logo = seo_site_logo | prepend: seo_url %} + {% endunless %} + {% assign seo_site_logo = seo_site_logo | escape %} {% endif %} {% if page.image %} - {% assign seo_page_image = page.image.path | default: page.image.facebook | default: page.image | prepend: seo_url | escape %} + {% assign seo_page_image = page.image.path | default: page.image.facebook | +default: page.image %} + {% unless seo_page_image contains "://" %} + {% assign seo_page_image = seo_page_image | prepend: seo_url %} + {% endunless %} + {% assign seo_page_image = seo_page_image | escape %} {% endif %} {% if seo_tag.title and seo_title %} diff --git a/spec/jekyll_seo_tag_spec.rb b/spec/jekyll_seo_tag_spec.rb index efb123e..355edc8 100644 --- a/spec/jekyll_seo_tag_spec.rb +++ b/spec/jekyll_seo_tag_spec.rb @@ -110,12 +110,21 @@ describe Jekyll::SeoTag do end end - context 'with page.image as a string' do + context 'with relative page.image as a string' do let(:page) { make_page('image' => '/img/foo.png') } it 'outputs the image' do - expected = %r{} - expect(output).to match(expected) + expected = '' + expect(output).to include(expected) + end + end + + context 'with absolute page.image' do + let(:page) { make_page('image' => 'http://cdn.example.invalid/img/foo.png') } + + it 'outputs the image' do + expected = '' + expect(output).to include(expected) end end @@ -168,6 +177,14 @@ describe Jekyll::SeoTag do 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['logo']).to eql('http://cdn.example.invalid/logo.png') + end + end + context 'with site.title' do let(:site) { make_site('title' => 'Foo', 'url' => 'http://example.invalid') }