Merge pull request #211 from 8parth/master

use canonical_url specified in page if present
This commit is contained in:
Ben Balter 2017-05-25 09:38:33 -04:00 committed by GitHub
commit 445339df18
3 changed files with 51 additions and 1 deletions

View File

@ -224,3 +224,30 @@ defaults:
### SmartyPants Titles
Titles will be processed using [Jekyll's `smartify` filter](https://jekyllrb.com/docs/templates/). This will use SmartyPants to translate plain ASCII punctuation into "smart" typographic punctuation. This will not render or strip any Markdown you may be using in a page title.
### Setting customized Canonical URL
You can set custom Canonical URL for a page by specifying canonical_url option in page front-matter.
E.g., you have the following in the page's front matter:
```yml
layout: post
title: Title of Your Post
canonical_url: 'https://github.com/jekyll/jekyll-seo-tag/'
```
Which will generate canonical_url with specified link in canonical_url.
```html
<link rel="canonical" href="https://github.com/jekyll/jekyll-seo-tag/" />
```
If no canonical_url option was specified, then uses page url for generating canonical_url.
E.g., you have not specified canonical_url in front-matter:
```yml
layout: post
title: Title of Your Post
```
Which will generate following canonical_url:
```html
<link rel="canonical" href="http://yoursite.com/title-of-your-post" />
```

View File

@ -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"].to_s.empty?
filters.absolute_url(page["url"]).to_s.gsub(%r!/index\.html$!, "/")
else
page["canonical_url"]
end
end
end

View File

@ -624,4 +624,23 @@ RSpec.describe Jekyll::SeoTag::Drop do
end
end
end
context "canonical url" do
let(:config) { { :url => "http://example.com" } }
context "when canonical url is specified for a page" do
let(:canonical_url) { "https://github.com/jekyll/jekyll-seo-tag/" }
let(:page_meta) { { "title" => "page title", "canonical_url" => canonical_url } }
it "uses specified canonical url" do
expect(subject.canonical_url).to eq(canonical_url)
end
end
context "when canonical url is not specified for a page" do
it "uses site specific canonical url" do
expect(subject.canonical_url).to eq("http://example.com/page.html")
end
end
end
end