summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-lunr/src/actions.js
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-plugin-lunr/src/actions.js')
-rw-r--r--packages/gitbook-plugin-lunr/src/actions.js43
1 files changed, 43 insertions, 0 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..765fa2e
--- /dev/null
+++ b/packages/gitbook-plugin-lunr/src/actions.js
@@ -0,0 +1,43 @@
+const GitBook = require('gitbook-core');
+
+const TYPES = {
+ LOAD: 'lunr/load'
+};
+const INDEX_FILENAME = 'search_index.json';
+
+/**
+ * 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) => {
+ const { lunr, file } = getState();
+ const { idx } = lunr;
+ const filePath = file.relative(INDEX_FILENAME);
+
+ if (idx) {
+ return GitBook.Promise.resolve();
+ }
+
+ return GitBook.Promise.resolve()
+ .then(() => {
+ return window.fetch(filePath);
+ })
+ .then(response => response.json())
+ .then(json => dispatch(load(json)));
+ };
+}
+
+module.exports = {
+ TYPES,
+ fetch
+};