Merge pull request #54 from benbalter/bb/title-optional

Make <title> Optional
This commit is contained in:
Ben Balter 2016-02-21 14:33:33 -05:00
commit d688aa049a
4 changed files with 45 additions and 13 deletions

View File

@ -82,6 +82,14 @@ The SEO tag will respect the following YAML front matter if included in a post,
* `image` - Relative 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)
### Disabling `<title>` output
Jekyll SEO Tag is designed to implement SEO best practices by default. If for some reason, you don't want the plugin to output `<title>` tags on each page, simply invoke the plugin within your template like so:
```
{% seo title=false %}
```
### 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`).

View File

@ -6,23 +6,37 @@ module Jekyll
MINIFY_REGEX = /(>\n|[%}]})\s+(<|{[{%])/
def initialize(_tag_name, text, _tokens)
super
@text = text
end
def render(context)
@context = context
output = template.render!(payload, info)
output
template.render!(payload, info)
end
private
def options
{
'version' => VERSION,
'title' => title?
}
end
def payload
{
'seo_tag' => { 'version' => VERSION },
'page' => context.registers[:page],
'site' => context.registers[:site].site_payload['site']
'site' => context.registers[:site].site_payload['site'],
'seo_tag' => options
}
end
def title?
!(@text =~ /title=false/i)
end
def info
{
registers: context.registers,

View File

@ -53,7 +53,7 @@
{% assign seo_author_twitter = seo_author_twitter | replace:"@","" %}
{% endif %}
{% if seo_title %}
{% if seo_tag.title and seo_title %}
<title>{{ seo_title }}</title>
{% endif %}

View File

@ -1,13 +1,14 @@
require 'spec_helper'
describe Jekyll::SeoTag do
subject { Jekyll::SeoTag.parse('seo', nil, nil, nil) }
let(:page) { make_page }
let(:site) { make_site }
let(:post) { make_post }
let(:context) { make_context(page: page, site: site) }
let(:output) { subject.render(context) }
let(:json) { output.match(%r{<script type=\"application/ld\+json\">(.*)</script>}m)[1] }
let(:page) { make_page }
let(:site) { make_site }
let(:post) { make_post }
let(:context) { make_context(page: page, site: site) }
let(:tag) { 'seo' }
let(:text) { '' }
let(:output) { Liquid::Template.parse("{% #{tag} #{text} %}").render!(context, {}) }
let(:json) { output.match(%r{<script type=\"application/ld\+json\">(.*)</script>}m)[1] }
let(:json_data) { JSON.parse(json) }
before do
@ -100,6 +101,7 @@ describe Jekyll::SeoTag do
context 'with site.baseurl' do
let(:site) { make_site('url' => 'http://example.invalid', 'baseurl' => '/foo') }
it 'uses baseurl to build the seo url' do
expected = %r{<link rel="canonical" href="http://example.invalid/foo/page.html" />}
expect(output).to match(expected)
@ -278,6 +280,14 @@ describe Jekyll::SeoTag do
end
end
context 'with title=false' do
let(:text) { 'title=false' }
it 'does not output a <title> tag' do
expect(output).not_to match(/<title>/)
end
end
it 'outputs valid HTML' do
site.process
options = {