Skip to content

Commit 8b735ae

Browse files
committed
Perf:Update global search
1 parent 645c5ed commit 8b735ae

4 files changed

Lines changed: 73 additions & 9 deletions

File tree

_includes/scripts.html

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,31 @@
102102
searchInput: sinput,
103103
resultsContainer: sresults,
104104
json: '{{ site.baseurl }}/search.json',
105-
searchResultTemplate: '<li><a href="{url}">{title}</a></li>',
106-
noResultsText: '<li><p style="padding:0 15px; color:#999;">无内容</p></li>',
107-
limit: 8
105+
searchResultTemplate: '<li><a href="{url}"><div class="search-result-title">{title}</div><div class="search-result-meta"><i class="fas fa-calendar-alt"></i> {date}</div><div class="search-result-snippet">{content}</div></a></li>',
106+
noResultsText: '<li><p style="padding:15px; color:var(--text-main);">没有找到相关内容哦...</p></li>',
107+
limit: 8,
108+
templateMiddleware: function(prop, value, template) {
109+
if (prop === 'content') {
110+
const query = sinput.value.toLowerCase().trim();
111+
if (!query) return '';
112+
const contentLower = value.toLowerCase();
113+
const index = contentLower.indexOf(query);
114+
if (index === -1) {
115+
return value.substring(0, 80) + '...';
116+
}
117+
const start = Math.max(0, index - 30);
118+
const end = Math.min(value.length, index + query.length + 50);
119+
let snippet = value.substring(start, end);
120+
if (start > 0) snippet = '...' + snippet;
121+
if (end < value.length) snippet = snippet + '...';
122+
123+
// 高亮关键词
124+
const regex = new RegExp(`(${query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
125+
snippet = snippet.replace(regex, '<span class="search-highlight">$1</span>');
126+
return snippet;
127+
}
128+
return value;
129+
}
108130
});
109131
}
110132

assets/css/main.css

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,3 +545,39 @@ footer {
545545
font-weight: bold;
546546
color: var(--primary-color);
547547
}
548+
549+
/* 15. Search Results Enhancement */
550+
.search-result-title {
551+
font-weight: bold;
552+
font-size: 1.05em;
553+
color: var(--primary-color);
554+
margin-bottom: 4px;
555+
}
556+
557+
.search-result-meta {
558+
font-size: 0.8em;
559+
color: #888;
560+
margin-bottom: 6px;
561+
}
562+
563+
.search-result-snippet {
564+
font-size: 0.85em;
565+
color: var(--text-main);
566+
line-height: 1.4;
567+
display: -webkit-box;
568+
-webkit-line-clamp: 3;
569+
-webkit-box-orient: vertical;
570+
overflow: hidden;
571+
}
572+
573+
.search-highlight {
574+
background-color: rgba(255, 193, 7, 0.4);
575+
color: var(--primary-color);
576+
font-weight: bold;
577+
padding: 0 2px;
578+
border-radius: 2px;
579+
}
580+
581+
.dark-mode .search-highlight {
582+
background-color: rgba(255, 193, 7, 0.2);
583+
}

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"jsdom": "^29.0.1"
4+
}
5+
}

search.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
[
44
{% for post in site.posts %}
55
{
6-
"title" : "{{ post.title | escape }}",
7-
"category" : "{{ post.categories | join: ', ' }}",
8-
"tags" : "{{ post.tags | join: ', ' }}",
9-
"url" : "{{ site.baseurl }}{{ post.url }}",
10-
"date" : "{{ post.date | date: '%Y-%m-%d' }}"
6+
"title" : {{ post.title | jsonify }},
7+
"category" : {{ post.categories | join: ', ' | jsonify }},
8+
"tags" : {{ post.tags | join: ', ' | jsonify }},
9+
"url" : {{ site.baseurl | append: post.url | jsonify }},
10+
"date" : {{ post.date | date: '%Y-%m-%d' | jsonify }},
11+
"content" : {{ post.content | strip_html | strip_newlines | jsonify }}
1112
} {% unless forloop.last %},{% endunless %}
1213
{% endfor %}
13-
]
14+
]

0 commit comments

Comments
 (0)