Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gatsby-source-wordpress] Handle all media types #4817

Merged
merged 11 commits into from
Apr 4, 2018
12 changes: 6 additions & 6 deletions packages/gatsby-source-wordpress/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ We welcome PRs adding support for data from other plugins.
// In your gatsby-config.js
plugins: [
/*
* Gatsby's data processing layer begins with “source”
* plugins. Here the site sources its data from Wordpress.
*/
* Gatsby's data processing layer begins with “source”
* plugins. Here the site sources its data from Wordpress.
*/
{
resolve: "gatsby-source-wordpress",
options: {
/*
* The base URL of the Wordpress site without the trailingslash and the protocol. This is required.
* Example : 'gatsbyjsexamplewordpress.wordpress.com' or 'www.example-site.com'
*/
* The base URL of the Wordpress site without the trailingslash and the protocol. This is required.
* Example : 'gatsbyjsexamplewordpress.wordpress.com' or 'www.example-site.com'
*/
baseUrl: "gatsbyjsexamplewordpress.wordpress.com",
// The protocol. This can be http or https.
protocol: "http",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const requestInQueue = require(`../request-in-queue`)
const axios = require(`axios`)

axios.mockImplementation(opts => {
if (opts.throw) { throw new Error(opts.throw) }
if (opts.throw) {
throw new Error(opts.throw)
}

return opts.url.slice(opts.url.lastIndexOf(`/`) + 1)
})
Expand All @@ -28,7 +30,7 @@ describe(`requestInQueue`, () => {
it(`runs all requests in queue`, async () => {
await requestInQueue(requests)

requests.forEach((req) => {
requests.forEach(req => {
expect(axios).toHaveBeenCalledWith(req)
})
})
Expand Down
42 changes: 34 additions & 8 deletions packages/gatsby-source-wordpress/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,15 @@ async function fetchData({
if (_verbose) console.time(`Fetching the ${type} took`)

let routeResponse = await getPages(
{ url, _perPage, _hostingWPCOM, _auth, _accessToken, _verbose, _concurrentRequests },
{
url,
_perPage,
_hostingWPCOM,
_auth,
_accessToken,
_verbose,
_concurrentRequests,
},
1
)

Expand Down Expand Up @@ -270,7 +278,15 @@ async function fetchData({
* @returns
*/
async function getPages(
{ url, _perPage, _hostingWPCOM, _auth, _accessToken, _concurrentRequests, _verbose },
{
url,
_perPage,
_hostingWPCOM,
_auth,
_accessToken,
_concurrentRequests,
_verbose,
},
page = 1
) {
try {
Expand Down Expand Up @@ -320,9 +336,13 @@ async function getPages(
}

// We got page 1, now we want pages 2 through totalPages
const pageOptions = _.range(2, totalPages + 1).map(getPage => getOptions(getPage))
const pageOptions = _.range(2, totalPages + 1).map(getPage =>
getOptions(getPage)
)

const pages = await requestInQueue(pageOptions, { concurrent: _concurrentRequests })
const pages = await requestInQueue(pageOptions, {
concurrent: _concurrentRequests,
})

const pageData = pages.map(page => page.data)
pageData.forEach(list => {
Expand Down Expand Up @@ -383,10 +403,17 @@ function getValidRoutes({
console.log(
colorized.out(`Invalid route.`, colorized.color.Font.FgRed)
)
} else if (_excludedRoutes.some(excludedRoute => minimatch(routePath, excludedRoute))) {
} else if (
_excludedRoutes.some(excludedRoute =>
minimatch(routePath, excludedRoute)
)
) {
if (_verbose)
console.log(
colorized.out(`Excluded route from excludedRoutes pattern.`, colorized.color.Font.FgYellow)
colorized.out(
`Excluded route from excludedRoutes pattern.`,
colorized.color.Font.FgYellow
)
)
} else {
if (_verbose)
Expand Down Expand Up @@ -475,8 +502,7 @@ const getRawEntityType = route =>
* @param {any} baseUrl The base site URL that should be removed
* @param {any} fullUrl The full URL to retrieve the route path from
*/
const getRoutePath = (baseUrl, fullUrl) =>
fullUrl.replace(baseUrl, ``)
const getRoutePath = (baseUrl, fullUrl) => fullUrl.replace(baseUrl, ``)

/**
* Extract the route manufacturer
Expand Down
18 changes: 9 additions & 9 deletions packages/gatsby-source-wordpress/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,13 @@ exports.mapTagsCategoriesToTaxonomies = entities =>
return e
})

exports.mapElementsToParent = entities => entities.map(e => {
exports.mapElementsToParent = entities =>
entities.map(e => {
if (e.wordpress_parent) {
// Create parent_element with a link to the parent node of type.
e.parent_element___NODE = entities
.find(t => t.wordpress_id === e.wordpress_parent && t.__type === e.__type).id
e.parent_element___NODE = entities.find(
t => t.wordpress_id === e.wordpress_parent && t.__type === e.__type
).id
}
return e
})
Expand Down Expand Up @@ -295,10 +297,8 @@ exports.mapEntitiesToMedia = entities => {
? true
: false

const photoRegex = /\.(gif|jpg|jpeg|tiff|png)$/i
const isPhotoUrl = filename =>
_.isString(filename) && photoRegex.test(filename)
const isPhotoUrlAlreadyProcessed = key => key == `source_url`
const isURL = value => _.isString(value) && value.startsWith(`http`)
const isMediaUrlAlreadyProcessed = key => key == `source_url`
const isFeaturedMedia = (value, key) =>
(_.isNumber(value) || _.isBoolean(value)) && key === `featured_media`
// ACF Gallery and similarly shaped arrays
Expand All @@ -308,7 +308,7 @@ exports.mapEntitiesToMedia = entities => {

// Try to get media node from value:
// - special case - check if key is featured_media and value is photo ID
// - check if value is photo url
// - check if value is media url
// - check if value is ACF Image Object
// - check if value is ACF Gallery
const getMediaFromValue = (value, key) => {
Expand All @@ -319,7 +319,7 @@ exports.mapEntitiesToMedia = entities => {
: null,
deleteField: true,
}
} else if (isPhotoUrl(value) && !isPhotoUrlAlreadyProcessed(key)) {
} else if (isURL(value) && !isMediaUrlAlreadyProcessed(key)) {
const mediaNodeID = getMediaItemID(
media.find(m => m.source_url === value)
)
Expand Down
12 changes: 7 additions & 5 deletions packages/gatsby-source-wordpress/src/request-in-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ async function handleQueue(task, cb) {
* @param {Options} opts Options that will be given to better-queue
* @return {Promise} Resolves with the accumulated values from the tasks
*/
module.exports = function requestInQueue (tasks, opts = {}) {
module.exports = function requestInQueue(tasks, opts = {}) {
return new Promise((res, rej) => {
const q = new Queue(handleQueue, { ..._defaults, ...opts })

const taskMap = new Map(tasks.map((t) => {
q.push(t)
return [t.url, null]
}))
const taskMap = new Map(
tasks.map(t => {
q.push(t)
return [t.url, null]
})
)

q.on(`task_failed`, (id, err) => {
rej(`${id} failed with err: ${err}`)
Expand Down