Skip to content

Commit

Permalink
fix(search): clean markdown elements in search contents
Browse files Browse the repository at this point in the history
  • Loading branch information
Koooooo-7 committed Jun 30, 2024
1 parent 2948731 commit 9bb016a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ function escapeHtml(string) {
return String(string).replace(/[&<>"']/g, s => entityMap[s]);
}

function formatContent(text) {
return escapeHtml(cleanMarkdown(ignoreDiacriticalMarks(text)));
}

function cleanMarkdown(text) {
if (text) {
text = markdownToTxt(text);
Expand Down Expand Up @@ -183,19 +187,14 @@ export function search(query) {
keywords.forEach(keyword => {
// From https://github.com/sindresorhus/escape-string-regexp
const regEx = new RegExp(
escapeHtml(ignoreDiacriticalMarks(keyword)).replace(
/[|\\{}()[\]^$+*?.]/g,
'\\$&',
),
formatContent(keyword).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'),
'gi',
);
let indexTitle = -1;
let indexContent = -1;
handlePostTitle = postTitle
? escapeHtml(ignoreDiacriticalMarks(postTitle))
: postTitle;
handlePostTitle = postTitle ? formatContent(postTitle) : postTitle;
handlePostContent = postContent
? escapeHtml(ignoreDiacriticalMarks(postContent))
? formatContent(postContent)
: postContent;

indexTitle = postTitle ? handlePostTitle.search(regEx) : -1;
Expand Down Expand Up @@ -234,8 +233,8 @@ export function search(query) {

if (matchesScore > 0) {
const matchingPost = {
title: cleanMarkdown(handlePostTitle),
content: cleanMarkdown(postContent ? resultStr : ''),
title: formatContent(handlePostTitle),
content: formatContent(postContent ? resultStr : ''),
url: postUrl,
score: matchesScore,
};
Expand Down

0 comments on commit 9bb016a

Please sign in to comment.