forked from TryGhost/gatsby-source-ghost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert image urls to media nodes w/ local files
- Loading branch information
Showing
3 changed files
with
118 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,38 @@ | ||
const GhostAPI = require('./api'); | ||
const {createNodeFactories} = require('./nodes'); | ||
const {getImagesFromApiResults} = require('./lib'); | ||
|
||
exports.sourceNodes = async ({actions}, configOptions) => { | ||
const {createNode} = actions; | ||
exports.sourceNodes = async ({actions, createNodeId, store, cache}, configOptions) => { | ||
const {createNode, touchNode} = actions; | ||
const imageArgs = {createNode, createNodeId, touchNode, store, cache}; | ||
|
||
return Promise.all([ | ||
const [posts, tags, users] = await Promise.all([ | ||
GhostAPI.fetchAllPosts(configOptions), | ||
GhostAPI.fetchAllTags(configOptions), | ||
GhostAPI.fetchAllUsers(configOptions) | ||
]).then(([posts, tags, users]) => { | ||
const { | ||
buildPostNode, | ||
buildTagNode, | ||
buildAuthorNode | ||
} = createNodeFactories({posts, tags, users}); | ||
]); | ||
|
||
posts.forEach(post => createNode(buildPostNode(post))); | ||
tags.forEach(tag => createNode(buildTagNode(tag))); | ||
users.forEach(user => createNode(buildAuthorNode(user))); | ||
}); | ||
const { | ||
buildPostNode, | ||
buildTagNode, | ||
buildAuthorNode, | ||
buildMediaNode | ||
} = createNodeFactories({posts, tags, users}, imageArgs); | ||
|
||
for (const post of posts) { | ||
createNode(await buildPostNode(post)); | ||
} | ||
|
||
for (const tag of tags) { | ||
createNode(buildTagNode(tag)); | ||
} | ||
|
||
for (const user of users) { | ||
createNode(buildAuthorNode(user)); | ||
} | ||
|
||
const images = getImagesFromApiResults([posts, tags, users]); | ||
for (const image of images) { | ||
createNode(await buildMediaNode(image)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports.getImagesFromApiResults = results => results | ||
.reduce((acc, entities) => acc.concat(entities)) | ||
.reduce((acc, entity) => acc.concat([ | ||
entity.feature_image, | ||
entity.cover_image, | ||
entity.profile_image | ||
]), []) | ||
.filter(url => !!url) | ||
.map(url => ({id: url, url})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters