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

View file

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