Class: Jekyll::LimitCollectionsGenerator

Inherits:
Generator
  • Object
show all
Defined in:
lib/generators/limit-collections.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



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
51
52
53
54
# File 'lib/generators/limit-collections.rb', line 23

def generate(site)
  limits = site.config['limit_collections']
  return unless limits.is_a?(Hash)

  # Check if randomization is disabled (default: true)
  randomize = limits.fetch('randomize', true)

  limits.each do |collection_name, limit|
    # Skip the 'randomize' option itself
    next if collection_name == 'randomize'
    next unless limit.is_a?(Integer) && limit > 0

    collection = site.collections[collection_name]
    next unless collection

    original_count = collection.docs.size
    next if original_count <= limit

    if randomize
      # Use a seeded random for repeatable but diverse sampling
      # Seed based on collection name so it's consistent across rebuilds
      rng = Random.new(collection_name.hash.abs)
      sampled_docs = collection.docs.shuffle(random: rng).first(limit)
      collection.docs.replace(sampled_docs)
      Jekyll.logger.info "LimitCollections:", "Limited '#{collection_name}' from #{original_count} to #{limit} documents (random sample)"
    else
      # Take first N documents in order
      collection.docs.replace(collection.docs.first(limit))
      Jekyll.logger.info "LimitCollections:", "Limited '#{collection_name}' from #{original_count} to #{limit} documents"
    end
  end
end