Skip to content

Commit 6e515af

Browse files
authored
Add support for title_category (#517)
Merge pull request 517
1 parent d61a2a8 commit 6e515af

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

docs/advanced-usage.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,24 @@ seo_paginator_message: "%<current>s / %<total>s | "
189189

190190
While the value can be any string text, we recommend using a Ruby string-template containing the variables `current` and `total`
191191
similar to the example above, to incorporate the current page-number and total number of paginated pages in the title.
192+
193+
### Adding a page title category
194+
195+
You can optionally add a page category to its title.
196+
This is useful for indicating to the users that some pages are logically grouped or are of a specific kind.
197+
198+
For example page front matter including:
199+
200+
```yml
201+
title: "my page"
202+
title_category: "category"
203+
```
204+
205+
And `_config.yml` including:
206+
207+
```yml
208+
title: "my site"
209+
```
210+
211+
Will generate `my page | category | my site` as the page's main `<title>` tag
212+
and `my page | category` as all other page's title declarations like `og:title`.

lib/jekyll-seo-tag/drop.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ def site_description
4747

4848
# Page title without site title or description appended
4949
def page_title
50-
@page_title ||= format_string(page["title"]) || site_title
50+
return @page_title if defined?(@page_title)
51+
52+
title = format_string(page["title"])
53+
title_category = format_string(page["title_category"])
54+
@page_title = if title && title_category && title != title_category
55+
title + TITLE_SEPARATOR + title_category
56+
else
57+
title || title_category || site_title
58+
end
5159
end
5260

5361
def site_tagline_or_description

spec/jekyll_seo_tag/drop_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@
7373
end
7474
end
7575

76+
context "with a page title, page title category and site title" do
77+
let(:page) { make_page("title" => "page title", "title_category" => "page title category") }
78+
79+
it "builds the title" do
80+
expect(subject.title).to eql("page title | page title category | site title")
81+
end
82+
end
83+
84+
context "with a page title category and site title" do
85+
let(:page) { make_page("title_category" => "page title category") }
86+
87+
it "builds the title" do
88+
expect(subject.title).to eql("page title category | site title")
89+
end
90+
end
91+
92+
context "with a page title, identical page title category and site title" do
93+
let(:page) { make_page("title" => "page title", "title_category" => "page title") }
94+
95+
it "builds the title" do
96+
expect(subject.title).to eql("page title | site title")
97+
end
98+
end
99+
76100
context "with a site description but no page title" do
77101
let(:page) { make_page }
78102
let(:config) do

0 commit comments

Comments
 (0)