diff --git a/lib/search/lunr-search.js b/lib/search/lunr-search.js
index f533e91860..75f02a863f 100644
--- a/lib/search/lunr-search.js
+++ b/lib/search/lunr-search.js
@@ -248,7 +248,7 @@ function field(matchData, record, name) {
if (!positions.length) return text
// Highlight the text
- return positions
+ const highlighted = positions
.map(([prev, start, end], i) => [
text.slice(prev, start),
mark(text.slice(start, end)),
@@ -257,6 +257,16 @@ function field(matchData, record, name) {
.flat()
.filter(Boolean)
.join('')
+
+ // We can't HTML escape the content until AFTER all the matchData positions
+ // have been processed otherwise, the positions should shift.
+ // The only HTML that is OK to keep is and .
+ return highlighted
+ .replace(/&/g, '&')
+ .replace(//g, '>')
+ .replace(/<mark>/g, '')
+ .replace(/<\/mark>/g, '')
}
function mark(text) {