Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions packages/gatsby-source-npm-package-search/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const algoliasearch = require(`algoliasearch`)
const crypto = require(`crypto`)

const client = algoliasearch(`OFCNCOG2CU`, `f54e21fa3a2a0160595bb058179bfb1e`)
const client = algoliasearch(`OFCNCOG2CU`, `6fbcaeafced8913bf0e4d39f0b541957`)
var index = client.initIndex(`npm-search`)

const createContentDigest = obj =>
Expand All @@ -10,6 +10,17 @@ const createContentDigest = obj =>
.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 }
Expand All @@ -18,19 +29,15 @@ exports.sourceNodes = async (

console.log(`Grabbing local NPM packages...`)

let buildFilter = []

keywords.forEach(keyword => {
buildFilter.push(`keywords:${keyword}`)
})
const buildFilter = keywords.map(keyword => `keywords:${keyword}`)

const data = await index.search({
query: ``,
const hits = await browse({
index,
filters: `(${buildFilter.join(` OR `)})`,
hitsPerPage: 1000,
})

data.hits.forEach(hit => {
hits.forEach(hit => {
// commented changed remove all badges and images from readme content to keep the creation of the node from failing below
// if (hit.readme.includes(`![`)) {
// hit.readme = hit.readme.replace(/[[]?!\[.*\b/gi, ``)
Expand All @@ -52,7 +59,6 @@ exports.sourceNodes = async (
readmeNode.internal.contentDigest = createContentDigest(readmeNode)
// Remove unneeded data
delete hit.readme
delete hit._highlightResult
delete hit.versions

const node = {
Expand Down
2 changes: 1 addition & 1 deletion www/src/components/searchbar-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ class SearchBar extends Component {
return (
<div>
<InstantSearch
apiKey="f54e21fa3a2a0160595bb058179bfb1e"
apiKey="ae43b69014c017e05950a1cd4273f404"
appId="OFCNCOG2CU"
indexName="npm-search"
searchState={this.state.searchState}
Expand Down