Skip to content

Commit b57d5ba

Browse files
committed
Add NoDuplicateTags check
1 parent f6d529b commit b57d5ba

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

lib/jekyll-pre-commit.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ module PreCommit
1010
require 'jekyll-pre-commit/checks/front_matter_variable_exists.rb'
1111
require 'jekyll-pre-commit/checks/front_matter_variable_is_not_duplicate.rb'
1212
require 'jekyll-pre-commit/checks/front_matter_variable_meets_length_requirements.rb'
13+
require 'jekyll-pre-commit/checks/no_duplicate_tags.rb'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module Jekyll
2+
module PreCommit
3+
module Checks
4+
class NoDuplicateTags < Check
5+
def check(staged, not_staged, site, args)
6+
raw = Hash.new
7+
normalized = Hash.new
8+
not_staged.each do |post|
9+
if post.data["tags"].size > 0
10+
post.data["tags"].each do |tag|
11+
if !raw[tag]
12+
raw[tag] = post.relative_path
13+
end
14+
15+
n = Jekyll::Utils.slugify(tag)
16+
if !normalized[n]
17+
normalized[n] = post.relative_path
18+
end
19+
end
20+
end
21+
end
22+
23+
staged.each do |post|
24+
if post.data["tags"].size > 0
25+
post.data["tags"].each do |tag|
26+
n = Jekyll::Utils.slugify(tag)
27+
28+
if normalized[n] && !raw[tag]
29+
@result[:ok] = false
30+
@result[:message] += "#{tag} appears to be duplicated in #{normalized[n]}!"
31+
end
32+
end
33+
end
34+
end
35+
36+
@result
37+
end
38+
end
39+
end
40+
end
41+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
layout: post
3+
title: "Duplicate Tags 1"
4+
description: Duplicate Tags 1
5+
image: Duplicate Tags 1
6+
tags: ["Duplicate"]
7+
---
8+
9+
!
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
layout: post
3+
title: "Duplicate Tags 2"
4+
description: Duplicate Tags 2
5+
image: Duplicate Tags 2
6+
tags: ["duplicate"]
7+
---
8+
9+
!
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
layout: post
3+
title: "Unique Tags"
4+
description: Unique Tags
5+
image: Unique Tags
6+
tags: ["unique"]
7+
---
8+
9+
!

spec/jekyll-pre-commit_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@
182182
end
183183
end
184184

185+
context "with NoDuplicateTags" do
186+
pre_commit_config = {"check" => "NoDuplicateTags"}
187+
let(:site) { build_site({ 'pre-commit' => [pre_commit_config] }) }
188+
189+
it "passes if a staged post has no tags" do
190+
result = runner.run(site, ["spec/fixtures/_posts/2017-01-06-description-is-too-long.md"])
191+
expect(result[:ok]).to eql(true)
192+
expect(result[:messages]).to match_array([])
193+
end
194+
195+
it "passes if a staged post has tags, but no duplicated" do
196+
result = runner.run(site, ["spec/fixtures/_posts/2017-07-24-has-unique-tags.md"])
197+
expect(result[:ok]).to eql(true)
198+
expect(result[:messages]).to match_array([])
199+
end
200+
201+
it "fails if a staged post has duplicate tags" do
202+
result = runner.run(site, ["spec/fixtures/_posts/2017-07-24-duplicate-tags-2.md"])
203+
expect(result[:ok]).to eql(false)
204+
expect(result[:messages]).to match_array(["duplicate appears to be duplicated in _posts/2017-07-24-duplicate-tags-1.md!"])
205+
end
206+
end
207+
185208
context "with a check that doesn't exist" do
186209
pre_commit_config = {"check" => "Garbage"}
187210
let(:site) { build_site({ 'pre-commit' => [pre_commit_config] }) }

0 commit comments

Comments
 (0)