';
+ suggestions.forEach(suggestion => {
+ const highlightText = highlightKeyword(suggestion.text, keyword);
+ if (suggestion.novelId) {
+ html += `
+ ${suggestion.type}
+ ${highlightText}
+
`;
+ } else {
+ html += `
+ ${suggestion.type}
+ ${highlightText}
+
`;
+ }
+ });
+ html += '
';
+
+ $('#search-suggestions').html(html).removeClass('hidden');
+ } catch (error) {
+ console.error('加载搜索建议失败:', error);
+ $('#search-suggestions').html('').addClass('hidden');
+ }
+ }
+
+ // 高亮关键词
+ function highlightKeyword(text, keyword) {
+ if (!text || !keyword) return text;
+ const regex = new RegExp(`(${keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
+ return text.replace(regex, '';
novels.forEach(novel => {
const isInShelf = Store.isInShelfCache(novel.id);
- html += renderNovelCard(novel, isInShelf);
+ html += renderNovelCard(novel, isInShelf, keyword);
});
html += '
';
diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html
index 175dd87..7684766 100644
--- a/src/main/resources/templates/index.html
+++ b/src/main/resources/templates/index.html
@@ -275,6 +275,9 @@