Skip to content

Commit 31cfaad

Browse files
committed
Allow removing site title from page title
1 parent 8403350 commit 31cfaad

3 files changed

Lines changed: 69 additions & 17 deletions

File tree

docs/advanced-usage.md

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22

33
Jekyll SEO Tag is designed to implement SEO best practices by default and to be the right fit for most sites right out of the box. If for some reason, you need more control over the output, read on:
44

5-
### Disabling `<title>` output
6-
7-
If for some reason, you don't want the plugin to output `<title>` tags on each page, simply invoke the plugin within your template like so:
8-
9-
<!-- {% raw %} -->
10-
```
11-
{% seo title=false %}
12-
```
13-
<!-- {% endraw %} -->
14-
155
### Disabling `<link rel="canonical">` output
166

177
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:
@@ -188,7 +178,42 @@ Which will generate following canonical_url:
188178
<link rel="canonical" href="https://example.com/title-of-your-post" />
189179
```
190180

191-
### Customizing title modifier for paginated pages
181+
### Customizing title
182+
Jekyll SEO tag outputs the `<title>` tag and automatically adds several
183+
important details such as the site title, page number for paginated
184+
pages, and can add the page category if applicable.
185+
186+
#### Disabling `<title>` output
187+
188+
If for some reason, you don't want the plugin to output `<title>` tags on each page, simply invoke the plugin within your template like so:
189+
190+
```
191+
{% seo title=false %}
192+
```
193+
194+
#### Removing site title from the page title
195+
The site title is appended to the majority as: `Page title | Site title`.
196+
197+
You can configure the site title by setting either `title` or `name` in `_config.yml`.
198+
```yml
199+
title: Site title
200+
```
201+
202+
Site title is not added on the "home" and "about" pages. Those pages
203+
are identified by the page's `url`.
204+
205+
You can override it by adding `homepage_or_about` to the page's front matter:
206+
207+
```yml
208+
---
209+
homepage_or_about: false
210+
---
211+
```
212+
213+
Setting `homepage_or_about: false` prevents the site name
214+
from being added the page title.
215+
216+
#### Customizing title modifier for paginated pages
192217

193218
You can override the default title modifier for paginated pages from `Page %{current} of %{total} for ` to a string of your
194219
choice by setting a `seo_paginator_message` key in your `_config.yml`.
@@ -202,7 +227,7 @@ seo_paginator_message: "%<current>s / %<total>s | "
202227
While the value can be any string text, we recommend using a Ruby string-template containing the variables `current` and `total`
203228
similar to the example above, to incorporate the current page-number and total number of paginated pages in the title.
204229

205-
### Adding a page title category
230+
#### Adding a page title category
206231

207232
You can optionally add a page category to its title.
208233
This is useful for indicating to the users that some pages are logically grouped or are of a specific kind.

lib/jekyll-seo-tag/drop.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ def site_tagline_or_description
7070
end
7171

7272
# Page title with site title or description appended
73-
# rubocop:disable Metrics/CyclomaticComplexity
74-
def title
73+
def title # rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize
7574
@title ||= begin
76-
if site_title && page_title != site_title
75+
if site_title && !homepage_or_about? && page_title != site_title
7776
page_title + TITLE_SEPARATOR + site_title
7877
elsif site_description && site_title
7978
site_title + TITLE_SEPARATOR + site_tagline_or_description
@@ -86,7 +85,6 @@ def title
8685

8786
@title
8887
end
89-
# rubocop:enable Metrics/CyclomaticComplexity
9088

9189
def name
9290
return @name if defined?(@name)
@@ -210,7 +208,11 @@ def site
210208
end
211209

212210
def homepage_or_about?
213-
page["url"] =~ HOMEPAGE_OR_ABOUT_REGEX
211+
if page.key?("homepage_or_about")
212+
page["homepage_or_about"]
213+
else
214+
page["url"] =~ HOMEPAGE_OR_ABOUT_REGEX
215+
end
214216
end
215217

216218
def page_number

spec/jekyll_seo_tag/drop_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@
168168
expect(subject.title).to be_nil
169169
end
170170
end
171+
172+
context "with a home_or_about page and a site title" do
173+
let(:config) { { "title" => "site title" } }
174+
let(:page_meta) { { "permalink" => "/", "title" => "page title" } }
175+
176+
it "is just the page title" do
177+
expect(subject.title).to eql("page title")
178+
end
179+
end
171180
end
172181
end
173182

@@ -507,6 +516,22 @@
507516
end
508517
end
509518

519+
context "when page has homepage_or_about" do
520+
let(:page_meta) { { "homepage_or_about" => true } }
521+
522+
it "knows it's the home or about page" do
523+
expect(subject.send(:homepage_or_about?)).to be_truthy
524+
end
525+
526+
context "with homepage_or_about=false" do
527+
let(:page_meta) { { "homepage_or_about" => false } }
528+
529+
it "knows it's not the home or about page" do
530+
expect(subject.send(:homepage_or_about?)).to be_falsy
531+
end
532+
end
533+
end
534+
510535
context "a random URL" do
511536
let(:page_meta) { { "permalink" => "/about-foo/" } }
512537

0 commit comments

Comments
 (0)