-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathsearch.html
More file actions
87 lines (75 loc) · 2.49 KB
/
Copy pathsearch.html
File metadata and controls
87 lines (75 loc) · 2.49 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
---
layout: page_default
title: 'Search'
---
<section>
<form action="{{site.baseurl}}/search.html" method="get" id="site-search">
<input type="text" id="search-box" name="query" placeholder="statement of joe spooky" />
<input type="submit" value="Search" />
</form>
<hr/>
<h3 class="search-results-header">Results:</h3>
<ul id="search-results"></ul>
</section>
<script>
window.store = {
{% for post in site.posts %}
"{{ post.url | slugify }}": {
"title": "{{ post.title | xml_escape }}",
"summary": "{{ post.summary | xml_escape }}",
"content": {{ post.content | strip_html | strip_newlines | jsonify }},
"url": "{{ post.url | xml_escape }}"
}
{% unless forloop.last %},{% endunless %}
{% endfor %}
};
</script>
<script src="{{site.baseurl}}/js/lunr.js"></script>
<script>
(function() {
function displaySearchResults(results, store) {
var searchResults = document.getElementById('search-results');
if (results.length) { // Are there any results?
var appendString = '';
for (var i = 0; i < results.length; i++) { // Iterate over the results
var item = store[results[i].ref];
appendString+='<li><a href="{{site.baseurl}}'+item.url+'">'+item.title+'</a>';
appendString+='<p>'+item.summary+'...</p></li>';
}
searchResults.innerHTML = appendString;
} else {
searchResults.innerHTML = '<li>No results found.</li>';
}
}
function getQueryVariable(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 searchTerm = getQueryVariable('query');
if (searchTerm) {
document.getElementById('search-box').setAttribute("value", searchTerm);
var idx = lunr(function () {
this.field('id');
this.field('title', { boost: 10 });
this.field('summary');
this.field('content');
for (var key in window.store) {
this.add({
'id': key,
'title': window.store[key].title,
'summary': window.store[key].summary,
'content': window.store[key].content
});
};
});
var results = idx.search(searchTerm); // Get lunr to perform a search
displaySearchResults(results, window.store);
}
})();
</script>