summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-lunr
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-plugin-lunr')
-rw-r--r--packages/gitbook-plugin-lunr/src/actions.js6
-rw-r--r--packages/gitbook-plugin-lunr/src/index.js10
-rw-r--r--packages/gitbook-plugin-lunr/src/reducer.js4
3 files changed, 18 insertions, 2 deletions
diff --git a/packages/gitbook-plugin-lunr/src/actions.js b/packages/gitbook-plugin-lunr/src/actions.js
index 8ead7ef..4d878c7 100644
--- a/packages/gitbook-plugin-lunr/src/actions.js
+++ b/packages/gitbook-plugin-lunr/src/actions.js
@@ -19,6 +19,12 @@ function load(json) {
*/
function fetch() {
return (dispatch, getState) => {
+ const { idx } = getState().lunr;
+
+ if (idx) {
+ return GitBook.Promise.resolve();
+ }
+
return GitBook.Promise.resolve()
.then(() => {
// TODO: resolve the file correctly
diff --git a/packages/gitbook-plugin-lunr/src/index.js b/packages/gitbook-plugin-lunr/src/index.js
index eab5975..1135f51 100644
--- a/packages/gitbook-plugin-lunr/src/index.js
+++ b/packages/gitbook-plugin-lunr/src/index.js
@@ -7,10 +7,16 @@ const actions = require('./actions');
* @param {String} query
* @return {Promise<List>}
*/
-function searchHandler(query, dispatch) {
+function searchHandler(query, dispatch, getState) {
+ // Fetch the index if non loaded
return dispatch(actions.fetch())
+
+ // Execute the search
.then(() => {
- return [];
+ const { idx, store } = getState().lunr;
+ const results = idx.search(query);
+
+ return results.map(({ref}) => store.get(ref).toJS());
});
}
diff --git a/packages/gitbook-plugin-lunr/src/reducer.js b/packages/gitbook-plugin-lunr/src/reducer.js
index 48c3c23..7e317c4 100644
--- a/packages/gitbook-plugin-lunr/src/reducer.js
+++ b/packages/gitbook-plugin-lunr/src/reducer.js
@@ -4,6 +4,10 @@ const { Record } = GitBook.Immutable;
const { TYPES } = require('./actions');
+/*
+ We store the lunr index an the document index in the store.
+ */
+
const LunrState = Record({
idx: null,
store: {}