-
Notifications
You must be signed in to change notification settings - Fork 11
/
pkgdown.js
72 lines (61 loc) · 1.45 KB
/
pkgdown.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
62
63
64
65
66
67
68
69
70
71
72
// Search ----------------------------------------------------------------------
var fuse;
$(function () {
// Initialise search index on focus
$("#search").focus(async function(e) {
if (fuse) {
return;
}
$(e.target).addClass("loading");
var response = await fetch('search.json');
var data = await response.json();
var options = {
keys: ["heading", "text", "code"],
ignoreLocation: true,
threshold: 0.1,
includeMatches: true,
includeScore: true,
};
fuse = new Fuse(data, options);
$(e.target).removeClass("loading");
});
// Use algolia autocomplete
var options = {
autoselect: true,
debug: true,
hint: false,
minLength: 2,
};
$("#search").autocomplete(options, [
{
name: "content",
source: searchFuse,
templates: {
suggestion: (s) => {
if (s.chapter == s.heading) {
return `${s.chapter}`;
} else {
return `${s.chapter} /<br> ${s.heading}`;
}
},
},
},
]).on('autocomplete:selected', function(event, s) {
window.location.href = s.path + "?q=" + q + "#" + s.id;
});
});
var q;
async function searchFuse(query, callback) {
await fuse;
var items;
if (!fuse) {
items = [];
} else {
q = query;
var results = fuse.search(query, { limit: 20 });
items = results
.filter((x) => x.score <= 0.75)
.map((x) => x.item);
}
callback(items);
}