guard against empty strings not being nil

This commit is contained in:
Ben Balter 2017-05-05 09:24:59 -04:00
parent 96b1fa22c5
commit e68122d8ce
No known key found for this signature in database
GPG Key ID: DBB67C246AD356C4
3 changed files with 67 additions and 5 deletions

View File

@ -31,18 +31,22 @@ module Jekyll
@site_title ||= format_string(site["title"] || site["name"])
end
def site_description
format_string site["description"]
end
# Page title without site title or description appended
def page_title
@page_title ||= format_string(page["title"] || site_title)
@page_title ||= format_string(page["title"]) || site_title
end
# Page title with site title or description appended
def title
@title ||= begin
if page["title"] && site_title
if site_title && page_title != site_title
page_title + TITLE_SEPARATOR + site_title
elsif site["description"] && site_title
site_title + TITLE_SEPARATOR + format_string(site["description"])
elsif site_description && site_title
site_title + TITLE_SEPARATOR + site_description
else
page_title || site_title
end

6
spec/fixtures/_posts/2017-01-03-.md vendored Normal file
View File

@ -0,0 +1,6 @@
---
---
# A post without a title
*See* https://github.com/jekyll/jekyll-seo-tag/commit/4f80ea50b773d1995985e35a8a63806516c353c4#commitcomment-22010563

View File

@ -97,6 +97,40 @@ RSpec.describe Jekyll::SeoTag::Drop do
expect(subject.title).to eql("site title")
end
end
context "without a page or site title" do
let(:page) { make_page }
let(:site) { make_site }
it "returns nil" do
expect(subject.title).to be_nil
end
end
context "with an empty page title" do
let(:page_meta) { { :title => "" } }
it "builds the title" do
expect(subject.title).to eql("site title")
end
end
context "with an empty site title" do
let(:config) { { :title => "" } }
it "builds the title" do
expect(subject.title).to eql("page title")
end
end
context "with an empty page and site title" do
let(:page_meta) { { :title => "" } }
let(:config) { { :title => "" } }
it "returns nil" do
expect(subject.title).to be_nil
end
end
end
end
@ -135,7 +169,25 @@ RSpec.describe Jekyll::SeoTag::Drop do
end
end
context "description" do
context "site description" do
context "with a site description" do
let(:config) { { :description => "site description " } }
it "returns the site discription" do
expect(subject.site_description).to eql("site description")
end
end
context "without a site description" do
let(:site) { make_site }
it "returns nil" do
expect(subject.site_description).to be_nil
end
end
end
context "page description" do
context "with a page description" do
let(:page_meta) { { "description"=> "page description" } }