Drop#image should return nil if there is no image#path in order to avoid a breaking change

This commit is contained in:
Ben Balter 2017-08-23 12:18:52 -04:00
parent 1081a0ebd9
commit 298d3b03d6
No known key found for this signature in database
GPG Key ID: DBB67C246AD356C4
4 changed files with 19 additions and 7 deletions

View File

@ -130,8 +130,11 @@ module Jekyll
end
end
# Returns a Drop representing the page's image
# Returns nil if the image has no path, to preserve backwards compatability
def image
@image ||= ImageDrop.new(:page => page, :context => @context)
@image if @image.path
end
def page_lang

View File

@ -1,20 +1,19 @@
module Jekyll
class SeoTag
# Returns nil or a hash representing the page image
# The image hash will always contain a path, pulled from:
# A drop representing the page image
# The image path will be pulled from:
#
# 1. The `image` key if it's a string
# 2. The `image.path` key if it's a hash
# 3. The `image.facebook` key
# 4. The `image.twitter` key
#
# The resulting path is always an absolute URL
class ImageDrop < Jekyll::Drops::Drop
include Jekyll::SeoTag::UrlHelper
# Initialize a new ImageDrop
#
# page - The page hash (e.g., Page#to_liquid)
# context - the Liquid::Context
def initialize(page: nil, context: nil)
raise ArgumentError unless page && context
@mutations = {}
@ -22,6 +21,9 @@ module Jekyll
@context = context
end
# Called path for backwards compatability, this is really
# the escaped, absolute URL representing the page's image
# Returns nil if no image path can be determined
def path
@path ||= filters.uri_escape(absolute_url) if absolute_url
end
@ -32,9 +34,10 @@ module Jekyll
attr_accessor :page
attr_accessor :context
# The normalized image hash with a `path` key (which may be nil)
def image_hash
@image_hash ||= if page["image"].is_a?(Hash)
page["image"]
{ "path" => nil }.merge(page["image"])
elsif page["image"].is_a?(String)
{ "path" => page["image"] }
else

View File

@ -1,8 +1,14 @@
module Jekyll
class SeoTag
# Mixin to share common URL-related methods between class
module UrlHelper
private
# Determines if the given string is an absolute URL
#
# Returns true if an absolute URL.
# Retruns false if it's a relative URL
# Returns nil if it is not a string or can't be parsed as a URL
def absolute_url?(string)
return unless string
Addressable::URI.parse(string).absolute?

View File

@ -27,7 +27,7 @@
<meta property="og:site_name" content="{{ seo_tag.site_title }}" />
{% endif %}
{% if seo_tag.image.path %}
{% if seo_tag.image %}
<meta property="og:image" content="{{ seo_tag.image.path }}" />
{% if seo_tag.image.height %}
<meta property="og:image:height" content="{{ seo_tag.image.height }}" />
@ -50,7 +50,7 @@
{% endif %}
{% if site.twitter %}
{% if seo_tag.image.path %}
{% if seo_tag.image %}
<meta name="twitter:card" content="summary_large_image" />
{% else %}
<meta name="twitter:card" content="summary" />