olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.94k stars 548 forks source link

Highlight term in result's extract #446

Closed inwardmovement closed 4 years ago

inwardmovement commented 4 years ago

May I have a little help on how to implement term highlighting for my use case? That's already talked about in some issues but I can't manage to get a proper solution, it seems quite complicated to implement.

I feel I can simply get the position of the term and wrap it between <mark></mark> with JQuery wrap() method for example. Any idea how I can do that?

This is the line where I display the result's extract (for now it's only the first characters of the "content" field, but I'd like it to be say 50 characters before and after the searched term), and here is the line where I whitelist the position metadata (so far that's the only thing I understood). The site can be previewed here.

Any guidelines would be appreciated!

olivernn commented 4 years ago

If there is an issue specifically with how Lunr is returning positions of matched terms then I can help, unfortunately I can't help you with using the results from Lunr in your application.