Giter Club home page Giter Club logo

Comments (10)

pixelastic avatar pixelastic commented on August 28, 2024 1

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.

pixelastic avatar pixelastic commented on August 28, 2024 1

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.

pixelastic avatar pixelastic commented on August 28, 2024

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.

iloveip avatar iloveip commented on August 28, 2024

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.

iloveip avatar iloveip commented on August 28, 2024

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.

iloveip avatar iloveip commented on August 28, 2024

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.

iloveip avatar iloveip commented on August 28, 2024

@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.

pixelastic avatar pixelastic commented on August 28, 2024
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.

iloveip avatar iloveip commented on August 28, 2024

@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.

pixelastic avatar pixelastic commented on August 28, 2024

No problem, glad to see it works :)

from algoliasearch-jekyll.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.