-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (53 loc) · 2.01 KB
/
index.js
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
53
54
55
56
57
58
59
60
61
window.docsetSearch = {};
window.docsetSearch.updateResults = () => {
const resultsContainer = document.getElementById("results");
const queryInput = document.getElementById("query");
const results = window.docsetSearch.index.search(queryInput.value, { enrich: true });
const children = results.map((r) => {
const fieldContainer = document.createElement('div');
const fieldHeader = document.createElement('h2');
fieldHeader.innerText = r.field;
fieldContainer.appendChild(fieldHeader);
r.result.forEach((d) => {
const pTag = document.createElement('p');
const preTag = document.createElement('pre');
const codeTag = document.createElement('code');
codeTag.innerText = JSON.stringify(d, null, 2);
preTag.appendChild(codeTag);
pTag.appendChild(preTag);
fieldContainer.appendChild(pTag);
});
return fieldContainer;
});
resultsContainer.replaceChildren(...children);
}
document.addEventListener('DOMContentLoaded', () => {
fetch("./data.json").then((response) => {
if (response.ok) {
console.log("got an ok response");
const startTime = (new Date()).getTime();
response.json().then((json) => {
console.log("parsed json, now indexing");
const index = new FlexSearch.Document({
document: {
id: "id",
index: ["name", "doc"],
store: true
},
tokenize: "forward",
context: true
});
window.docsetSearch.documents = json["defs"].concat(json["namespaces"]);
for (const [i, doc] of window.docsetSearch.documents.entries()) {
index.add(Object.assign({ id: i }, doc));
}
window.docsetSearch.index = index;
const endTime = (new Date()).getTime();
console.log("all indexed", (endTime - startTime));
document.getElementById("query").addEventListener('keyup', debounce(window.docsetSearch.updateResults));
})
} else {
console.log("bad response loading data", response);
}
});
});