statamic / ideas

šŸ’”Discussions on ideas and feature requests for Statamic
https://statamic.dev
32 stars 1 forks source link

Contextual keyword snippets in search results #913

Closed jacksleight closed 1 year ago

jacksleight commented 1 year ago

Something @robdekort and I were just chatting about was the ability to show contextual keyword snippets in search results, where you show the matched word(s) plus a few either side. This kinda thing (search for "lorem"):

Lorem ipsum dolor sit amet, consectetur adipiscing elit. ... Etiam bibendum lorem nec tempus sollicitudin. ... Sed in dapibus lorem. ... Nunc turpis ipsum, bibendum quis sodales sed, ullamcorper et lorem. Donec et metus hendrerit, interdum elit ut, dignissim dui.

This is something that Algolia (and I think Meilisearch) can do, but it's not currently possible with the internal Comb driver.

I had a look and in principle I think it could be fairly simple to implement, it's basically just running an extra regex against the searched text and then returning a list of matches along with each result:

Screenshot 2022-11-23 at 14 25 14

You could do this manually when rendering the results, but that would involve scanning through all the content a second time, it would be more efficient to do it inside the search driver.

Could then be used with the mark modifier to highlight the matches.

jackmcdade commented 1 year ago

I like it!

jasonvarga commented 1 year ago

Algolia does this (like you said) and I think will already just work if you use the Algolia driver.