diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-10-05 02:28:12 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-10-05 02:28:12 +0200 |
commit | 9ce3646d6e0d10035b6528e5384189fbed3d18c6 (patch) | |
tree | fc979cd4927178cf00fa3e3567b8c2c07176d338 /packages | |
parent | 45752fc79c9e3a5b7e84ed8572a8f0c12d8176b1 (diff) | |
download | gitbook-9ce3646d6e0d10035b6528e5384189fbed3d18c6.zip gitbook-9ce3646d6e0d10035b6528e5384189fbed3d18c6.tar.gz gitbook-9ce3646d6e0d10035b6528e5384189fbed3d18c6.tar.bz2 |
Add base for loading lunr index
Diffstat (limited to 'packages')
-rw-r--r-- | packages/gitbook-plugin-lunr/src/actions.js | 35 | ||||
-rw-r--r-- | packages/gitbook-plugin-lunr/src/index.js | 14 | ||||
-rw-r--r-- | packages/gitbook-plugin-lunr/src/reducer.js | 27 | ||||
-rw-r--r-- | packages/gitbook-plugin-search/src/actions/search.js | 7 |
4 files changed, 75 insertions, 8 deletions
diff --git a/packages/gitbook-plugin-lunr/src/actions.js b/packages/gitbook-plugin-lunr/src/actions.js new file mode 100644 index 0000000..8ead7ef --- /dev/null +++ b/packages/gitbook-plugin-lunr/src/actions.js @@ -0,0 +1,35 @@ +const GitBook = require('gitbook-core'); + +const TYPES = { + LOAD: 'lunr/load' +}; + +/** + * Load an index set + * @param {JSON} json + * @return {Action} + */ +function load(json) { + return { type: TYPES.LOAD, json }; +} + +/** + * Fetch an index + * @return {Action} + */ +function fetch() { + return (dispatch, getState) => { + return GitBook.Promise.resolve() + .then(() => { + // TODO: resolve the file correctly + return window.fetch('search_index.json'); + }) + .then(response => response.json()) + .then(json => dispatch(load(json))); + }; +} + +module.exports = { + TYPES, + fetch +}; diff --git a/packages/gitbook-plugin-lunr/src/index.js b/packages/gitbook-plugin-lunr/src/index.js index f1dc913..eab5975 100644 --- a/packages/gitbook-plugin-lunr/src/index.js +++ b/packages/gitbook-plugin-lunr/src/index.js @@ -1,20 +1,22 @@ const GitBook = require('gitbook-core'); -const lunr = require('lunr'); +const reduce = require('./reducer'); +const actions = require('./actions'); /** * Search in the local index * @param {String} query * @return {Promise<List>} */ -function searchHandler(query) { - return [ - { title: 'Hello world' } - ]; +function searchHandler(query, dispatch) { + return dispatch(actions.fetch()) + .then(() => { + return []; + }); } module.exports = GitBook.createPlugin({ activate: (dispatch, getState, { Search }) => { dispatch(Search.registerHandler('lunr', searchHandler)); }, - reduce: (state, action) => state + reduce }); diff --git a/packages/gitbook-plugin-lunr/src/reducer.js b/packages/gitbook-plugin-lunr/src/reducer.js new file mode 100644 index 0000000..48c3c23 --- /dev/null +++ b/packages/gitbook-plugin-lunr/src/reducer.js @@ -0,0 +1,27 @@ +const lunr = require('lunr'); +const GitBook = require('gitbook-core'); +const { Record } = GitBook.Immutable; + +const { TYPES } = require('./actions'); + +const LunrState = Record({ + idx: null, + store: {} +}); + +module.exports = GitBook.createReducer('lunr', (state, action) => { + state = state || LunrState(); + + switch (action.type) { + + case TYPES.LOAD: + return state + .set('idx', lunr.Index.load(action.json.index)) + .merge({ + store: action.json.store + }); + + default: + return state; + } +}); diff --git a/packages/gitbook-plugin-search/src/actions/search.js b/packages/gitbook-plugin-search/src/actions/search.js index 4ed3b53..8c1c08c 100644 --- a/packages/gitbook-plugin-search/src/actions/search.js +++ b/packages/gitbook-plugin-search/src/actions/search.js @@ -40,7 +40,7 @@ function handleQuery(q) { return clear(); } - return (dispatch, getState, { Navigation }) => { + return (dispatch, getState, actions) => { const { handlers } = getState().search; dispatch({ type: TYPES.START, query: q }); @@ -48,7 +48,7 @@ function handleQuery(q) { return Promise.reduce( handlers.toArray(), (results, handler) => { - return Promise.resolve(handler(q)) + return Promise.resolve(handler(q, dispatch, getState, actions)) .then(handlerResults => results.concat(handlerResults)); }, List() @@ -56,6 +56,9 @@ function handleQuery(q) { .then( results => { dispatch({ type: TYPES.END, query: q, results }); + }, + error => { + console.error(error); } ); }; |