Skip to content

Commit

Permalink
tweaked how search suggestions highlight work
Browse files Browse the repository at this point in the history
  • Loading branch information
toreysoloio committed Jun 11, 2024
1 parent 3a5355c commit a48864e
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions layouts/partials/docs/footer/flexsearch.html
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,35 @@
if(match) {
try {
let string = '';
const { [0]:fullmatch, index } = match;
const { [0]:fullmatch, [1]:pre, index } = match;
let preSegment = '';
let preChunk = 20; // check this many characters before the word
if(index > 0) {
// we want words before the matched word - ignore the earliest one though, since it will likely not be a full word.
string += doc.content.substring(index-30, index).trim().split(' ').slice(1).join(' ');
// -2 and extra `pre` at end are need for sentence detection to properly work, and should be removed after if test don't catch them
preSegment = doc.content.substring(index-preChunk-2, index).replace(/\s/g, ' ')+pre;
if(preSegment.indexOf('. ') > -1) {
preSegment = preSegment.split('. ')[1];
} else {
// remove extra 2 we added in front to confirm there was a sentence end right before
preSegment = preSegment.substring(2);
// we want words before the matched word - ignore the earliest one though, since it will likely not be a full word.
preSegment = preSegment.split(' ').slice(1).join(' ');
// show an ellipsis since we didn't detect a sentence ending
preSegment = '…' + preSegment.trimStart();
}
// remove the ending space we added to let sentence end detection work
if(pre) preSegment = preSegment.replace(new RegExp(pre+'$'), '');
}
string += preSegment;
string += fullmatch;
const indexAfter = index + fullmatch.length;
let suffChunk = 40+(preChunk-preSegment.length); // check this many characters after the word
// we want words after the matched word - ignore the last one though, since it will likely not be a full word.
string += doc.content.substring(indexAfter, indexAfter+30).trim().split(' ').slice(0, -1).join(' ');
return '…' + string.trim() + '…';
string += doc.content.substring(indexAfter, indexAfter+suffChunk).split(' ').slice(0, -1).join(' ');
string = string.trimEnd() + '…';
return string;
}
catch(e){}
}
Expand Down

0 comments on commit a48864e

Please sign in to comment.