Merge pull request #206 from jekyll/guard-against-empty-strings

Guard against empty title or description strings
This commit is contained in:
Ben Balter 2017-05-05 14:22:52 -04:00 committed by GitHub
commit f2e788e0f1
3 changed files with 71 additions and 9 deletions

View File

@ -31,18 +31,22 @@ module Jekyll
@site_title ||= format_string(site["title"] || site["name"])
end
def site_description
@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
@ -58,14 +62,14 @@ module Jekyll
elsif site_social["name"]
format_string site_social["name"]
elsif site_title
format_string site_title
site_title
end
end
def description
@description ||= format_string(
page["description"] || page["excerpt"] || site["description"]
)
@description ||= begin
format_string(page["description"] || page["excerpt"]) || site_description
end
end
# Returns a nil or a hash representing the author

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" } }