forked from cockroachdb/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.html
184 lines (165 loc) · 4.94 KB
/
search.html
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
---
title: Search results
toc: false
contribute: false
---
<div id="search-stats"></div>
<div id="search-hits"></div>
<div id="search-pages"></div>
<script>
const queryString = (function() {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
const query_string = {};
const query = window.location.search.substring(1);
const vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
const pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
const arr = [query_string[pair[0]], decodeURIComponent(pair[1])];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]));
}
}
return (query_string.query || "").replace(/\+/g, " ");
})();
const router = instantsearch.routers.history({
windowTitle({ category, query }) {
const queryTitle = query ? `Results for "${query}"` : "Search";
return queryTitle;
},
createURL({ qsModule, routeState, location }) {
const urlParts = location.href.match(/^(.*?)\/search/);
const baseUrl = `${urlParts ? urlParts[1] : ""}/`;
const queryParameters = {};
const { query = "", page } = qsModule.parse(location.search.slice(1));
queryParameters.query = query;
if (page !== 1) {
queryParameters.page = routeState.page;
}
const queryString = qsModule.stringify(queryParameters, {
addQueryPrefix: true,
arrayFormat: "repeat"
});
return `${baseUrl}search.html${queryString}`;
},
parseURL({ qsModule, location }) {
const pathnameMatches = location.pathname.match(/search\/(.*?)\/?$/);
const { query = "", page } = qsModule.parse(location.search.slice(1));
return {
query,
page
};
}
});
const stateMapping = {
stateToRoute(uiState) {
return {
query: uiState["{{site.algolia.index_name}}"].query,
page: uiState["{{site.algolia.index_name}}"].page
};
},
routeToState(routeState) {
return {
// eslint-disable-next-line camelcase
"{{site.algolia.index_name}}": {
query: routeState.query,
page: routeState.page
}
};
}
};
$("#search-input").val(queryString);
const searchClient = algoliasearch(
"{{site.algolia.application_id}}",
"{{site.algolia.search_api_key}}"
);
const search = instantsearch({
indexName: "{{site.algolia.index_name}}",
routing: {
router,
stateMapping
},
searchClient
});
const addToResultsIfPresent = (match) => {
if(match && match.matchLevel && match.matchLevel !== 'none' && match.value) {
return (match.value || '') + '<br>';
}
return '';
}
const showResult = ({content, tags, title, html}) => {
if(content && content.value) {
return content.value
}
return addToResultsIfPresent(title) + addToResultsIfPresent(tags);
}
// Create the render function
const renderHits = (renderOptions, isFirstRender) => {
const { hits, widgetParams } = renderOptions;
var div = $("<div/>");
let results = "";
if (hits.length) {
results = hits
.map(
hit =>
`<div class="search-item">
<div class="search-title">
<a href="${hit.url}">${hit.title}</a>
</div>
<div class="search-snippet">${showResult(hit._highlightResult)}</div>
<div class="search-link">${hit.doc_type}</div>
</div>`
)
.join("");
} else {
results = "<p>No results.</p>";
}
widgetParams.container.innerHTML = results;
};
// Create the custom widget
const customHits = instantsearch.connectors.connectHits(renderHits);
search.addWidgets([
customHits({
container: document.querySelector("#search-hits")
}),
instantsearch.widgets.stats({
container: "#search-stats",
templates: {
text(data) {
let count = "";
if (data.hasManyResults) {
count += `${data.nbHits} results`;
} else if (data.hasOneResult) {
count += `1 result`;
} else {
count += `no result`;
}
return `${count} found for ‘${queryString}’`;
}
}
}),
instantsearch.widgets.pagination({
container: "#search-pages",
padding: 2,
showFirst: false,
showLast: false,
templates: {
previous: '<span class="fas fa-chevron-left" />',
next: '<span class="fas fa-chevron-right" />'
}
}),
instantsearch.widgets.configure({
hitsPerPage: 10,
query: queryString
})
]);
// Starting the search
search.start();
</script>