Comments (10)
I don't have an example, but here is how I would do it:
First, create a ./_plugins/search.rb
file in your Jekyll root directory. Any ruby file in ./_plugins
will be loaded by Jekyll. It doesn't matter if you name if search.rb
or anything else, though.
In that file, we'll override the custom_hook_each
of the plugin, like this:
class AlgoliaSearchRecordExtractor
def custom_hook_each(item, node)
# To get the node name (p, or div, etc)
puts node.name
# To get the value of the class. Will be nil if no class defined
puts node.attr('class')
# You then just have to return item if you want to keep the element, or nil if you want to discard it
end
end
I will add this example to the documentation, to make it clearer for the next user :)
from algoliasearch-jekyll.
Actually, puts
only displays the variable. I just used that as an example. To do what you have in mind, I think this should work
class AlgoliaSearchRecordExtractor
def custom_hook_each(item, node)
if node.name == 'div' && node.attr('class').include?('related-article')
return nil
end
item
end
end
This would exclude all div
that have the class related-article
. Depending on what you exclude, you'll have to adapt it. You can add several if
in the method, and return nil
when you want to exclude the element.
from algoliasearch-jekyll.
Hello,
You can configure the record_css_selector to match only the elements you would like to index. For example if you only want the div
with class myClass
, you would put:
algolia:
record_css_selector: 'div.myClass`
Now if it's easier to exclude elements that to whitelist them, you can use the custom_hook_each method that takes the item
and node
as input. node
is a Nokogiri node, so you can simply check if it matches the classes you want to exclude and return nil
from the method. Returning nil
will prevent the element from being indexed.
Regarding the limit of words in the search results, you have several way to do that:
- You can use the Algolia snippet feature to only display the part of the text that matches. I recommend that approach as this is the one that requires the least amount of work and yields the better results.
- You can also cut the text to a shorter version. Either in Ruby before pushing it (same method than above,
custom_hook_each
lets you modify the record before pushing it), or in JavaScript before displaying it.
Hope that helps
from algoliasearch-jekyll.
Hi @pixelastic,
Thank you very much for your reply. I've done the second part (added attributesToSnippet: 20;
to _config.yml
file).
Do you have any code examples I could look at for custom_hook_each method? I would like to exclude one p
with a class and one div
with a class.
from algoliasearch-jekyll.
Hi @pixelastic,
Thank you very much for your explanation and reply! Would something like this be correct?
class AlgoliaSearchRecordExtractor
def custom_hook_each(item, node)
puts node.attr('related-article')
nil
end
def custom_hook_each(item, node)
puts node.attr('warning')
nil
end
end
from algoliasearch-jekyll.
Hi @pixelastic,
In addition to my last question is it ok to put just:
class AlgoliaSearchRecordExtractor
def custom_hook_each(item, node)
puts node.attr('related-article')
nil
end
end
Or do I have to put both, node.name
and node.attr
like this:
class AlgoliaSearchRecordExtractor
def custom_hook_each(item, node)
puts node.div
puts node.attr('related-article')
nil
end
end
Thank you very much in advance!
from algoliasearch-jekyll.
@pixelastic thank you very much! I tried the code above, but I get an undefined method error:
jekyll 3.0.0.pre.rc1 | Error: undefined method `include?' for nil:NilClass
from algoliasearch-jekyll.
class AlgoliaSearchRecordExtractor
def custom_hook_each(item, node)
class_name = node.attr('class')
if node.name == 'div' && (!class_name.nil? && class_name.include?('related-article'))
return nil
end
item
end
end
This might work better. Note that this is more related to ruby than to the plugin itself.
from algoliasearch-jekyll.
@pixelastic thank you very much for your help and I'm sorry for the trouble.
I edited the code as follows and it's working now:
class AlgoliaSearchRecordExtractor
def custom_hook_each(item, node)
if node.name == 'div' && node.attr('class') == 'related-article'
return nil
end
item
end
end
from algoliasearch-jekyll.
No problem, glad to see it works :)
from algoliasearch-jekyll.
Related Issues (20)
- Enhancement: Using the new facetingAfterDistinct=true for correct facet count HOT 3
- Timeout at indexing time HOT 6
- Content not rendered with Liquid before indexing? HOT 6
- Error: API key cannot write to {index_name}_tmp index HOT 2
- Ruby 2.4 compatibility HOT 2
- Status of V1 HOT 7
- Typo in README
- algolia selector returning undefined HOT 6
- Plugin not pushing all pages to index HOT 3
- Catch errors when zero element indexed HOT 1
- Unable to exclude files from indexing based on directories HOT 2
- Update json dependency to v2.0
- Update nokogiri dependency to version 1.7.2 or higher HOT 3
- Nokogiri Error HOT 8
- Better explain the difference between record_css_selector and attributesToIndex settings HOT 2
- Algolia push error: "no implicit conversion of nil into Hash" HOT 5
- Cannot install algoliasearch-jekyll HOT 6
- Asciidoc files don't get indexed HOT 1
- Synonyms deleted on index HOT 6
- Using "removeWordsIfNOResults" setting parameter does not seem to work HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from algoliasearch-jekyll.