Skip to content

Commit 7a9cb2a

Browse files
committedJul 18, 2018
fix(error): Correctly calculate record size in bytes
1 parent 528e2f6 commit 7a9cb2a

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed
 

‎lib/jekyll/algolia/error_handler.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def self.invalid_credentials?(error, _context = {})
183183
# should not happen as we proactively check for record size before pushing
184184
# them. If it still happens it means that the value set in max_record_size
185185
# is not matching the value in the plan.
186-
def self.record_too_big_api?(error, _contex = {})
186+
def self.record_too_big_api?(error, _context = {})
187187
details = error_hash(error.message)
188188
return false if details == false
189189

‎lib/jekyll/algolia/extractor.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,20 @@ def self.add_unique_object_id(record)
5454
#
5555
# content - The HTML content to parse
5656
def self.extract_raw_records(content)
57-
AlgoliaHTMLExtractor.run(
57+
records = AlgoliaHTMLExtractor.run(
5858
content,
5959
options: {
6060
css_selector: Configurator.algolia('nodes_to_index'),
6161
tags_to_exclude: 'script,style,iframe'
6262
}
6363
)
64+
# We remove objectIDs, as the will be added at the very end, after all
65+
# the hooks and shrinkage
66+
records.each do |record|
67+
record.delete(:objectID)
68+
end
69+
70+
records
6471
end
6572
end
6673
end

‎lib/jekyll/algolia/indexer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def self.set_settings(settings)
346346
# their dashboard
347347
#
348348
# When users change some settings in their dashboard, those settings might
349-
# get overwritten by the pluging. We can't prevent that, but we can warn
349+
# get overwritten by the plugin. We can't prevent that, but we can warn
350350
# them when we detect they changed something.
351351
def self.warn_of_manual_dashboard_editing(changed_keys)
352352
# Transform the hash into readable YAML

‎lib/jekyll/algolia/shrinker.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Shrinker
1010
# Public: Get the byte size of the object once converted to JSON
1111
# - record: The record to estimate
1212
def self.size(record)
13-
record.to_json.length
13+
record.to_json.bytesize
1414
end
1515

1616
# Public: Attempt to reduce the size of the record by reducing the size of
@@ -33,7 +33,7 @@ def self.fit_to_size(raw_record, max_size)
3333
record[:excerpt_html] = record[:excerpt_text]
3434
return record if size(record) <= max_size
3535

36-
# We halve the excerpts
36+
# We half the excerpts
3737
excerpt_words = record[:excerpt_text].split(/\s+/)
3838
shortened_excerpt = excerpt_words[0...excerpt_words.size / 2].join(' ')
3939
record[:excerpt_text] = shortened_excerpt

‎spec/jekyll/algolia/shrinker_spec.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616
it { should eq 2 }
1717
end
1818

19-
describe 'should return a size in bytes' do
19+
describe 'should return the size if no special chars' do
2020
let(:input) { { foo: 'bar' } }
2121
it { should eq 13 }
2222
end
23+
24+
describe 'should return a size in bytes, even with UTF-8 chars' do
25+
let(:input) { { foo: '“æé' } }
26+
it { should eq 17 }
27+
end
2328
end
2429

2530
describe '.fit_to_size' do

0 commit comments

Comments
 (0)
Failed to load comments.