Allow custom hook to return nil to skip a record

This commit is contained in:
Pixelastic 2015-07-07 17:06:47 +02:00
parent c2e82523f2
commit 9277ab9e9b
5 changed files with 44 additions and 9 deletions

View File

@ -117,7 +117,8 @@ logic. They currently simply return the argument they take as input.
```ruby
class AlgoliaSearchRecordExtractor
# Hook to modify a record after extracting
def custom_hook_each(item)
# `node` refers to the Nokogiri HTML node of the element
def custom_hook_each(item, node)
item
end

View File

@ -145,7 +145,7 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
css_selector
css_selector_parent
),
customRanking: ['desc(posted_at)', 'desc(title_weight)'],
customRanking: ['desc(posted_at)', 'desc(weight)'],
highlightPreTag: '<span class="algolia__result-highlight">',
highlightPostTag: '</span>'
}

View File

@ -13,7 +13,7 @@ class AlgoliaSearchRecordExtractor
end
# Hook to modify a record after extracting
def custom_hook_each(item)
def custom_hook_each(item, node)
item
end
@ -71,7 +71,7 @@ class AlgoliaSearchRecordExtractor
# If initially called on a heading, we must not accept it but only accept
# strong headings
level = node.name if headings.include?(node.name)
level = node.name if level == 'h7' && headings.include?(node.name)
previous = node.previous_element
@ -203,7 +203,9 @@ class AlgoliaSearchRecordExtractor
item[:css_selector_parent] = node_css_selector(node_heading_parent(node))
item[:weight] = weight(item)
item = custom_hook_each(item)
# We pass item through the user defined custom hook
item = custom_hook_each(item, node)
next if item.nil?
items << item
end

View File

@ -20,9 +20,13 @@ TEXT4-H2B-H1
TEXT5-H3-H2B-H1
<div>
<h4>H4</h4>
<p>TEXT6-H4-H3-H2B-H1</p>
<p>TEXT7-H4-H3-H2B-H1</p>
</div>
## H2C
TEXT8-H2C-H1

View File

@ -111,6 +111,21 @@ describe(AlgoliaSearchRecordExtractor) do
expect(actual.name).to eq 'h1'
expect(actual.text).to eq 'H1'
end
it 'should find the correct parent when indexing deep headings' do
# Given
site = get_site(algolia: { 'record_css_selector' => 'h2' })
test_hierarchy = extractor.new(site.file_by_name('hierarchy.md'))
nodes = test_hierarchy.html_nodes
h2 = nodes[2]
# When
actual = test_hierarchy.node_heading_parent(h2)
# Then
expect(actual.name).to eq 'h1'
expect(actual.text).to eq 'H1'
end
end
describe 'node_hierarchy' do
@ -234,7 +249,7 @@ describe(AlgoliaSearchRecordExtractor) do
expect(actual).to eq '#text4'
end
it 'uses p:nth-of-type' do
it 'uses p:nth-of-type if no #id found' do
# Given
nodes = test_page.html_nodes
p = nodes[2]
@ -339,7 +354,7 @@ describe(AlgoliaSearchRecordExtractor) do
describe 'custom_hook_each' do
it 'let the user call a custom hook to modify a record' do
# Given
def test_page.custom_hook_each(item)
def test_page.custom_hook_each(item, _)
item[:custom_attribute] = 'foo'
item
end
@ -350,6 +365,19 @@ describe(AlgoliaSearchRecordExtractor) do
# Then
expect(actual[0]).to include(custom_attribute: 'foo')
end
it 'let the user discard a record by returning nil' do
# Given
def test_page.custom_hook_each(_, _)
nil
end
# When
actual = test_page.extract
# Then
expect(actual.size).to eq 0
end
end
describe 'custom_hook_all' do