diff --git a/lib/jekyll-seo-tag/drop.rb b/lib/jekyll-seo-tag/drop.rb index 831cb18..7656b06 100644 --- a/lib/jekyll-seo-tag/drop.rb +++ b/lib/jekyll-seo-tag/drop.rb @@ -183,7 +183,11 @@ module Jekyll def canonical_url @canonical_url ||= begin - filters.absolute_url(page["url"]).to_s.gsub(%r!/index\.html$!, "/") + if page["canonical_url"].present? + page["canonical_url"] + else + filters.absolute_url(page["url"]).to_s.gsub(%r!/index\.html$!, "/") + end end end diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index 179f5cf..2e32449 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -624,4 +624,23 @@ RSpec.describe Jekyll::SeoTag::Drop do end end end + + context "canonical url" do + context "when canonical url is specified for a page" do + let(:page) { make_page( { "title" => "page title", "canonical_url" => "https://github.com/jekyll/jekyll-seo-tag/"} ) } + let(:canonical_url) { 'https://github.com/jekyll/jekyll-seo-tag/' } + it "uses specified canonical url" do + puts subject.canonical_url + expect(subject.canonical_url).to eq(canonical_url) + end + end + + context "when canonical url is not specified for a page" do + let(:canonical_url) { 'https://github.com/jekyll/jekyll-seo-tag/' } + it "uses site specific canonical url" do + expect(subject.canonical_url).not_to eq(canonical_url) + end + end + end + end