Skip to content

Commit c7d55db

Browse files
committed
fix: search options
1 parent d6b203b commit c7d55db

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

assets/search.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,18 @@ class Search {
6666
if (this.fuse) return this.fuse;
6767
const all = await Promise.all(this.feeds.map((f) => this.fetchFeed(f)));
6868
const index = all.flat();
69-
this.fuse = new Fuse(index, { keys: ["title", "description"], threshold: 0.4 });
69+
this.fuse = new Fuse(index, { keys: ["title", "description"], threshold: 0.1, ignoreLocation: true, shouldSort: true });
7070
return this.fuse;
7171
}
7272

7373
// ---- rendering / highlighting ----
74+
getOptimalDescription(description, query) {
75+
const regex = new RegExp(Search.escapeRegExp(q), 'i');
76+
const idx = (description || '').search(regex);
77+
78+
return this.highlightText(description.slice(Math.max(description.lastIndexOf("\n", idx) + 1, Math.max(0, idx - 100)), Math.min(idx + 100, description.indexOf("\n", idx))), q);
79+
}
80+
7481
highlightText(text, query) {
7582
if (!query || !text) return text;
7683
try {
@@ -95,7 +102,7 @@ class Search {
95102
<div class="search-item">
96103
<a href="${item.url}${query ? '#search=' + encodeURIComponent(query) : ''}">
97104
<h4>${this.highlightText(item.title, query)}</h4>
98-
<p>${this.highlightText(item.description, query)}</p>
105+
<p>${this.getOptimalDescription(item.description, query)}</p>
99106
</a>
100107
<span class="label">${item.module}</span>
101108
</div>

0 commit comments

Comments
 (0)