Skip to content

Commit 5a2e1fd

Browse files
authored
feat: Add canonical=false option to disable canonical URL output (#521)
Merge pull request 521
1 parent 96e86da commit 5a2e1fd

5 files changed

Lines changed: 56 additions & 2 deletions

File tree

docs/advanced-usage.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ If for some reason, you don't want the plugin to output `<title>` tags on each p
1212
```
1313
<!-- {% endraw %} -->
1414

15+
### Disabling `<link rel="canonical">` output
16+
17+
If you're using another plugin to generate canonical URLs (such as [jekyll-polyglot](https://github.com/untra/polyglot) for multilingual sites), you can disable the canonical URL output:
18+
19+
<!-- {% raw %} -->
20+
```
21+
{% seo canonical=false %}
22+
```
23+
<!-- {% endraw %} -->
24+
25+
This will prevent the plugin from outputting the `<link rel="canonical">` tag, while still outputting all other SEO tags including the `<meta property="og:url">` tag.
26+
1527
### Author information
1628

1729
Author information is used to propagate the `creator` field of Twitter summary cards. This should be an author-specific, not site-wide Twitter handle (the site-wide username be stored as `site.twitter.username`).

lib/jekyll-seo-tag/drop.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ def title?
3333
@display_title = (@text !~ %r!title=false!i)
3434
end
3535

36+
# Should the `<link rel="canonical">` tag be generated for this page?
37+
def canonical?
38+
return @canonical if defined?(@canonical)
39+
40+
@canonical = (@text !~ %r!canonical=false!i)
41+
end
42+
3643
def site_title
3744
@site_title ||= format_string(site["title"] || site["name"])
3845
end

lib/template.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
{% endif %}
2222

2323
{% if site.url %}
24-
<link rel="canonical" href="{{ seo_tag.canonical_url }}" />
25-
<meta property="og:url" content="{{ seo_tag.canonical_url }}" />
24+
{% if seo_tag.canonical? %}<link rel="canonical" href="{{ seo_tag.canonical_url }}" />
25+
{% endif %}<meta property="og:url" content="{{ seo_tag.canonical_url }}" />
2626
{% endif %}
2727

2828
{% if seo_tag.site_title %}

spec/jekyll_seo_tag/drop_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,28 @@
535535
end
536536
end
537537

538+
context "canonical?" do
539+
it "knows to include the canonical by default" do
540+
expect(subject.canonical?).to be_truthy
541+
end
542+
543+
context "with canonical=false" do
544+
let(:text) { "canonical=false" }
545+
546+
it "knows not to include the canonical" do
547+
expect(subject.canonical?).to be_falsy
548+
end
549+
end
550+
551+
context "with CANONICAL=FALSE (case insensitive)" do
552+
let(:text) { "CANONICAL=FALSE" }
553+
554+
it "knows not to include the canonical" do
555+
expect(subject.canonical?).to be_falsy
556+
end
557+
end
558+
end
559+
538560
context "pagination" do
539561
let(:context) do
540562
make_context(

spec/jekyll_seo_tag_integration_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,19 @@
622622
end
623623
end
624624

625+
context "with canonical=false" do
626+
let(:site) { make_site("url" => "http://example.invalid") }
627+
let(:text) { "canonical=false" }
628+
629+
it "does not output a canonical link tag" do
630+
expect(output).not_to match(%r!<link rel="canonical"!)
631+
end
632+
633+
it "still outputs og:url meta tag" do
634+
expect(output).to match(%r!<meta property="og:url"!)
635+
end
636+
end
637+
625638
context "with pagination" do
626639
let(:context) { make_context({}, "paginator" => paginator) }
627640

0 commit comments

Comments
 (0)