-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
search.js
445 lines (387 loc) · 17.8 KB
/
search.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* Sphinx and Mkdocs search overrides
*/
var rtddata = require('./rtd-data');
var xss = require('xss/lib/index');
var MAX_RESULT_PER_SECTION = 3;
var MAX_SUBSTRING_LIMIT = 100;
/**
* Use try...catch block to append html to contents
*
* @param {Object} contents html element on which additional html is be appended
* @param {String} template underscore.js template string
* @param {Object} data template vars and their values
*/
function append_html_to_contents(contents, template, data) {
// underscore.js throws variable not defined error
// because of change of syntax in new versions.
// See: https://stackoverflow.com/a/25881231/8601393
try {
// this is the pre-1.7 syntax from Underscore.js
contents.append(
$u.template(
template,
data
)
);
}
catch (error) {
// this is the new syntax
contents.append(
$u.template(template)(data)
);
}
}
/*
* Search query override for hitting our local API instead of the standard
* Sphinx indexer. This will fall back to the standard indexer on an API
* failure,
*/
function attach_elastic_search_query_sphinx(data) {
var project = data.project;
var version = data.version;
var language = data.language || 'en';
var query_override = function (query) {
var search_def = $.Deferred();
var search_url = document.createElement('a');
search_url.href = data.proxied_api_host + '/api/v2/search/';
search_url.search = '?q=' + $.urlencode(query) + '&project=' + project +
'&version=' + version + '&language=' + language;
/*
* Compatibility function to set a text to a Node or JQuery object.
*/
var setText = function (node, text) {
if (node.jquery) {
node.text(text);
} else {
node.innerText = text;
}
};
search_def
.then(function (data) {
var results = data.results || [];
if (results.length) {
for (var i = 0; i < results.length; i += 1) {
var result = results[i];
var blocks = result.blocks;
var list_item = $('<li>');
var title = result.title;
// if highlighted title is present, use that.
if (result.highlights.title.length) {
title = xss(result.highlights.title[0]);
}
var link = result.path + "?highlight=" + $.urlencode(query);
var item = $('<a>', {'href': link});
item.html(title);
item.find('span').addClass('highlighted');
list_item.append(item);
// If the document is from a subproject, add extra information
if (result.project !== project) {
var text = " (from project " + result.project_alias + ")";
var extra = $('<span>', {'text': text});
list_item.append(extra);
}
for (var block_index = 0; block_index < blocks.length; block_index += 1) {
var current_block = blocks[block_index];
var contents = $('<div class="context">');
var section_template =
'<div>' +
'<a href="<%= section_subtitle_link %>">' +
'<%= section_subtitle %>' +
'</a>' +
'</div>' +
'<% for (var i = 0; i < section_content.length; ++i) { %>' +
'<div>' +
'<%= section_content[i] %>' +
'</div>' +
'<% } %>';
var domain_template =
'<div>' +
'<a href="<%= domain_subtitle_link %>">' +
'<%= domain_subtitle %>' +
'</a>' +
'</div>' +
'<div>' +
'<%= domain_content %>' +
'</div>';
// if the result is page section
if (current_block.type === "section") {
var section = current_block;
var section_subtitle = section.title;
var section_subtitle_link = link + "#" + section.id;
var section_content = [section.content.substr(0, MAX_SUBSTRING_LIMIT) + " ..."];
if (section.highlights.title.length) {
section_subtitle = xss(section.highlights.title[0]);
}
if (section.highlights.content.length) {
var content = section.highlights.content;
section_content = [];
for (
var k = 0;
k < content.length && k < MAX_RESULT_PER_SECTION;
k += 1
) {
section_content.push("... " + xss(content[k]) + " ...");
}
}
append_html_to_contents(
contents,
section_template,
{
section_subtitle_link: section_subtitle_link,
section_subtitle: section_subtitle,
section_content: section_content
}
);
}
// if the result is a sphinx domain object
if (current_block.type === "domain") {
var domain = current_block;
var domain_role_name = domain.role;
var domain_subtitle_link = link + "#" + domain.id;
var domain_name = domain.name;
var domain_content = "";
if (domain.content !== "") {
domain_content = domain.content.substr(0, MAX_SUBSTRING_LIMIT) + " ...";
}
if (domain.highlights.content.length) {
domain_content = "... " + xss(domain.highlights.content[0]) + " ...";
}
if (domain.highlights.name.length) {
domain_name = xss(domain.highlights.name[0]);
}
var domain_subtitle = "[" + domain_role_name + "]: " + domain_name;
append_html_to_contents(
contents,
domain_template,
{
domain_subtitle_link: domain_subtitle_link,
domain_subtitle: domain_subtitle,
domain_content: domain_content
}
);
}
contents.find('span').addClass('highlighted');
list_item.append(contents);
// Create some spacing between the results.
// Also, don't add this spacing in the last hit.
if (block_index < blocks.length - 1) {
list_item.append($("<div class='rtd_search_hits_spacing'></div>"));
}
}
if (Search.output.jquery) {
Search.output.append(list_item);
} else {
Search.output.appendChild(list_item.get(0));
}
}
setText(
Search.status,
_('Search finished, found %s page(s) matching the search query.').replace('%s', results.length)
);
} else {
console.log('Read the Docs search failed. Falling back to Sphinx search.');
Search.query_fallback(query);
}
})
.fail(function (error) {
console.debug('Read the Docs search failed. Falling back to Sphinx search.');
Search.query_fallback(query);
})
.always(function () {
$('#search-progress').empty();
Search.stopPulse();
setText(Search.title, _('Search Results'))
});
$.ajax({
url: search_url.href,
crossDomain: true,
xhrFields: {
withCredentials: true,
},
complete: function (resp, status_code) {
if (
status_code !== 'success' ||
typeof (resp.responseJSON) === 'undefined' ||
resp.responseJSON.count === 0
) {
return search_def.reject();
}
return search_def.resolve(resp.responseJSON);
}
})
.fail(function (resp, status_code, error) {
return search_def.reject();
});
};
if (typeof Search !== 'undefined' && project && version) {
// Do not replace the built-in search if RTD's docsearch is disabled
if (!data.features || !data.features.docsearch_disabled) {
var query_fallback = Search.query;
Search.query_fallback = query_fallback;
Search.query = query_override;
} else {
console.log('Server side search is disabled.');
}
}
$(document).ready(function () {
if (typeof Search !== 'undefined') {
Search.init();
}
});
}
/*
* Mkdocs search override for hitting our API instead of the standard Mkdocs search index.
* This will fall back to the original search on an API failure.
*/
function attach_elastic_search_query_mkdocs(data) {
var project = data.project;
var version = data.version;
var language = data.language || 'en';
var fallbackSearch = function () {
if (typeof window.doSearchFallback !== 'undefined') {
window.doSearchFallback();
} else {
console.log('Unable to fallback to original MkDocs search.');
}
};
var doSearch = function () {
var query = document.getElementById('mkdocs-search-query').value;
var search_def = $.Deferred();
var search_url = document.createElement('a');
search_url.href = data.proxied_api_host + '/api/v2/search/';
search_url.search = '?q=' + encodeURIComponent(query) + '&project=' + project +
'&version=' + version + '&language=' + language;
search_def
.then(function (data) {
var results = data.results || [];
if (results.length) {
var searchResults = $('#mkdocs-search-results');
searchResults.empty();
for (var i = 0; i < results.length; i += 1) {
var result = results[i];
var blocks = result.blocks;
var link = result.path;
var item = $('<article>');
item.append(
$('<h3>').append($('<a>', {'href': link, 'text': result.title}))
);
if (result.project !== project) {
var text = '(from project ' + result.project_alias + ')';
item.append($('<span>', {'text': text}));
}
for (var j = 0; j < blocks.length; j += 1) {
var section = blocks[j];
if (section.type === 'section') {
var section_link = link + '#' + section.id;
var section_title = section.title;
var section_content = section.content;
if (section_content.length > MAX_SUBSTRING_LIMIT) {
section_content = section_content.substr(0, MAX_SUBSTRING_LIMIT) + " ...";
}
var section_contents = [section_content];
if (section.highlights.title.length) {
section_title = section.highlights.title[0];
}
if (section.highlights.content.length) {
var contents = section.highlights.content;
section_contents = [];
for (
var k = 0;
k < contents.length && k < MAX_RESULT_PER_SECTION;
k += 1
) {
section_contents.push("... " + contents[k] + " ...");
}
}
section_title = xss(section_title)
.replace(/<span>/g, '<mark>')
.replace(/<\/span>/g, '</mark>');
item.append(
$('<h4>')
.append($('<a>', {'href': section_link}).html(section_title))
);
for (var m = 0; m < section_contents.length; m += 1) {
var content = xss(section_contents[m]);
content = content
.replace(/<span>/g, '<mark>')
.replace(/<\/span>/g, '</mark>');
item.append(
$('<p>').html(content)
);
}
searchResults.append(item);
}
}
}
} else {
console.log('Read the Docs search returned 0 result. Falling back to MkDocs search.');
fallbackSearch();
}
})
.fail(function (error) {
console.log('Read the Docs search failed. Falling back to MkDocs search.');
fallbackSearch();
});
$.ajax({
url: search_url.href,
crossDomain: true,
xhrFields: {
withCredentials: true,
},
complete: function (resp, status_code) {
if (
status_code !== 'success' ||
typeof (resp.responseJSON) === 'undefined' ||
resp.responseJSON.count === 0
) {
return search_def.reject();
}
return search_def.resolve(resp.responseJSON);
}
})
.fail(function (resp, status_code, error) {
return search_def.reject();
});
};
var initSearch = function () {
var search_input = document.getElementById('mkdocs-search-query');
if (search_input) {
search_input.addEventListener('keyup', doSearch);
}
var term = window.getSearchTermFromLocation();
if (term) {
search_input.value = term;
doSearch();
}
};
$(document).ready(function () {
// We can't override the search completely,
// because we can't delete the original event listener,
// and MkDocs includes its search functions after ours.
// If MkDocs is loaded before, this will trigger a double search
// (but ours will have precendece).
// Note: this function is only available on Mkdocs >=1.x
window.doSearchFallback = window.doSearch;
window.doSearch = doSearch;
window.initSearch = initSearch;
initSearch();
});
}
function init() {
var data = rtddata.get();
if (data.is_sphinx_builder()) {
// Check to disabled server side search for Sphinx
// happens inside the function, because we still need to call Search.init().
attach_elastic_search_query_sphinx(data);
}
// MkDocs projects should have this flag explicitly for now.
else if (data.features && !data.features.docsearch_disabled) {
attach_elastic_search_query_mkdocs(data);
} else {
console.log('Server side search is disabled.');
}
}
module.exports = {
init: init
};