Class: Jekyll::PreCommit::Checks::FrontMatterVariableMeetsLengthRequirements

Inherits:
Check
  • Object
show all
Defined in:
lib/jekyll-pre-commit/checks/front_matter_variable_meets_length_requirements.rb

Constant Summary collapse

DEFAULT_LENGTH_REQUIREMENTS =
{
  "description" => {
    "min" => 145,
    "max" => 165,
  },
  "title" => {
    "max" => 59
  }
}

Instance Method Summary collapse

Methods inherited from Check

#initialize

Constructor Details

This class inherits a constructor from Jekyll::PreCommit::Checks::Check

Instance Method Details

#check(staged, not_staged, site, args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jekyll-pre-commit/checks/front_matter_variable_meets_length_requirements.rb', line 16

def check(staged, not_staged, site, args)
  if !args["variables"]
    @result[:message] += "No variables to check."
    return @result
  end

  staged.each do |post|
    args["variables"].each do |variable|
      parts = variable.split('|')
      next if !post.data[parts[0]]
      # If use custom configuration if provided
      if parts[1]
        min = parts[1].to_i
        max = parts[2].to_i
      else
        if DEFAULT_LENGTH_REQUIREMENTS[variable] && DEFAULT_LENGTH_REQUIREMENTS[variable]["min"]
          min = DEFAULT_LENGTH_REQUIREMENTS[variable]["min"]
        end
        if DEFAULT_LENGTH_REQUIREMENTS[variable] && DEFAULT_LENGTH_REQUIREMENTS[variable]["max"]
          max = DEFAULT_LENGTH_REQUIREMENTS[variable]["max"]
        end
      end

      if min && post.data[parts[0]].length < min
        @result[:ok] = false
        @result[:message] += "#{post.data["title"]}'s #{parts[0]} is too short. "
      elsif max && post.data[parts[0]].length > max
        @result[:ok] = false
        @result[:message] += "#{post.data["title"]}'s #{parts[0]} is too long. "
      end
    end
  end

  @result
end