From e68122d8ce3b0f4f8821d509b7804732fb7fd8b8 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Fri, 5 May 2017 09:24:59 -0400 Subject: [PATCH] guard against empty strings not being nil --- lib/jekyll-seo-tag/drop.rb | 12 ++++--- spec/fixtures/_posts/2017-01-03-.md | 6 ++++ spec/jekyll_seo_tag/drop_spec.rb | 54 ++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 spec/fixtures/_posts/2017-01-03-.md diff --git a/lib/jekyll-seo-tag/drop.rb b/lib/jekyll-seo-tag/drop.rb index 228bc3c..c6f4af6 100644 --- a/lib/jekyll-seo-tag/drop.rb +++ b/lib/jekyll-seo-tag/drop.rb @@ -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 diff --git a/spec/fixtures/_posts/2017-01-03-.md b/spec/fixtures/_posts/2017-01-03-.md new file mode 100644 index 0000000..1325f9b --- /dev/null +++ b/spec/fixtures/_posts/2017-01-03-.md @@ -0,0 +1,6 @@ +--- +--- + +# A post without a title + +*See* https://github.com/jekyll/jekyll-seo-tag/commit/4f80ea50b773d1995985e35a8a63806516c353c4#commitcomment-22010563 diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index af35131..179f5cf 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -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" } }