Skip to content

Instantly share code, notes, and snippets.

@matiasgarciaisaia
Created October 21, 2015 18:53
Show Gist options
  • Save matiasgarciaisaia/848ac0057c3ed79f3508 to your computer and use it in GitHub Desktop.
Save matiasgarciaisaia/848ac0057c3ed79f3508 to your computer and use it in GitHub Desktop.
Autocomplete-enabled search box for a Middleman static site using middleman-search extension and jquery-ui
var lunrIndex = null;
var lunrData = null;
// Download index data
$.ajax({
url: '/index.json',
cache: true,
method: 'GET',
success: function(data) {
lunrData = data;
}
});
// Setup autocomplete field
$(function() {
$('#search').autocomplete({
source: function(request, response) {
if (lunrIndex == null) {
lunrIndex = lunr.Index.load(lunrData.index);
}
var result = _(lunrIndex.search(request.term)).take(50).pluck('ref').map(function(ref) {
return lunrData.docs[ref];
}).value();
if (result.length == 0) {
result = [{'noresults': true}]
}
response(result);
},
select: function(event, selected) {
if (!selected.item.noresults) {
window.location.href = selected.item.url;
}
},
open: function() {
$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function() {
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
}
}).autocomplete("instance")._renderItem = function(ul, item) {
// Copied from https://jqueryui.com/autocomplete/#custom-data
var content = item.noresults
? '<span class="noresults">No results found</span>'
: '<a href="' + item.url + '">' + item.title + '</a>';
$(this.menu.element).toggleClass('noresults', item.noresults);
return $("<li>").append(content).appendTo(ul);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment