-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.json
More file actions
52 lines (43 loc) · 1.49 KB
/
Copy pathsearch.json
File metadata and controls
52 lines (43 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
let idx = null;
let documents = [];
fetch('/search.json')
.then(res => res.json())
.then(data => {
documents = data;
idx = lunr(function () {
this.ref('url');
this.field('title');
this.field('content');
data.forEach(doc => this.add(doc));
});
const input = document.getElementById('search-box');
const resultsContainer = document.getElementById('search-results');
input.addEventListener('input', function () {
const query = this.value.trim();
resultsContainer.innerHTML = '';
if (query.length < 2) return;
const results = idx.search(query);
if (results.length === 0) {
resultsContainer.innerHTML = '<p style="padding: 10px;">No results found.</p>';
return;
}
results.forEach(result => {
const item = documents.find(d => d.url === result.ref);
const div = document.createElement('div');
div.innerHTML = `
<div style="padding: 10px; border-bottom: 1px solid #ccc;">
<a href="${item.url}" style="color:#4CAF50; font-weight:bold;">${item.title}</a>
<p style="font-size: 0.9rem;">${item.content.slice(0, 100)}...</p>
</div>
`;
resultsContainer.appendChild(div);
});
});
});
document.addEventListener('click', function (e) {
const form = document.getElementById('search-form');
const resultsBox = document.getElementById('search-results');
if (!form.contains(e.target)) {
resultsBox.innerHTML = '';
}
});