Improve search context preview

This commit is contained in:
Helder Pereira 2021-08-29 16:12:02 +01:00
parent 4e08a7b5a4
commit 98ccdc2bd6
2 changed files with 7 additions and 5 deletions

View file

@ -43,4 +43,6 @@
.autocomplete-suggestion > .context {
font-size: 12px;
overflow: hidden;
text-overflow: ellipsis;
}

View file

@ -46,7 +46,7 @@ function search(queryTerm) {
// Find the item in our index corresponding to the lunr one to have more info
var searchTerm = queryTerm.match(/\w+/g).map(word => word + '^100' + ' ' + word + '*^10' + ' ' + '*' + word + '^10' + ' ' + word + '~2^1').join(' ');
return lunrIndex.search(searchTerm).map(function(result) {
return { index: result.ref };
return { index: result.ref, matches: Object.keys(result.matchData.metadata) };
});
}
@ -64,10 +64,10 @@ $(function() {
renderItem: function(item, term) {
var page = pagesIndex[item.index];
var numContextWords = 2;
var text = page.content.match(
'(?:\\s?(?:[\\w]+)\\s?){0,' + numContextWords + '}' +
term.trim() + '(?:\\s?(?:[\\w]+)\\s?){0,' + numContextWords + '}');
var context = text;
var contextPattern = '(?:\\S+ +){0,' + numContextWords + '}\\S*\\b(?:' +
item.matches.map(match => match.replace(/\W/g, '\\$&')).join('|') +
')\\b\\S*(?: +\\S+){0,' + numContextWords + '}';
var context = page.content.match(new RegExp(contextPattern, 'i'));
var divcontext = document.createElement('div');
divcontext.className = 'context';
divcontext.innerText = (context || '');