summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core/search.js
diff options
context:
space:
mode:
Diffstat (limited to 'theme/javascript/core/search.js')
-rw-r--r--theme/javascript/core/search.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/theme/javascript/core/search.js b/theme/javascript/core/search.js
new file mode 100644
index 0000000..f38e86f
--- /dev/null
+++ b/theme/javascript/core/search.js
@@ -0,0 +1,41 @@
+define([
+ "jQuery",
+ "lodash",
+ "lunr",
+], function($, _, lunr) {
+ var index = null;
+
+ // Load complete index
+ var loadIndex = function() {
+ return $.getJSON("search_index.json")
+ .then(function(data) {
+ index = lunr.Index.load(data);
+ });
+ };
+
+ // Search for a term
+ var search = function(q) {
+ if (!index) return;
+ var results = _.chain(index.search(q))
+ .map(function(result) {
+ var parts = result.ref.split("#")
+ return {
+ path: parts[0],
+ hash: parts[1]
+ }
+ })
+ .value();
+
+ return results;
+ };
+
+
+ var init = function() {
+ loadIndex();
+ };
+
+ return {
+ init: init,
+ search: search
+ };
+}); \ No newline at end of file