From 62626267ac10d4993556c5031fe04abce728b499 Mon Sep 17 00:00:00 2001 From: Ynda Jas Date: Fri, 11 Nov 2022 17:59:53 +0000 Subject: [PATCH] Don't add ellipses to search excerpt when at start/end If the excerpt starts at the start of the content, ellipses will not be prepended; if the excerpt ends at the end of the content, ellipses will not be appended --- src/assets/js/search.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/assets/js/search.js b/src/assets/js/search.js index 68160eff3..5285336b3 100644 --- a/src/assets/js/search.js +++ b/src/assets/js/search.js @@ -88,7 +88,7 @@ `

${item.title}

` + '' + (breadcrumbs.length ? `
${breadcrumbsString}
` : '') + - `

...${excerpt}...

` + + `

${excerpt}

` + '' }) @@ -99,10 +99,13 @@ const queryRegex = new RegExp(query, 'i') const searchIndex = content.search(queryRegex) const queryLength = query.length - const excerpt = content.slice( - getStartIndex(searchIndex, content), - getEndIndex(searchIndex, queryLength, content) - ) + + const excerptStartIndex = getStartIndex(searchIndex, content) + const excerptEndIndex = getEndIndex(searchIndex, queryLength, content) + let excerpt = content.slice(excerptStartIndex, excerptEndIndex) + + if (excerptStartIndex > 0) { excerpt = '...' + excerpt } + if (excerptEndIndex < content.length) { excerpt += '...' } return excerpt.replace( queryRegex,