Skip to content

Commit

Permalink
Merge pull request #206 from jekyll/guard-against-empty-strings
Browse files Browse the repository at this point in the history
Guard against empty title or description strings
  • Loading branch information
benbalter authored May 5, 2017
2 parents 96b1fa2 + 19c6d2a commit f2e788e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
20 changes: 12 additions & 8 deletions lib/jekyll-seo-tag/drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ def site_title
@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
Expand All @@ -58,14 +62,14 @@ def name
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
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/_posts/2017-01-03-.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
---

# A post without a title

*See* https://github.com/jekyll/jekyll-seo-tag/commit/4f80ea50b773d1995985e35a8a63806516c353c4#commitcomment-22010563
54 changes: 53 additions & 1 deletion spec/jekyll_seo_tag/drop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@
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

Expand Down Expand Up @@ -135,7 +169,25 @@
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" } }

Expand Down

0 comments on commit f2e788e

Please # to comment.