diff --git a/packages/gatsby-source-wordpress/README.md b/packages/gatsby-source-wordpress/README.md index ed9d893c2f083..b1f756936c936 100644 --- a/packages/gatsby-source-wordpress/README.md +++ b/packages/gatsby-source-wordpress/README.md @@ -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", diff --git a/packages/gatsby-source-wordpress/src/__tests__/request-in-queue.js b/packages/gatsby-source-wordpress/src/__tests__/request-in-queue.js index 4c1ffd9a8f9ce..f5c7bb7fa5f91 100644 --- a/packages/gatsby-source-wordpress/src/__tests__/request-in-queue.js +++ b/packages/gatsby-source-wordpress/src/__tests__/request-in-queue.js @@ -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) }) @@ -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) }) }) diff --git a/packages/gatsby-source-wordpress/src/fetch.js b/packages/gatsby-source-wordpress/src/fetch.js index 6305afd5b59c0..daef071fdda51 100644 --- a/packages/gatsby-source-wordpress/src/fetch.js +++ b/packages/gatsby-source-wordpress/src/fetch.js @@ -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 ) @@ -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 { @@ -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 => { @@ -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) @@ -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 diff --git a/packages/gatsby-source-wordpress/src/normalize.js b/packages/gatsby-source-wordpress/src/normalize.js index 35f42a453c60c..24e2d9045686d 100644 --- a/packages/gatsby-source-wordpress/src/normalize.js +++ b/packages/gatsby-source-wordpress/src/normalize.js @@ -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 }) @@ -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 @@ -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) => { @@ -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) ) diff --git a/packages/gatsby-source-wordpress/src/request-in-queue.js b/packages/gatsby-source-wordpress/src/request-in-queue.js index b462e5022a98f..357bb215acb62 100644 --- a/packages/gatsby-source-wordpress/src/request-in-queue.js +++ b/packages/gatsby-source-wordpress/src/request-in-queue.js @@ -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}`)