forked from carlosperate/jekyll-theme-rtd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.html
executable file
·89 lines (81 loc) · 2.99 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
---
layout: base
title: Search
nav_exclude: true
search_exclude: true
---
<h1 id="search">Search Results</h1>
<div id="search-results" class="search-results">
Enter a search query in the search box on the left.
</div>
<!-- We only need to load the search dependencies in this page. -->
<script src="https://unpkg.com/lunr/lunr.js"></script>
<script type="text/javascript">
"use strict";
// First we figure out if there is a search query and show a "searching..." animation
var getQueryVariable = function(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (pair[0] === variable) {
return decodeURIComponent(pair[1].replace(/\+/g, '%20'));
}
}
};
var searchResults = document.getElementById('search-results');
var searchQuery = getQueryVariable('q');
var dotAnimation = null;
if (searchQuery) {
document.getElementById('search-query').setAttribute('value', searchQuery);
var dotsCount = 0;
dotAnimation = setInterval(function() {
dotsCount++;
var dots = new Array(dotsCount % 5).join('.');
searchResults.innerHTML = '<li>Searching' + dots + '</li>';
}, 500);
}
// Then we perform the search on page load
window.addEventListener('load', function() {
var displaySearchResults = function(results, store) {
clearInterval(dotAnimation);
if (results.length) {
var appendString = '';
for (var i = 0; i < results.length; i++) {
var item = store[results[i].ref];
appendString += '<li><a href="' + item.url + '"><h3>' + item.title + '</h3></a>';
appendString += '<p>' + item.content.substring(0, 150) + '...</p></li>';
}
searchResults.innerHTML = appendString;
} else {
searchResults.innerHTML = '<li>Your search did not match any documents. Please make sure that all words are spelled correctly and that you\'ve selected enough categories.</li>';
}
};
if (searchQuery) {
var idx = lunr(function() {
this.field('id');
this.field('title', { boost: 10 });
this.field('author');
this.field('content');
});
$.getJSON('/search_data.json').then(function(search_data) {
var idx = lunr(function() {
this.field('id');
this.field('title', { boost: 10 });
this.field('author');
this.field('content');
for (var key in search_data) {
this.add({
'id': key,
'title': search_data[key].title,
'author': search_data[key].author,
'content': search_data[key].content
});
}
});
var results = idx.search(searchQuery);
displaySearchResults(results, search_data);
});
}
});
</script>