/*global jQuery */
/*!
* Simple Jekyll Search
* Christian Fei - https://github.com/christian-fei/Simple-Jekyll-Search *
*
*/
(function($) {
$.fn.simpleJekyllSearch = function(options) {
var settings = $.extend({
jsonFile : '/search.json',
jsonFormat : 'title,category,desc,url,date,shortdate',
template : '{title}',
searchResults : '.results',
searchResultsTitle : '
Search results
',
limit : '10',
noResults : 'Oh shucks
Nothing found :(
'
}, 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 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