-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsearch.js
More file actions
195 lines (170 loc) · 7.45 KB
/
Copy pathsearch.js
File metadata and controls
195 lines (170 loc) · 7.45 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
window.addEventListener("DOMContentLoaded", function() {
const elSearchQuery = document.getElementById("srcf-search-input");
const elSearchResults = document.getElementById("srcf-search-results");
let selectedIndex = 0;
let searcher = null;
let searchIndex = null;
let searcherFetcher = null;
elSearchQuery.addEventListener("input", async function(event) {
if (searcherFetcher === null) {
searcherFetcher = fetch("/index.json")
.then(response => response.json())
.then(index => {
searchIndex = index;
fixupIndex(searchIndex);
searcher = new FlexSearch.Document({
document: {
id: "id",
index: [
{
field: "title-forward",
tokenize: "forward",
boost: 3
},
{
field: "title-tolerant",
tokenize: "tolerant",
boost: 3
},
{
field: "heading-title-forward",
tokenize: "forward",
boost: 1
},
{
field: "heading-title-tolerant",
tokenize: "tolerant",
boost: 1
},
{
field: "contents",
tokenize: "tolerant"
}
]
},
});
searchIndex.forEach(item => {
let headings = item.headings.map(h => h.title);
searcher.add({
"id": item.id,
"contents": item.contents,
"title-forward": item.title,
"title-tolerant": item.title,
"heading-title-tolerant": headings,
"heading-title-forward": headings,
"permalink": item.permalink
});
});
})
.catch(error => {
console.error("Error loading search index:", error);
searcherFetcher = null;
});
}
if (searcher === null) {
await searcherFetcher;
}
const searchQuery = event.target.value;
if (searchQuery.length > 0) {
elSearchResults.classList.remove("d-none");
executeSearch(searchQuery);
selectedIndex = 0;
updateSelected();
} else {
elSearchResults.classList.add("d-none");
}
});
elSearchQuery.addEventListener("keydown", function(event) {
const resultCount = elSearchResults.children.length;
if (event.key === "ArrowDown") {
event.preventDefault();
selectedIndex = (selectedIndex + 1) % resultCount;
updateSelected();
} else if (event.key === "ArrowUp") {
event.preventDefault();
selectedIndex = (selectedIndex - 1 + resultCount) % resultCount;
updateSelected();
} else if (event.key === "Enter") {
event.preventDefault();
elSearchResults.children[selectedIndex].click();
}
});
window.addEventListener("keydown", function(event) {
if (event.key === "/" && (event.ctrlKey || event.metaKey)) {
event.preventDefault();
elSearchQuery.focus();
elSearchQuery.select();
}
});
window.addEventListener("mousedown", function(event) {
if (!elSearchResults.contains(event.target) && event.target !== elSearchQuery) {
elSearchResults.classList.add("d-none");
}
});
function updateSelected() {
for (let child of elSearchResults.children) {
child.classList.remove("active");
if (child === elSearchResults.children[selectedIndex]) {
child.classList.add("active");
}
}
}
function fixupIndex(index) {
const parser = new DOMParser();
index.forEach((item, index) => {
item.id = index;
if (item.headingTags) {
item.headings = item.headingTags.map(tag => {
const element = parser.parseFromString(tag, "text/html").body.firstChild
return {
id: element.getAttribute("href").slice(1),
title: element.textContent
};
});
} else {
item.headings = [];
}
});
}
function executeSearch(searchQuery) {
var results = searcher.search({ query: searchQuery, merge: true, suggest: true });
console.log(results);
elSearchResults.innerHTML = "";
if (results.length == 0) {
const elNoResults = document.createElement("div");
elNoResults.className = "dropdown-item disabled";
elNoResults.innerText = "No matches found";
elSearchResults.appendChild(elNoResults);
return;
}
results.forEach((result, index) => {
const doc = searchIndex.find(doc => doc.id == result.id);
const elResult = document.createElement("a");
elResult.className = "dropdown-item";
elResult.href = doc.permalink;
elResult.innerText = doc.title;
// if the best match was a heading, conservatively estimate which one it was
// unfortunately this is the best we have as flexsearch won't highlight arrays at the moment
// TODO: find some kind of fix for the above
if (result.field.some(field => field.startsWith("heading-title")) && !result.field.some(field => field.startsWith("title"))) {
let terms = searchQuery.trim().toLowerCase().split(/ +/);
let estimatedHeadings = doc.headings.filter(h => terms.some(term => h.title.toLowerCase().includes(term)));
// if we know exactly which heading it was, show it.
// do not show if there are multiple matches in case we picked the wrong one - this could mislead
// users into thinking the content they want is not in the page as it is not under the suggested heading.
if (estimatedHeadings.length == 1) {
const subheader = document.createElement("small");
subheader.className = "text-muted search-result-subheader";
subheader.innerText = estimatedHeadings[0].title;
elResult.href += "#" + estimatedHeadings[0].id;
elResult.appendChild(subheader);
}
}
elResult.addEventListener("mousemove", function() {
selectedIndex = index;
updateSelected();
});
elSearchResults.appendChild(elResult);
});
}
});