Skip to content

Commit 589feb0

Browse files
authored
Truncate the description value to 100 words (#492)
Merge pull request 492
1 parent db0e639 commit 589feb0

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

docs/advanced-usage.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@ You can set it the same way as the other author properties. For example, you can
8080
url: https://example.com/
8181
```
8282

83+
### Customizing description length
84+
85+
By default, the description is limited to the first 100 words of the full content.
86+
87+
You can adjust this limit at the page level, by using the `seo_description_max_words` page property:
88+
89+
```yml
90+
seo_description_max_words: 200
91+
```
92+
93+
You can also set a default site-wide value for all pages using [Front Matter defaults](https://jekyllrb.com/docs/configuration/front-matter-defaults/) in your `_config.yml` file:
94+
95+
```yml
96+
defaults:
97+
- scope:
98+
path: ""
99+
values:
100+
seo_description_max_words: 200
101+
```
102+
103+
83104
### Customizing JSON-LD output
84105

85106
The following options can be set for any particular page. While the default options are meant to serve most users in the most common circumstances, there may be situations where more precise control is necessary.

lib/jekyll-seo-tag/drop.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def name
8989

9090
def description
9191
@description ||= begin
92-
format_string(page["description"] || page["excerpt"]) || site_description
92+
value = format_string(page["description"] || page["excerpt"]) || site_description
93+
snippet(value, description_max_words)
9394
end
9495
end
9596

@@ -175,6 +176,10 @@ def canonical_url
175176
end
176177
end
177178

179+
def description_max_words
180+
@description_max_words ||= page["seo_description_max_words"] || 100
181+
end
182+
178183
private
179184

180185
def filters
@@ -217,6 +222,13 @@ def format_string(string)
217222
string unless string.empty?
218223
end
219224

225+
def snippet(string, max_words)
226+
return string if string.nil?
227+
228+
result = string.split(%r!\s+!, max_words + 1)[0...max_words].join(" ")
229+
result.length < string.length ? result.concat("…") : result
230+
end
231+
220232
def seo_name
221233
@seo_name ||= format_string(page_seo["name"]) if page_seo["name"]
222234
end

spec/jekyll_seo_tag/drop_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,24 @@
233233
expect(subject.description).to be_nil
234234
end
235235
end
236+
237+
context "truncation" do
238+
context "without seo_description_max_words" do
239+
let(:page_meta) { { "description" => "word " * 150 } }
240+
241+
it "truncates the description to the first 200 words" do
242+
expect(subject.description).to eql(("word " * 100).chop.concat("…"))
243+
end
244+
end
245+
246+
context "with an explicit seo_description_max_words property" do
247+
let(:page_meta) { { "description" => "For a long time, I went to bed early", "seo_description_max_words" => 6 } }
248+
249+
it "truncates the description to the configured words count" do
250+
expect(subject.description).to eql("For a long time, I went…")
251+
end
252+
end
253+
end
236254
end
237255

238256
context "author" do

spec/jekyll_seo_tag_integration_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,23 @@
350350
it "removes null values from JSON-LD" do
351351
expect(output).to_not match(%r!:null!)
352352
end
353+
354+
context "description" do
355+
context "with page.seo_description_max_words" do
356+
let(:meta) do
357+
{
358+
"title" => "post",
359+
"description" => "For a long time, I went to bed early",
360+
"image" => "/img.png",
361+
"seo_description_max_words" => 6,
362+
}
363+
end
364+
365+
it "truncates the description" do
366+
expect(json_data["description"]).to eql("For a long time, I went…")
367+
end
368+
end
369+
end
353370
end
354371
end
355372

0 commit comments

Comments
 (0)