Repository navigation
Expand file tree
/
Copy pathsimpleJekyllSearch.js
More file actions
89 lines (76 loc) · 3.01 KB
/
Copy pathsimpleJekyllSearch.js
File metadata and controls
89 lines (76 loc) · 3.01 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
88
89
(function($) {
$.fn.simpleJekyllSearch = function(options) {
var settings = $.extend({
jsonFile : '/search.json',
jsonFormat : 'title,tags,url,date',
template : '<li><article><a href="{url}">{title} <span class="entry-date"><time datetime="{date}">{date}</time></span></a></article></li>',
searchResults : '.search-results',
searchResultsTitle : '<h4>Search results:</h4>',
limit : '10',
noResults : '<p>Oh snap!<br/><small>We found nothing :(</small></p>'
}, options);
var properties = settings.jsonFormat.split(',');
var jsonData = [],
origThis = this,
searchResults = $(settings.searchResults);
if(settings.jsonFile.length && searchResults.length){
$.ajax({
type: "GET",
url: settings.jsonFile,
dataType: 'json',
success: function(data, textStatus, jqXHR) {
jsonData = data;
registerEvent();
},
error: function(x,y,z) {
console.log("***ERROR in simpleJekyllSearch.js***");
console.log(x);
console.log(y);
console.log(z);
// x.responseText should have what's wrong
}
});
}
function registerEvent(){
origThis.keyup(function(e){
if($(this).val().length){
writeMatches( performSearch($(this).val()));
}else{
clearSearchResults();
}
});
}
function performSearch(str){
var matches = [];
$.each(jsonData,function(i,entry){
for(var i=0;i<properties.length;i++)
if(entry[properties[i]] !== undefined && entry[properties[i]].toLowerCase().indexOf(str.toLowerCase()) !== -1){
matches.push(entry);
i=properties.length;
}
});
return matches;
}
function writeMatches(m) {
clearSearchResults();
searchResults.append( $(settings.searchResultsTitle) );
if(m.length){
$.each(m,function(i,entry){
if(i<settings.limit){
var output=settings.template;
for(var i=0;i<properties.length;i++){
var regex = new RegExp("\{" + properties[i] + "\}", 'g');
output = output.replace(regex, entry[properties[i]]);
}
searchResults.append($(output));
}
});
}else{
searchResults.append( settings.noResults );
}
}
function clearSearchResults(){
searchResults.children().remove();
}
}
}(Zepto));