Skip to content

Commit cf5c2d6

Browse files
committed
Document adding custom checks
Also, add a test case.
1 parent 92358a4 commit cf5c2d6

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ This check ensures that any listed variables exist in the front matter of any po
4444
4545
#### FrontMatterVariableIsNotDuplicate
4646
47-
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.
47+
This check ensures that any listed variables are unique amongst all the posts on your site in the front matter of any post that is staged to be committed.
4848
4949
#### FrontMatterVariableMeetsLengthRequirements
5050
51-
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).
51+
This check ensures that any listed variables meet the length requirements (in number of characters) in the front matter of any post that is staged to be committed.
5252
5353
This check includes the following defaults:
5454
@@ -74,6 +74,35 @@ For example...
7474

7575
In the above, there would be a maximum length of 50 characters for the title (rather than the default of 59)
7676

77+
## Roll Your Own
78+
79+
You can also add your own checks. To do so, create a class in the `Jekyll::PreCommit::Check` module and and define a `check` method. Your class should extend the `Jekyll::PreCommit::Check::Check` class and return the `@result` instance variable.
80+
81+
For example...
82+
83+
```ruby
84+
module Jekyll
85+
module PreCommit
86+
module Check
87+
class DoesNothing < Check
88+
def check(staged, not_staged, site, args)
89+
@result
90+
end
91+
end
92+
end
93+
end
94+
end
95+
```
96+
97+
Put this file in your plugins_path (which is _plugins by default) and `jekyll-pre-commit` will load it automatically. Then just specify that you'd like to run this check in your front matter.
98+
99+
```yaml
100+
pre-commit:
101+
- check: DoesNothing
102+
```
103+
104+
As you can probably tell by the name, this check doesn't actually do anything. Review the checks in `lib/jekyll-pre-commit/checks` for some more useful examples.
105+
77106
## Contributing
78107

79108
Bug reports and pull requests are welcome on GitHub at https://github.com/mpchadwick/jekyll-pre-commit.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Jekyll
2+
module PreCommit
3+
module Check
4+
class CustomCheck < Check
5+
def check(staged, not_staged, site, args)
6+
@result[:message] += "I was created."
7+
@result
8+
end
9+
end
10+
end
11+
end
12+
end

spec/jekyll-pre-commit_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,15 @@
209209
])
210210
end
211211
end
212+
213+
context "with a custom check" do
214+
pre_commit_config = {"check" => "CustomCheck"}
215+
let(:site) { build_site({ 'pre-commit' => [pre_commit_config] }) }
216+
217+
it "is able to instantiate the custom check" do
218+
result = runner.run(site, ["spec/fixtures/_posts/2017-01-06-has-description.md"])
219+
expect(result[:ok]).to eql(true)
220+
expect(result[:messages]).to match_array(["I was created."])
221+
end
222+
end
212223
end

0 commit comments

Comments
 (0)