algolia / algoliasearch-jekyll

⚠ DEPRECATED Use jekyll-algolia instead.
https://community.algolia.com/jekyll-algolia/
MIT License
125 stars 12 forks source link

Remove Algolia anchors in the URL #10

Closed iloveip closed 9 years ago

iloveip commented 9 years ago

Hello,

I'm trying to configure Algolia search. When I get search results for a query the url looks like this: /kb/summary/#algolia:p:nth-of-type(8). Is this an intended behavior? How can I remove this anchor from the url and link simply to a page?

borisschapira commented 9 years ago

I was also astonished by this behaviour but I understood when explained in another issue : in fact, Algolia is indexing your content by pieces (see https://github.com/algolia/algoliasearch-jekyll#settings for the settings), including each paragraph separately.

When searched, the index is able to point at the specific paragraph containing the searched term :eyes:

pixelastic commented 9 years ago

As @borisschapira perfectly explained, the anchor in the url is here so the search result is linked to the exact HTML node in the page it references.

If you want to remove it, you can use the custom hooks and override the url attribute. For example, you can create a file in _plugins/search.rb with the following content (every page in _plugins will be loaded by Jekyll).

class AlgoliaSearchRecordExtractor
  def custom_hook_each(item, node)
    # Here you have access to the item object, right before it is pushed
    # Modify it as much as you want (in your case, changing the item[:url] key) then return it
    # item[:url] = '...'
    item
  end
end

Also note that you can run jekyll algolia push -n to do a dry-run (parse data but does not send anything to the Algolia API).

Tell me if this works for you

iloveip commented 9 years ago

@borisschapira @pixelastic Thank you very much for your answers! It would be great if index could point at the specific paragraph, but instead it points at the top of the page, even though the paragraph is at the bottom.

I guess I can simply remove #algolia:{{ css_selector }} from:

<a class="algolia__result-link" href="{{ full_url }}#algolia:{{ css_selector }}">
    {{{ _highlightResult.title.value }}}
 </a>

Would that be enough?

P.S. I have a few more questions about integrating Algolia, but I'm not sure if GitHub issues is the right place to post them. Is it ok if I ask them here?

pixelastic commented 9 years ago

My mistake, I thought the url was saved, including the css_selector, in the record. As they are actually two different fields, you're right that you just have to update the link to target the page without the anchor. That will work.

To scroll the page to the correct part, you need a bit of custom JavaScript. You can have a look at how it was done on our Jekyll/Hyde demo. Basically we wait for page load, read the url, parse the anchor and scroll to the element matching the selector.

Regarding your other questions, feel free to post issues here (one thread per issue, to make tracking easier). If something is not clear enough, I'll update the documentation accordingly.

iloveip commented 9 years ago

@pixelastic ok, thank you very much.

Actually, I already have this piece of code in my search.js file. (I copied the whole algolia.js file from Jekyll/Hyde demo.) But it's still not working for me.

pixelastic commented 9 years ago

Sorry to hear that it did not work for you. If you have a public url I can have a look and see what is not working.

But as I understand this issue you prefer having the results pointing to the top of the page anyway. I'll close this issue for now then, but feel free to comment back if you want to push it further.

iloveip commented 9 years ago

Ok, thank you very much. I'll send you a link when I push it to GitHub.