-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.html
More file actions
32 lines (29 loc) · 956 Bytes
/
search.html
File metadata and controls
32 lines (29 loc) · 956 Bytes
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
---
title: Search
---
<h1>Search</h1>
<input id="q" type="search" placeholder="Type to search…" style="width:100%;padding:8px">
<ul id="results"></ul>
<script src="https://unpkg.com/lunr/lunr.js"></script>
<script>
fetch('{{ "/search.json" | relative_url }}').then(r=>r.json()).then(index=>{
const lunrIndex = lunr(function () {
this.ref('url'); this.field('title'); this.field('content');
index.forEach(doc => this.add(doc), this);
});
const input = document.getElementById('q');
const out = document.getElementById('results');
input.addEventListener('input', () => {
const q = input.value.trim();
out.innerHTML = '';
if (!q) return;
const hits = lunrIndex.search(q).slice(0,20);
hits.forEach(hit=>{
const doc = index.find(d=>d.url===hit.ref);
const li=document.createElement('li');
li.innerHTML = `<a href="${doc.url}">${doc.title}</a>`;
out.appendChild(li);
});
});
});
</script>