-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
source "https://rubygems.org" | ||
require "json" | ||
require "open-uri" | ||
|
||
gemspec | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
module Jekyll | ||
class SeoTag | ||
class AuthorDrop < Jekyll::Drops::Drop | ||
# A drop representing the current page's author | ||
# | ||
# Author name will be pulled from: | ||
# | ||
# 1. The page's `author` key | ||
# 2. The first author in the page's `authors` key | ||
# 3. The `author` key in the site config | ||
# | ||
# If the result from the name search is a string, we'll also check | ||
# for additional author metadata in `site.data.authors` | ||
def initialize(page: nil, site: nil) | ||
raise ArugementError unless page && site | ||
@mutations = {} | ||
@page = page | ||
@site = site | ||
end | ||
|
||
# Public methods to delegate to keys of the author hash | ||
# Ensures keys will be present when `to_h` is called, even with nil values | ||
DELEGATED_METHODS = %i[name picture].freeze | ||
|
||
DELEGATED_METHODS.each do |meth| | ||
define_method meth do | ||
author_hash[meth.to_s] | ||
end | ||
end | ||
|
||
# AuthorDrop#to_s should return name, allowing the author drop to safely | ||
# replace `page.author`, if necessary, and remain backwards compatible | ||
alias_method :to_s, :name | ||
|
||
def twitter | ||
return @twitter if defined? @twitter | ||
twitter = author_hash["twitter"] || author_hash["name"] | ||
@twitter = twitter.is_a?(String) ? twitter.sub(%r!^@!, "") : nil | ||
end | ||
|
||
private | ||
|
||
attr_reader :page | ||
attr_reader :site | ||
|
||
# Finds the page author in the page.author, page.authors, or site.author | ||
# | ||
# Returns a string or hash representing the author | ||
def resolved_author | ||
return @resolved_author if defined? @resolved_author | ||
sources = [page["author"]] | ||
sources << page["authors"].first if page["authors"].is_a?(Array) | ||
sources << site["author"] | ||
@resolved_author = sources.find { |s| !s.to_s.empty? } | ||
end | ||
|
||
# If resolved_author is a string, attempts to find coresponding author | ||
# metadata in `site.data.authors` | ||
# | ||
# Returns a hash representing additional metadata or an empty hash | ||
def site_data_hash | ||
@site_data_hash ||= begin | ||
return {} unless resolved_author.is_a?(String) | ||
return {} unless site.data["authors"].is_a?(Hash) | ||
author_hash = site.data["authors"][resolved_author] | ||
author_hash.is_a?(Hash) ? author_hash : {} | ||
end | ||
end | ||
|
||
# Returns the normalized author hash representing the page author, | ||
# including site-wide metadata if the author is provided as a string, | ||
# or an empty hash, if the author cannot be resolved | ||
def author_hash | ||
if resolved_author.is_a? Hash | ||
resolved_author | ||
elsif resolved_author.is_a? String | ||
{ "name" => resolved_author }.merge(site_data_hash) | ||
else | ||
{} | ||
end | ||
end | ||
|
||
# Since author_hash is aliased to fallback_data, any values in the hash | ||
# will be exposed via the drop, allowing support for arbitrary metadata | ||
alias_method :fallback_data, :author_hash | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters