mirror of
https://github.com/McShelby/hugo-theme-relearn.git
synced 2025-02-17 09:10:08 +00:00
Improve search context preview
This commit is contained in:
parent
4e08a7b5a4
commit
98ccdc2bd6
2 changed files with 7 additions and 5 deletions
|
@ -43,4 +43,6 @@
|
||||||
|
|
||||||
.autocomplete-suggestion > .context {
|
.autocomplete-suggestion > .context {
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ function search(queryTerm) {
|
||||||
// Find the item in our index corresponding to the lunr one to have more info
|
// 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(' ');
|
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 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) {
|
renderItem: function(item, term) {
|
||||||
var page = pagesIndex[item.index];
|
var page = pagesIndex[item.index];
|
||||||
var numContextWords = 2;
|
var numContextWords = 2;
|
||||||
var text = page.content.match(
|
var contextPattern = '(?:\\S+ +){0,' + numContextWords + '}\\S*\\b(?:' +
|
||||||
'(?:\\s?(?:[\\w]+)\\s?){0,' + numContextWords + '}' +
|
item.matches.map(match => match.replace(/\W/g, '\\$&')).join('|') +
|
||||||
term.trim() + '(?:\\s?(?:[\\w]+)\\s?){0,' + numContextWords + '}');
|
')\\b\\S*(?: +\\S+){0,' + numContextWords + '}';
|
||||||
var context = text;
|
var context = page.content.match(new RegExp(contextPattern, 'i'));
|
||||||
var divcontext = document.createElement('div');
|
var divcontext = document.createElement('div');
|
||||||
divcontext.className = 'context';
|
divcontext.className = 'context';
|
||||||
divcontext.innerText = (context || '');
|
divcontext.innerText = (context || '');
|
||||||
|
|
Loading…
Add table
Reference in a new issue