-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
78 lines (68 loc) · 2.04 KB
/
gatsby-node.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const algoliasearch = require(`algoliasearch`)
const crypto = require(`crypto`)
const client = algoliasearch(`OFCNCOG2CU`, `6fbcaeafced8913bf0e4d39f0b541957`)
var index = client.initIndex(`npm-search`)
const createContentDigest = obj =>
crypto
.createHash(`md5`)
.update(JSON.stringify(obj))
.digest(`hex`)
function browse({ index, ...params }) {
let hits = []
const browser = index.browseAll(params)
return new Promise((resolve, reject) => {
browser.on(`result`, content => (hits = hits.concat(content.hits)))
browser.on(`end`, () => resolve(hits))
browser.on(`error`, err => reject(err))
})
}
exports.sourceNodes = async (
{ boundActionCreators, createNodeId },
{ keywords }
) => {
const { createNode } = boundActionCreators
const buildFilter = keywords.map(keyword => `keywords:${keyword}`)
const hits = await browse({
index,
filters: `(${buildFilter.join(` OR `)})`,
hitsPerPage: 1000,
})
hits.forEach(hit => {
const parentId = createNodeId(`plugin ${hit.objectID}`)
const readmeNode = {
id: createNodeId(`readme ${hit.objectID}`),
parent: parentId,
slug: `/packages/en/${hit.objectID}`,
children: [],
internal: {
type: `NPMPackageReadme`,
mediaType: `text/markdown`,
content: hit.readme !== undefined ? hit.readme : ``,
},
}
readmeNode.internal.contentDigest = createContentDigest(readmeNode)
// Remove unneeded data
delete hit.readme
delete hit.versions
const node = {
...hit,
deprecated: `${hit.deprecated}`,
created: new Date(hit.created),
modified: new Date(hit.modified),
id: parentId,
parent: null,
children: [],
slug: `/packages/${hit.objectID}/`,
readme___NODE: readmeNode.id,
title: `${hit.objectID}`,
internal: {
type: `NPMPackage`,
content: hit.readme !== undefined ? hit.readme : ``,
},
}
node.internal.contentDigest = createContentDigest(node)
createNode(readmeNode)
createNode(node)
})
return
}