Update search script

This commit is contained in:
Michael Rose 2014-08-22 13:50:42 -04:00
parent 349bde67a0
commit d2a1d52eb0
4 changed files with 54 additions and 40 deletions

View file

@ -5,10 +5,11 @@
<!-- Jekyll Simple Search option -->
<script>
$(document).ready(function() {
$('.search-field').simpleJekyllSearch({
jsonFile : '{{ site.url }}/search.json',
searchResults : '.search-results',
template : '<li><article><a href="{url}">{title} <span class="entry-date"><time datetime="{date}">{shortdate}</time></span></a></article></li>',
$('.search-field').jekyllSearch({
jsonFile: '{{ site.url }}/search.json',
searchResults: '.search-results',
template: '<li><article><a href="{url}">{title} <span class="entry-date"><time datetime="{date}">{shortdate}</time></span></a></article></li>',
fuzzy: true,
noResults: '<p>Nothing found.</p>'
});
});

View file

@ -6,23 +6,22 @@
*/
(function($) {
$.fn.simpleJekyllSearch = function(options) {
$.fn.jekyllSearch = function(options) {
var settings = $.extend({
jsonFile : '/search.json',
jsonFormat : 'title,category,desc,url,date,shortdate',
template : '<a href="{url}" title="{title}">{title}</a>',
template : '<a href="{url}" title="{desc}">{title}</a>',
searchResults : '.results',
searchResultsTitle : '<h4>Search results</h4>',
limit : '10',
noResults : '<p>Oh shucks<br/><small>Nothing found :(</small></p>'
}, options);
var properties = settings.jsonFormat.split(',');
var jsonData = [],
origThis = this,
searchResults = $(settings.searchResults);
var matches = [];
if(settings.jsonFile.length && searchResults.length){
$.ajax({
type: "GET",
@ -33,7 +32,7 @@
registerEvent();
},
error: function(x,y,z) {
console.log("***ERROR in simpleJekyllSearch.js***");
console.log("***ERROR in jekyllSearch.js***");
console.log(x);
console.log(y);
console.log(z);
@ -42,9 +41,15 @@
});
}
function registerEvent(){
origThis.keyup(function(e){
if(e.which === 13){
if(matches)
window.location = matches[0].url;
//follow the first link
// if(searchResults.children().length)
}
if($(this).val().length){
writeMatches( performSearch($(this).val()) );
}else{
@ -54,41 +59,48 @@
}
function performSearch(str){
var matches = [];
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;
for (var i = 0; i < jsonData.length; i++) {
var obj = jsonData[i];
for (key in obj) {
if(obj.hasOwnProperty(key)){
if (obj[key] instanceof Array){
var seen = false;
for (var j = 0; j < obj[key].length; j++){
if(obj[key][j].toLowerCase().indexOf(str.toLowerCase()) >= 0){
matches.push(obj);
break;
}
}
}else if (obj[key].toLowerCase().indexOf(str.toLowerCase()) >= 0){
matches.push(obj);
break;
}
}
}
}
});
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]]);
}
if(m && m.length){
for (var i = 0; i < m.length && i < settings.limit; i++) {
var obj = m[i];
output = settings.template;
output = output.replace(/\{(.*?)\}/g, function(match, property) {
return obj[property];
});
searchResults.append($(output));
}
});
}else{
searchResults.append( settings.noResults );
}
}
function clearSearchResults(){
searchResults.children().remove();
}

File diff suppressed because one or more lines are too long

View file

@ -6,6 +6,7 @@ sitemap: false
{
"title" : "{{ post.title | escape }}",
"category" : "{{ post.category }}",
"tags" : "{{ post.tags | array_to_sentence_string }}",
"url" : "{{ site.url }}{{ post.url }}",
"date" : "{{ post.date }}",
"shortdate" : "{{ post.date | date: '%B %d, %Y' }}"