From 4d537a745e39cf88e80325aec755a14ab26c74b7 Mon Sep 17 00:00:00 2001 From: koooooo-7 <369491420@qq.com> Date: Thu, 23 Jul 2020 22:20:13 +0800 Subject: [PATCH] [fix #529] Search plugin: matched text is replaced with search text. --- src/plugins/search/search.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 1c7c77895..d876c55df 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -165,13 +165,18 @@ export function search(query) { end = postContent.length; } - const matchContent = - '...' + - escapeHtml(postContent) - .substring(start, end) - .replace(regEx, `${keyword}`) + - '...'; - + let content = escapeHtml(postContent).substring(start, end); + let matchKeywords = content.match(regEx); + + matchKeywords = new Set(matchKeywords); + matchKeywords.forEach(keyword => { + content = content.replace( + new RegExp(keyword, 'g'), + `${keyword}` + ); + }); + + const matchContent = '...' + content + '...'; resultStr += matchContent; } });