mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2025-04-30 04:23:30 +08:00
Use | operator to concatenate keywords
Idea from https://github.com/CaiJimmy/hugo-theme-stack/pull/436
This commit is contained in:
parent
d55e4f5e2d
commit
7593168032
@ -6,5 +6,7 @@
|
|||||||
"*"
|
"*"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"lib": ["es2020", "dom"],
|
||||||
|
"jsx": "preserve"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -104,6 +104,11 @@ class Search {
|
|||||||
const rawData = await this.getData();
|
const rawData = await this.getData();
|
||||||
let results: pageData[] = [];
|
let results: pageData[] = [];
|
||||||
|
|
||||||
|
const regex = new RegExp(keywords.filter((v, index, arr) => {
|
||||||
|
arr[index] = escapeRegExp(v);
|
||||||
|
return v !== '';
|
||||||
|
}).join('|'), 'gi');
|
||||||
|
|
||||||
for (const item of rawData) {
|
for (const item of rawData) {
|
||||||
let titleMatches: match[] = [],
|
let titleMatches: match[] = [],
|
||||||
contentMatches: match[] = [];
|
contentMatches: match[] = [];
|
||||||
@ -114,26 +119,20 @@ class Search {
|
|||||||
matchCount: 0
|
matchCount: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const keyword of keywords) {
|
const contentMatchAll = item.content.matchAll(regex);
|
||||||
if (keyword === '') continue;
|
for (const match of Array.from(contentMatchAll)) {
|
||||||
|
contentMatches.push({
|
||||||
|
start: match.index,
|
||||||
|
end: match.index + match[0].length
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const regex = new RegExp(escapeRegExp(keyword), 'gi');
|
const titleMatchAll = item.title.matchAll(regex);
|
||||||
|
for (const match of Array.from(titleMatchAll)) {
|
||||||
const contentMatchAll = item.content.matchAll(regex);
|
titleMatches.push({
|
||||||
for (const match of Array.from(contentMatchAll)) {
|
start: match.index,
|
||||||
contentMatches.push({
|
end: match.index + match[0].length
|
||||||
start: match.index,
|
});
|
||||||
end: match.index + match[0].length
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const titleMatchAll = item.title.matchAll(regex);
|
|
||||||
for (const match of Array.from(titleMatchAll)) {
|
|
||||||
titleMatches.push({
|
|
||||||
start: match.index,
|
|
||||||
end: match.index + match[0].length
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (titleMatches.length > 0) result.title = Search.processMatches(result.title, titleMatches).join('');
|
if (titleMatches.length > 0) result.title = Search.processMatches(result.title, titleMatches).join('');
|
||||||
|
Loading…
Reference in New Issue
Block a user