Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

Feature/relative urls #71

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions js/jquery.lunr.search.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
this.$entries = $(options.entries, this.$results);
this.indexDataUrl = options.indexUrl;
this.index = this.createIndex();
this.category = options.category;
this.template = this.compileTemplate($(options.template));

this.initialize();
Expand Down Expand Up @@ -69,15 +70,35 @@

LunrSearch.prototype.populateIndex = function(data) {
var index = this.index;

var category = this.category

// format the raw json into a form that is simpler to work with
this.entries = $.map(data.entries, this.createEntry);

// if category is set only index documents with that category.
$.each(this.entries, function(idx, entry) {
index.add(entry);
if(category != ""){
if(entry.categories.contains(category)){
index.add(entry);
}
}else{
index.add(entry);
}

if(entry.url != ""){
entry.url = entry.url.replace("/", "");
}
});
};

Array.prototype.contains = function(element){
for(var i = 0; i < this.length; i++){
if(this[i].indexOf(element) > -1){
return true;
}
}
};

LunrSearch.prototype.createEntry = function(raw, index) {
var entry = $.extend({
id: index + 1
Expand Down Expand Up @@ -173,6 +194,7 @@
indexUrl : '/search.json', // Url for the .json file containing search index source data (containing: title, url, date, body)
results : '#search-results', // selector for containing search results element
entries : '.entries', // selector for search entries containing element (contained within results above)
template : '#search-results-template' // selector for Mustache.js template
template : '#search-results-template', // selector for Mustache.js template
category : ''
};
})(jQuery);