Skip to content

Commit

Permalink
Don't add ellipses to search excerpt when at start/end
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yndajas committed Nov 18, 2022
1 parent 35c9a96 commit 6262626
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/assets/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
`<h3 class="search-results__result-title">${item.title}</h3>` +
'</a>' +
(breadcrumbs.length ? `<div class='search-results__result-breadcrumbs'>${breadcrumbsString}</div>` : '') +
`<p class="search-results__result-excerpt">...${excerpt}...</p>` +
`<p class="search-results__result-excerpt">${excerpt}</p>` +
'</li>'
})

Expand All @@ -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,
Expand Down

0 comments on commit 6262626

Please sign in to comment.