From f6332b020d51e227532aae1f13459e5a6d6aba50 Mon Sep 17 00:00:00 2001 From: huangzhhui Date: Wed, 22 Jul 2020 03:23:20 +0800 Subject: [PATCH 1/4] Fix search plugin will replace matching text --- src/plugins/search/search.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index b931e62cc..24cbb7db0 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -166,11 +166,19 @@ export function search(query) { end = postContent.length; } + let originKeyword = escapeHtml(postContent).substr( + indexContent, + keyword.length + ); + const matchContent = '...' + escapeHtml(postContent) .substring(start, end) - .replace(regEx, `${keyword}`) + + .replace( + regEx, + `${originKeyword}` + ) + '...'; resultStr += matchContent; From 6674007f3f9128bfeef08f0a710cac4dd1b05aac Mon Sep 17 00:00:00 2001 From: huangzhhui Date: Wed, 22 Jul 2020 20:04:09 +0800 Subject: [PATCH 2/4] Fix bug that will replace a unexpected text --- src/plugins/search/search.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 24cbb7db0..023c450c4 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -166,10 +166,12 @@ export function search(query) { end = postContent.length; } - let originKeyword = escapeHtml(postContent).substr( - indexContent, - keyword.length - ); + let originKeyword = escapeHtml(postContent) + .substring(indexContent, end) + .match(regEx); + originKeyword = Array.isArray(originKeyword) + ? originKeyword[0] + : keyword; const matchContent = '...' + From c3daf11ca51d9b63a50b3c5a14abcb23071dbaec Mon Sep 17 00:00:00 2001 From: huangzhhui Date: Thu, 23 Jul 2020 23:25:25 +0800 Subject: [PATCH 3/4] Fix bug that will replace a unexpected text --- src/plugins/search/search.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index a2d8a03b8..ea7c93d9d 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -166,21 +166,13 @@ export function search(query) { end = postContent.length; } - let originKeyword = escapeHtml(postContent) - .substring(indexContent, end) - .match(regEx); - originKeyword = Array.isArray(originKeyword) - ? originKeyword[0] - : keyword; - const matchContent = '...' + escapeHtml(postContent) .substring(start, end) - .replace( - regEx, - `${originKeyword}` - ) + + .replace(regEx, function(word) { + return `${word}`; + }) + '...'; resultStr += matchContent; From ad023c386ca550f57c38c3ff7c1f88f8caff7cb9 Mon Sep 17 00:00:00 2001 From: huangzhhui Date: Thu, 23 Jul 2020 23:37:11 +0800 Subject: [PATCH 4/4] Optimized --- src/plugins/search/search.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index ea7c93d9d..093904b0e 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -170,9 +170,10 @@ export function search(query) { '...' + escapeHtml(postContent) .substring(start, end) - .replace(regEx, function(word) { - return `${word}`; - }) + + .replace( + regEx, + word => `${word}` + ) + '...'; resultStr += matchContent;