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)
randomize = limits.fetch('randomize', true)
limits.each do |collection_name, limit|
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
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
collection.docs.replace(collection.docs.first(limit))
Jekyll.logger.info "LimitCollections:", "Limited '#{collection_name}' from #{original_count} to #{limit} documents"
end
end
end
|