convert JSON LD to a drop

This commit is contained in:
Ben Balter 2017-08-23 16:11:55 -04:00
parent 298d3b03d6
commit 5de6cd7f2c
No known key found for this signature in database
GPG Key ID: DBB67C246AD356C4
6 changed files with 108 additions and 72 deletions

View File

@ -3,12 +3,13 @@ require "jekyll-seo-tag/version"
module Jekyll
class SeoTag < Liquid::Tag
autoload :JSONLD, "jekyll-seo-tag/json_ld"
autoload :JSONLD, "jekyll-seo-tag/json_ld"
autoload :AuthorDrop, "jekyll-seo-tag/author_drop"
autoload :ImageDrop, "jekyll-seo-tag/image_drop"
autoload :UrlHelper, "jekyll-seo-tag/url_helper"
autoload :Drop, "jekyll-seo-tag/drop"
autoload :Filters, "jekyll-seo-tag/filters"
autoload :ImageDrop, "jekyll-seo-tag/image_drop"
autoload :JSONLDDrop, "jekyll-seo-tag/jason_ld_drop"
autoload :UrlHelper, "jekyll-seo-tag/url_helper"
autoload :Drop, "jekyll-seo-tag/drop"
autoload :Filters, "jekyll-seo-tag/filters"
attr_accessor :context

View File

@ -1,7 +1,6 @@
module Jekyll
class SeoTag
class Drop < Jekyll::Drops::Drop
include Jekyll::SeoTag::JSONLD
include Jekyll::SeoTag::UrlHelper
TITLE_SEPARATOR = " | ".freeze
@ -78,6 +77,18 @@ module Jekyll
@author ||= AuthorDrop.new(:page => page, :site => site)
end
# A drop representing the JSON-LD output
def json_ld
@json_ld ||= JSONLDDrop.new(self)
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 date_modified
@date_modified ||= begin
date = if page_seo["date_modified"]
@ -130,13 +141,6 @@ 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
@page_lang ||= page["lang"] || site["lang"] || "en_US"
end

View File

@ -0,0 +1,79 @@
module Jekyll
class SeoTag
class JSONLDDrop < Jekyll::Drops::Drop
extend Forwardable
def_delegator :page_drop, :name, :name
def_delegator :page_drop, :description, :description
def_delegator :page_drop, :canonical_url, :url
def_delegator :page_drop, :page_title, :headline
def_delegator :page_drop, :date_modified, :dateModified
def_delegator :page_drop, :date_published, :datePublished
def_delegator :page_drop, :links, :sameAs
def_delegator :page_drop, :logo, :logo
# Expose #type and #logo and private methods and #@type as a public method
def_delegator :page_drop, :type, :type
alias_method :"@type", :type
private :type
private :logo
# page_drop should be an instance of Jekyll::SeoTag::Drop
def initialize(page_drop)
@mutations = {}
@page_drop = page_drop
end
def fallback_data
{
"@context" => "http://schema.org",
}
end
def author
return unless page_drop.author["name"]
{
"@type" => "Person",
"name" => page_drop.author["name"],
}
end
def image
return unless page_drop.image
return page_drop.image.path if page_drop.image.keys.length == 1
hash = page_drop.image.to_h
hash["url"] = hash.delete("path")
hash["@type"] = "imageObject"
hash
end
def publisher
return unless logo
output = {
"@type" => "Organization",
"logo" => {
"@type" => "ImageObject",
"url" => logo,
},
}
output["name"] = page_drop.author.name if page_drop.author.name
output
end
def main_entity
return unless %w(BlogPosting CreativeWork).include?(type)
{
"@type" => "WebPage",
"@id" => page_drop.canonical_url,
}
end
alias_method :mainEntityOfPage, :main_entity
private :main_entity
private
attr_reader :page_drop
end
end
end

View File

@ -1,8 +1,7 @@
module Jekyll
class SeoTag
module JSONLD
# A hash of instance methods => key in resulting JSON-LD hash
# Unused, but here to preserve backwards compatability
METHODS_KEYS = {
:json_context => "@context",
:type => "@type",
@ -18,62 +17,6 @@ module Jekyll
:links => "sameAs",
:canonical_url => "url",
}.freeze
def json_ld
@json_ld ||= begin
output = {}
METHODS_KEYS.each do |method, key|
value = send(method)
output[key] = value unless value.nil?
end
output
end
end
private
def json_context
"http://schema.org"
end
def json_author
return unless author
{
"@type" => "Person",
"name" => author["name"],
}
end
def json_image
return unless image
return image["path"] if image.keys.length == 1
hash = image.to_h
hash["url"] = hash.delete("path")
hash["@type"] = "imageObject"
hash
end
def publisher
return unless logo
output = {
"@type" => "Organization",
"logo" => {
"@type" => "ImageObject",
"url" => logo,
},
}
output["name"] = author["name"] if author
output
end
def main_entity
return unless %w(BlogPosting CreativeWork).include?(type)
{
"@type" => "WebPage",
"@id" => canonical_url,
}
end
end
end
end

View File

@ -330,7 +330,7 @@ EOS
it "minifies JSON-LD" do
expected = <<-EOS.strip
{"@context":"http://schema.org","@type":"BlogPosting","headline":"post",
{"name":null,"description":"description","author":null,"@type":"BlogPosting"
EOS
expect(output).to match(expected)
end

View File

@ -3,6 +3,15 @@ require "jekyll"
require "jekyll-seo-tag"
require "html-proofer"
# Monkey patch Jekyll::Drops::Drop so Rspec's `have_key` works as expected
module Jekyll
module Drops
class Drop
alias_method :has_key?, :key?
end
end
end
ENV["JEKYLL_LOG_LEVEL"] = "error"
def dest_dir