Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,18 @@ export function search(query) {
end = postContent.length;
}

const matchContent =
'...' +
escapeHtml(postContent)
.substring(start, end)
.replace(regEx, `<em class="search-keyword">${keyword}</em>`) +
'...';

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'),
`<em class="search-keyword">${keyword}</em>`
);
});

const matchContent = '...' + content + '...';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when the title matched keyword, new Set(matchKeywords) will filte the matchKeywords == null.

when the postContent has more than one matched and the keyword has the same case, like testCase2.

search a keyword testDemo, and testDemo have more than one in `postContent`. 

Using new Set(matchKeywords) to remove the duplicated replaces from testDemo to <em class="search-keyword">testDemo</em>.

resultStr += matchContent;
}
});
Expand Down