-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathalgoliasync.js
50 lines (47 loc) · 1.16 KB
/
algoliasync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const { request } = require("graphql-request");
const algoliasearch = require("algoliasearch");
//
// const query = `{
// allMarkdownRemark {
// edges {
// node {
// frontmatter {
// title
// }
// fields {
// slug
// }
// excerpt(pruneLength: 1000)
// }
// }
// }
// }`
//
// request('http://localhost:8000/___graphql', query)
// .then(data => {
// })
module.exports = {
syncToAlgolia: function syncToAlgolia(data) {
const client = algoliasearch(
"IY3ZFDIVWH",
"1fa689b14c46101df00641af78a8e1d6"
);
const index = client.initIndex("wekan");
const objects = data.allMarkdownRemark.edges
.map(edge => edge.node)
.map(node => ({
title: node.frontmatter.title,
objectID: node.fields.slug,
body: node.excerpt
}));
index.clearIndex((clearErr, clearContent) => {
index.saveObjects(objects, (err, content) => {
if (!err) {
console.log(`Successfully synced ${objects.length} items to Algolia`);
} else {
console.error(`Error while syncing to Algolia`, err);
}
});
});
}
};