Skip to content

Commit 9d88487

Browse files
committed
Add FrontMatterVariableMeetsLengthRequirements check
1 parent 32c6e98 commit 9d88487

9 files changed

+123
-31
lines changed

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,47 @@ pre-commit:
3030
variables: ['description', 'image']
3131
- check: FrontMatterVariableIsNotDuplicate
3232
variables: ['description']
33-
- check: DescriptionIsGoodLength
33+
- check: FrontMatterVariableMeetsLengthRequirements
34+
variables: ['description', 'title']
3435
```
3536
3637
## Available Checks
3738
3839
#### FrontMatterVariableExists
3940
40-
This check ensures that any variable properties exist in the front matter of any post that is staged to be committed.
41+
This check ensures that any listed variables exist in the front matter of any post that is staged to be committed.
4142
4243
#### FrontMatterVariableIsNotDuplicate
4344
4445
This check ensures that any listed variable in the front matter of any post that is staged to be committed are unique amongst all the posts on your site.
4546
46-
#### DescriptionIsGoodLength
47+
#### FrontMatterVariableMeetsLengthRequirements
4748
48-
This check ensures that the `description` in the front matter of any post that is staged to be commited is a [good length](https://moz.com/learn/seo/meta-description). Specifically it checks that the description is between 145 and 165 characters.
49+
This check ensures that any listed variable in the front matter of any post that is staged to be committed meet the length requirements (in number of characters).
50+
51+
This check includes the following defaults:
52+
53+
**title**
54+
55+
- max: 59
56+
57+
**description**
58+
59+
- min: 145
60+
- max: 165
61+
62+
These can be overridden, or requirements can be specified for other variables in the following format...
63+
64+
- `variable|min|max`
65+
66+
For example...
67+
68+
```yaml
69+
- check: FrontMatterVariableMeetsLengthRequirements
70+
variables: ['title||50']
71+
```
72+
73+
In the above, there would be a maximum length of 50 characters for the title (rather than the default of 59)
4974

5075
## Contributing
5176

lib/jekyll-pre-commit.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ module PreCommit
99
require 'jekyll-pre-commit/checks/check.rb'
1010
require 'jekyll-pre-commit/checks/front_matter_variable_exists.rb'
1111
require 'jekyll-pre-commit/checks/front_matter_variable_is_not_duplicate.rb'
12-
require 'jekyll-pre-commit/checks/description_is_good_length.rb'
12+
require 'jekyll-pre-commit/checks/front_matter_variable_meets_length_requirements.rb'

lib/jekyll-pre-commit/checks/description_is_good_length.rb

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/jekyll-pre-commit/checks/front_matter_variable_exists.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Jekyll
22
module PreCommit
33
module Check
44
class FrontMatterVariableExists < Check
5-
def Check(staged, not_staged, site, args)
5+
def check(staged, not_staged, site, args)
66
if !args["variables"]
77
@result[:message] += "No variables to check."
88
return @result

lib/jekyll-pre-commit/checks/front_matter_variable_is_not_duplicate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Jekyll
22
module PreCommit
33
module Check
44
class FrontMatterVariableIsNotDuplicate < Check
5-
def Check(staged, not_staged, site, args)
5+
def check(staged, not_staged, site, args)
66
if !args["variables"]
77
@result[:message] += "No variables to check."
88
return @result
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module Jekyll
2+
module PreCommit
3+
module Check
4+
class FrontMatterVariableMeetsLengthRequirements < Check
5+
6+
DEFAULT_LENGTH_REQUIREMENTS = {
7+
"description" => {
8+
"min" => 145,
9+
"max" => 165,
10+
},
11+
"title" => {
12+
"max" => 59
13+
}
14+
}
15+
16+
def check(staged, not_staged, site, args)
17+
if !args["variables"]
18+
@result[:message] += "No variables to check."
19+
return @result
20+
end
21+
22+
staged.each do |post|
23+
args["variables"].each do |variable|
24+
parts = variable.split('|')
25+
next if !post.data[parts[0]]
26+
# If use custom configuration if provided
27+
if parts[1]
28+
min = parts[1].to_i
29+
max = parts[2].to_i
30+
else
31+
if DEFAULT_LENGTH_REQUIREMENTS[variable] && DEFAULT_LENGTH_REQUIREMENTS[variable]["min"]
32+
min = DEFAULT_LENGTH_REQUIREMENTS[variable]["min"]
33+
end
34+
if DEFAULT_LENGTH_REQUIREMENTS[variable] && DEFAULT_LENGTH_REQUIREMENTS[variable]["max"]
35+
max = DEFAULT_LENGTH_REQUIREMENTS[variable]["max"]
36+
end
37+
end
38+
39+
if min && post.data[parts[0]].length < min
40+
@result[:ok] = false
41+
@result[:message] += "#{post.data["title"]}'s #{parts[0]} is too short. "
42+
elsif max && post.data[parts[0]].length > max
43+
@result[:ok] = false
44+
@result[:message] += "#{post.data["title"]}'s #{parts[0]} is too long. "
45+
end
46+
end
47+
end
48+
49+
@result
50+
end
51+
end
52+
end
53+
end
54+
end

lib/jekyll-pre-commit/runner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def run(site, staged_files)
3535
result[:messages].push("The check #{c["check"]} does not exist! Please fix your configuration.")
3636
break
3737
end
38-
r = o.Check(staged_posts, not_staged_posts, site, c)
38+
r = o.check(staged_posts, not_staged_posts, site, c)
3939
if !r[:ok]
4040
result[:ok] = false
4141
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
layout: post
3+
title: "description-is-too-long-for-custom-length-requirements"
4+
description: description-is-too-long-for-custom-length-requirements description-is-too-long-for-custom-length-requirements description-is-too-long-for-custom-length-requirements description-is-too-long-for-custom-length-requirements description-is-too-long-for-custom-length-requirements description-is-too-long-for-custom-length-requirements description-is-too-long-for-custom-length-requirements description-is-too-long-for-custom-length-requirements description-is-too-long-for-custom-length-requirements description-is-too-long-for-custom-length-requirements description-is-too-long-for-custom-length-requirements description-is-too-long-for-custom-length-requirements
5+
---
6+
7+
!
8+
9+
10+
description-is-too-long-for-custom-length-requirements

spec/jekyll-pre-commit_spec.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@
136136
end
137137
end
138138

139-
context "with description is good length check" do
140-
let(:site) { build_site({'pre-commit' => [{"check" => "DescriptionIsGoodLength"}]}) }
139+
context "with FrontMatterVariableMeetsLengthRequirements checking description with default length settings" do
140+
pre_commit_config = {"check" => "FrontMatterVariableMeetsLengthRequirements", "variables" => ["description"]}
141+
let(:site) { build_site({ 'pre-commit' => [pre_commit_config] }) }
141142

142143
it "fails if a staged post has a description that's too long" do
143144
result = runner.run(site, ["spec/fixtures/_posts/2017-01-06-description-is-too-long.md"])
@@ -158,6 +159,29 @@
158159
end
159160
end
160161

162+
context "with FrontMatterVariableMeetsLengthReqirements checking description with custom length settings" do
163+
pre_commit_config = {"check" => "FrontMatterVariableMeetsLengthRequirements", "variables" => ["description||200"]}
164+
let(:site) { build_site({ 'pre-commit' => [pre_commit_config] }) }
165+
166+
it "passes if a staged post is doesn't meet default requirements, but meets custom requirements" do
167+
result = runner.run(site, ["spec/fixtures/_posts/2017-01-06-description-is-too-long.md"])
168+
expect(result[:ok]).to eql(true)
169+
expect(result[:messages]).to match_array([])
170+
end
171+
172+
it "passes if a staged post is doesn't meet default requirements, but meets custom requirements - #2" do
173+
result = runner.run(site, ["spec/fixtures/_posts/2017-01-06-description-is-too-short.md"])
174+
expect(result[:ok]).to eql(true)
175+
expect(result[:messages]).to match_array([])
176+
end
177+
178+
it "fails if a staged post doesn't meet custom length requirements" do
179+
result = runner.run(site, ["spec/fixtures/_posts/2017-01-09-description-is-too-long-for-custom-length-requirements.md"])
180+
expect(result[:ok]).to eql(false)
181+
expect(result[:messages]).to match_array(["description-is-too-long-for-custom-length-requirements's description is too long. "])
182+
end
183+
end
184+
161185
context "with a check that doesn't exist" do
162186
pre_commit_config = {"check" => "Garbage"}
163187
let(:site) { build_site({ 'pre-commit' => [pre_commit_config] }) }

0 commit comments

Comments
 (0)