diff --git a/.rspec b/.rspec index 8c18f1a..83e16f8 100644 --- a/.rspec +++ b/.rspec @@ -1,2 +1,2 @@ ---format documentation --color +--require spec_helper diff --git a/lib/jekyll-seo-tag/drop.rb b/lib/jekyll-seo-tag/drop.rb index badc82d..b9cf031 100644 --- a/lib/jekyll-seo-tag/drop.rb +++ b/lib/jekyll-seo-tag/drop.rb @@ -18,7 +18,8 @@ module Jekyll # Should the `` tag be generated for this page? def title? - @text !~ %r!title=false!i && title + return false unless title + @text !~ %r!title=false!i end def site_title @@ -33,20 +34,21 @@ module Jekyll # Page title with site title or description appended def title if page["title"] && site_title - format_string(page["title"]) + TITLE_SEPARATOR + site_title + page_title + TITLE_SEPARATOR + site_title elsif site["description"] && site_title site_title + TITLE_SEPARATOR + format_string(site["description"]) else - format_string(page["title"]) || site_title + page_title || site_title end end def name - if page["seo"] && page["seo"]["name"] - format_string page["seo"]["name"] - elsif homepage_or_about? && site["social"] && site["social"]["name"] + return format_string(seo_name) if seo_name + return unless homepage_or_about? + + if site["social"] && site["social"]["name"] format_string site["social"]["name"] - elsif homepage_or_about? && site_title + elsif site_title format_string site_title end end @@ -65,18 +67,13 @@ module Jekyll # If the result from the name search is a string, we'll also check # to see if the author exists in `site.data.authors` def author - author = page["author"] - author = page["authors"][0] if author.to_s.empty? && page["authors"] - author = site["author"] if author.to_s.empty? - return if author.to_s.empty? + return if author_string_or_hash.to_s.empty? - if author.is_a?(String) - author = if site.data["authors"] && site.data["authors"][author] - site.data["authors"][author] - else - { "name" => author } - end - end + author = if author_string_or_hash.is_a?(String) + author_hash(author_string_or_hash) + else + author_string_or_hash + end author["twitter"] ||= author["name"] author["twitter"].delete! "@" @@ -127,7 +124,8 @@ module Jekyll # # The resulting path is always an absolute URL def image - return unless image = page["image"] + image = page["image"] + return unless image image = { "path" => image } if image.is_a?(String) image["path"] ||= image["facebook"] || image["twitter"] @@ -177,6 +175,27 @@ module Jekyll string unless string.empty? end + + def author_string_or_hash + author = page["author"] + author = page["authors"][0] if author.to_s.empty? && page["authors"] + author = site["author"] if author.to_s.empty? + author + end + + def author_hash(author_string) + if site.data["authors"] && site.data["authors"][author_string] + hash = site.data["authors"][author_string] + hash["twitter"] ||= author_string + hash + else + { "name" => author_string } + end + end + + def seo_name + page["seo"] && page["seo"]["name"] + end end end end diff --git a/lib/template.html b/lib/template.html index b28fd7b..d05ce92 100755 --- a/lib/template.html +++ b/lib/template.html @@ -49,7 +49,6 @@ <link rel="next" href="{{ paginator.next_page_path | absolute_url }}"> {% endif %} -{{ seo_tag.author | jsonify }} {% if site.twitter %} {% if seo_tag.image %} <meta name="twitter:card" content="summary_large_image" /> diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb new file mode 100644 index 0000000..ad7e5f6 --- /dev/null +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -0,0 +1,233 @@ +RSpec.describe Jekyll::SeoTag::Drop do + let(:page) { make_page({ "title" => "page title" }) } + let(:site) { make_site({ "title" => "site title" }) } + let(:context) { make_context(:page => page, :site => site) } + let(:text) { "" } + subject { described_class.new(text, context) } + + it "returns the version" do + expect(subject.version).to eql(Jekyll::SeoTag::VERSION) + end + + context "title?" do + it "knows to include the title" do + expect(subject.title?).to be_truthy + end + + context "with title=false" do + let(:text) { "title=false" } + + it "knows not to include the title" do + expect(subject.title?).to be_falsy + end + end + + context "site title" do + it "knows the site title" do + expect(subject.site_title).to eql("site title") + end + + context "with site.name" do + let(:site) { make_site({ "name" => "site title" }) } + + it "knows the site title" do + expect(subject.site_title).to eql("site title") + end + end + end + + context "page title" do + it "knows the page title" do + expect(subject.page_title).to eql("page title") + end + + context "without a page title" do + let(:page) { make_page } + + it "knows the page title" do + expect(subject.page_title).to eql("site title") + end + end + end + + context "title" do + context "with a page and site title" do + it "builds the title" do + expect(subject.title).to eql("page title | site title") + end + end + + context "with a site description but no page title" do + let(:page) { make_page } + let(:site) do + make_site({ "title" => "site title", "description" => "site description" }) + end + + it "builds the title" do + expect(subject.title).to eql("site title | site description") + end + end + + context "with just a page title" do + let(:site) { make_site } + + it "builds the title" do + expect(subject.title).to eql("page title") + end + end + + context "with just a site title" do + let(:page) { make_page } + + it "builds the title" do + expect(subject.title).to eql("site title") + end + end + end + end + + context "name" do + context "with seo.name" do + let(:page) { make_page({ "seo" => { "name" => "seo name" } }) } + + it "uses the seo name" do + expect(subject.name).to eql("seo name") + end + end + + context "the index" do + let(:page) { make_page({ "permalink" => "/" }) } + + context "with site.social.name" do + let(:site) { make_site({ "social" => { "name" => "social name" } }) } + + it "uses site.social.name" do + expect(subject.name).to eql("social name") + end + end + + it "uses the site title" do + expect(subject.name).to eql("site title") + end + end + + context "description" do + context "with a page description" do + let(:page) { make_page({ "description"=> "page description" }) } + + it "uses the page description" do + expect(subject.description).to eql("page description") + end + end + + context "with a page excerpt" do + let(:page) { make_page({ "description"=> "page excerpt" }) } + + it "uses the page description" do + expect(subject.description).to eql("page excerpt") + end + end + + context "with a site description" do + let(:site) { make_site({ "description"=> "site description" }) } + + it "uses the page description" do + expect(subject.description).to eql("site description") + end + end + end + + context "author" do + let(:page_data) { {} } + let(:page) { make_page(page_data) } + let(:data) { {} } + let(:site) do + site = make_site({ "author" => "author" }) + site.data = data + site + end + + %i[with without].each do |site_data_type| + context "#{site_data_type} site.author data" do + let(:data) do + if site_data_type == :with + { + "authors" => { + "author" => { "name" => "Author" }, + }, + } + else + {} + end + end + + { + :string => { "author" => "author" }, + :array => { "authors" => %w(author author2) }, + :empty_string => { "author" => "" }, + :nil => { "author" => nil }, + :hash => { "author" => { "name" => "author" } }, + }.each do |author_type, data| + context "with author as #{author_type}" do + let(:page_data) { data } + + it "returns a hash" do + expect(subject.author).to be_a(Hash) + end + + it "returns the name" do + if site_data_type == :with && author_type != :hash + expect(subject.author["name"]).to eql("Author") + else + expect(subject.author["name"]).to eql("author") + end + end + + it "returns the twitter handle" do + expect(subject.author["twitter"]).to eql("author") + end + end + end + end + end + + context "twitter" do + let(:page_data) { { "author" => "author" } } + + it "pulls the handle from the author" do + expect(subject.author["twitter"]).to eql("author") + end + + context "with an @" do + let(:page_data) do + { + "author" => { + "name" => "author", + "twitter" => "@twitter", + }, + } + end + + it "strips the @" do + expect(subject.author["twitter"]).to eql("twitter") + end + end + + context "with an explicit handle" do + let(:page_data) do + { + "author" => { + "name" => "author", + "twitter" => "twitter", + }, + } + end + + it "pulls the handle from the hash" do + expect(subject.author["twitter"]).to eql("twitter") + end + end + end + end + end +end diff --git a/spec/jekyll_seo_tag_spec.rb b/spec/jekyll_seo_tag_spec.rb index 546c2c2..9f5d052 100755 --- a/spec/jekyll_seo_tag_spec.rb +++ b/spec/jekyll_seo_tag_spec.rb @@ -403,6 +403,7 @@ EOS context "with the author in site.data.authors" do let(:author_data) { { "benbalter" => { "twitter" => "test" } } } + it "outputs the twitter card" do expected = %r!<meta name="twitter:creator" content="@test" />! expect(output).to match(expected)