Skip to content

Commit 4d537a7

Browse files
committed
[fix #529] Search plugin: matched text is replaced with search text.
1 parent 4b02177 commit 4d537a7

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/plugins/search/search.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,18 @@ export function search(query) {
165165
end = postContent.length;
166166
}
167167

168-
const matchContent =
169-
'...' +
170-
escapeHtml(postContent)
171-
.substring(start, end)
172-
.replace(regEx, `<em class="search-keyword">${keyword}</em>`) +
173-
'...';
174-
168+
let content = escapeHtml(postContent).substring(start, end);
169+
let matchKeywords = content.match(regEx);
170+
171+
matchKeywords = new Set(matchKeywords);
172+
matchKeywords.forEach(keyword => {
173+
content = content.replace(
174+
new RegExp(keyword, 'g'),
175+
`<em class="search-keyword">${keyword}</em>`
176+
);
177+
});
178+
179+
const matchContent = '...' + content + '...';
175180
resultStr += matchContent;
176181
}
177182
});

0 commit comments

Comments
 (0)