module Jekyll::Algolia::Shrinker
def self.fit_to_size(raw_record, max_size)
The excerpts are the attributes most subject to being reduced. We'll go
- max_size: The max size to achieve in bytes
- raw_record: The record to attempt to reduce
the less needed attributes
Public: Attempt to reduce the size of the record by reducing the size of
def self.fit_to_size(raw_record, max_size) return raw_record if size(raw_record) <= max_size # No excerpt, we can't shrink it return stop_with_error(raw_record) unless raw_record.key?(:excerpt_html) record = raw_record.clone # We replace the HTML excerpt with the textual one record[:excerpt_html] = record[:excerpt_text] return record if size(record) <= max_size # We half the excerpts excerpt_words = record[:excerpt_text].split(/\s+/) shortened_excerpt = excerpt_words[0...excerpt_words.size / 2].join(' ') record[:excerpt_text] = shortened_excerpt record[:excerpt_html] = shortened_excerpt return record if size(record) <= max_size # We remove the excerpts completely record.delete(:excerpt_text) record.delete(:excerpt_html) return record if size(record) <= max_size # Still too big, we fail stop_with_error(record) end