Merge pull request #52 from benbalter/bb/global-author

Support for global author data
This commit is contained in:
Ben Balter 2016-02-21 14:02:32 -05:00
commit 0210a770dd
3 changed files with 126 additions and 12 deletions

View File

@ -52,12 +52,14 @@ The SEO tag will respect any of the following if included in your site's `_confi
* `title` - Your site's title (e.g., Ben's awesome site, The GitHub Blog, etc.)
* `description` - A short description (e.g., A blog dedicated to reviewing cat gifs)
* `url` - The full URL to your site. Note: `site.github.url` will be used by default.
* `author` - global author information (see below)
* `twitter:username` - The site's Twitter handle. You'll want to describe it like so:
```yml
twitter:
username: benbalter
```
* `facebook:app_id` (A Facebook app ID for Facebook insights), and/or `facebook:publisher` (A Facebook page URL or ID of the publishing entity). You'll want to describe one or both like so:
```yml
@ -65,6 +67,7 @@ The SEO tag will respect any of the following if included in your site's `_confi
app_id: 1234
publisher: 1234
```
* `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`)
@ -77,4 +80,60 @@ 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`)
* `author` - The username of the post, page, or document author
* `author` - Page-, post-, or document-specific author information (see below)
### Author information
Author information is used to propagate the `creator` field of Twitter summary cards. This is should be an author-specific, not site-wide Twitter handle (the site-wide username be stored as `site.twitter.username`).
*TL;DR: In most cases, put `author: [your Twitter handle]` in the document's front matter, for sites with multiple authors. If you need something more complicated, read on.*
There are several ways to convey this author-specific information. Author information is found in the following order of priority:
1. An `author` object, in the documents's front matter, e.g.:
```yml
author:
twitter: benbalter
```
2. An `author` object, in the site's `_config.yml`, e.g.:
```yml
author:
twitter: benbalter
```
3. `site.data.authors[author]`, if an author is specified in the document's front matter, and a corresponding key exists in `site.data.authors`. E.g., you have the following in the document's front matter:
```yml
author: benbalter
```
And you have the following in `_data/authors.yml`:
```yml
benbalter:
picture: /img/benbalter.png
twitter: jekyllrb
potus:
picture: /img/potus.png
twitter: whitehouse
```
In the above example, the author `benbalter`'s Twitter handle will be resolved to `@jekyllrb`. This allows you to centralize author information in a single `_data/authors` file for site with many authors that require more than just the author's username.
*Pro-tip: If `authors` is present in the document's front matter as an array (and `author` is not), the plugin will use the first author listed, as Twitter supports only one author.*
4. An author in the document's front matter (the simplest way), e.g.:
```yml
author: benbalter
```
5. An author in the site's `_config.yml`, e.g.:
```yml
author: benbalter
```

View File

@ -39,13 +39,18 @@
{% assign seo_description = seo_description | markdownify | strip_html | strip_newlines | escape_once %}
{% endif %}
{% if page.author %}
{% assign seo_author_name = page.author.name | default: page.author %}
{% assign seo_author_twitter = page.author.twitter | default: page.author %}
{% endif %}
{% if seo_author_twitter %}
{% assign seo_author_twitter = seo_author_twitter | replace:"@","" | prepend:"@" %}
{% assign seo_author = page.author | default: page.authors[0] | default: site.author %}
{% if seo_author %}
{% if seo_author.twitter %}
{% assign seo_author_twitter = seo_author.twitter %}
{% else %}
{% if site.data.authors and site.data.authors[seo_author] %}
{% assign seo_author_twitter = site.data.authors[seo_author].twitter %}
{% else %}
{% assign seo_author_twitter = seo_author %}
{% endif %}
{% endif %}
{% assign seo_author_twitter = seo_author_twitter | replace:"@","" %}
{% endif %}
{% if seo_title %}
@ -117,7 +122,7 @@
{% endif %}
{% if seo_author_twitter %}
<meta name="twitter:creator" content="{{ seo_author_twitter }}" />
<meta name="twitter:creator" content="@{{ seo_author_twitter }}" />
{% endif %}
{% endif %}

View File

@ -173,7 +173,8 @@ describe Jekyll::SeoTag do
context 'twitter' do
context 'with site.twitter.username' do
let(:site) { make_site('twitter' => { 'username' => 'jekyllrb' }) }
let(:site_twitter) { { 'username' => 'jekyllrb' } }
let(:site) { make_site('twitter' => site_twitter) }
context 'with page.author as a string' do
let(:page) { make_page('author' => 'benbalter') }
@ -185,12 +186,61 @@ describe Jekyll::SeoTag do
expected = %r{<meta name="twitter:creator" content="@benbalter" />}
expect(output).to match(expected)
end
context 'with an @' do
let(:page) { make_page('author' => '@benbalter') }
it 'outputs the twitter card' do
expected = %r{<meta name="twitter:creator" content="@benbalter" />}
expect(output).to match(expected)
end
end
context 'with site.data.authors' do
let(:author_data) { {} }
let(:data) { { 'authors' => author_data } }
let(:site) { make_site('data' => data, 'twitter' => site_twitter) }
context 'with the author in site.data.authors' do
let(:author_data) { { 'benbalter' => { 'twitter' => 'test' } } }
it 'outputs the twitter card' do
expected = %r{<meta name="twitter:creator" content="@test" />}
expect(output).to match(expected)
end
end
context 'without the author in site.data.authors' do
it 'outputs the twitter card' do
expected = %r{<meta name="twitter:creator" content="@benbalter" />}
expect(output).to match(expected)
end
end
end
end
context 'with page.author as an object' do
context 'with page.author as a hash' do
let(:page) { make_page('author' => { 'twitter' => '@test' }) }
it 'supports author data as an object' do
it 'supports author data as a hash' do
expected = %r{<meta name="twitter:creator" content="@test" />}
expect(output).to match(expected)
end
end
context 'with page.authors as an array' do
let(:page) { make_page('authors' => %w(test foo)) }
it 'supports author data as an array' do
expected = %r{<meta name="twitter:creator" content="@test" />}
expect(output).to match(expected)
end
end
context 'with site.author as a hash' do
let(:author) { { 'twitter' => '@test' } }
let(:site) { make_site('author' => author, 'twitter' => site_twitter) }
it 'supports author data as an hash' do
expected = %r{<meta name="twitter:creator" content="@test" />}
expect(output).to match(expected)
end