From 227cc28e01cd48ce3f69d84b4d686273850d4942 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Sun, 14 Jan 2018 00:08:42 +0000 Subject: [PATCH] Search and replace the hostname in URLs. (#3498) * Search and replace the hostname in URLs. https://github.com/gatsbyjs/gatsby/issues/3450 * Fixed code styling issues * Missed backticks for the timers requirement * Updated readme with search and replace options * Removed async, wrapped json parse in a try catch and updated blacklist function * Updated search and replace api and updated documentation * Added logging for invalid search and replace option * make option more explicit, updated default value & removed redundant logging * Updated the variable name making it clearer --- packages/gatsby-source-wordpress/README.md | 5 ++ .../src/gatsby-node.js | 7 +++ .../gatsby-source-wordpress/src/normalize.js | 49 +++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/packages/gatsby-source-wordpress/README.md b/packages/gatsby-source-wordpress/README.md index 5ef5e5fd82500..6b2144a8b8678 100644 --- a/packages/gatsby-source-wordpress/README.md +++ b/packages/gatsby-source-wordpress/README.md @@ -83,6 +83,11 @@ plugins: [ // Set verboseOutput to true to display a verbose output on `npm run develop` or `npm run build` // It can help you debug specific API Endpoints problems verboseOutput: false, + // Search and Replace Urls across WordPress content + searchAndReplaceContentUrls: { + sourceUrl: "https://source-url.com", + replacementUrl: "https://replacement-url.com" + } }, }, ]; diff --git a/packages/gatsby-source-wordpress/src/gatsby-node.js b/packages/gatsby-source-wordpress/src/gatsby-node.js index 31bf8dfbf67be..8e5cf5493ded1 100644 --- a/packages/gatsby-source-wordpress/src/gatsby-node.js +++ b/packages/gatsby-source-wordpress/src/gatsby-node.js @@ -27,6 +27,7 @@ exports.sourceNodes = async ( auth = {}, verboseOutput, perPage = 100, + searchAndReplaceContentUrls = {}, } ) => { const { createNode } = boundActionCreators @@ -92,6 +93,12 @@ exports.sourceNodes = async ( createNode, }) + // Search and replace Content Urls + entities = normalize.searchReplaceContentUrls({ + entities, + searchAndReplaceContentUrls, + }) + // creates nodes for each entry normalize.createNodesFromEntities({ entities, createNode }) diff --git a/packages/gatsby-source-wordpress/src/normalize.js b/packages/gatsby-source-wordpress/src/normalize.js index 7c68b0ef24c33..cfb7d8162d7f6 100644 --- a/packages/gatsby-source-wordpress/src/normalize.js +++ b/packages/gatsby-source-wordpress/src/normalize.js @@ -234,6 +234,55 @@ exports.mapTagsCategoriesToTaxonomies = entities => return e }) +exports.searchReplaceContentUrls = function ({ entities, searchAndReplaceContentUrls }) { + + if ( + !(_.isPlainObject(searchAndReplaceContentUrls)) || + !(_.has(searchAndReplaceContentUrls, `sourceUrl`)) || + !(_.has(searchAndReplaceContentUrls, `replacementUrl`)) || + typeof searchAndReplaceContentUrls.sourceUrl !== `string` || + typeof searchAndReplaceContentUrls.replacementUrl !== `string` + ) { + return entities + } + + const { sourceUrl, replacementUrl } = searchAndReplaceContentUrls + + const _blacklist = [ + `_links`, + `__type`, + ] + + const blacklistProperties = function (obj = {}, blacklist = []) { + for (var i = 0; i < blacklist.length; i++) { + delete obj[blacklist[i]] + } + + return obj + } + + return entities.map(function (entity) { + const original = Object.assign({}, entity) + + try { + var whiteList = blacklistProperties(entity, _blacklist) + var replaceable = JSON.stringify(whiteList) + var replaced = replaceable.replace(new RegExp(sourceUrl, `g`), replacementUrl) + var parsed = JSON.parse(replaced) + } catch (e) { + console.log( + colorized.out( + e.message, + colorized.color.Font.FgRed + ) + ) + return original + } + + return _.defaultsDeep(parsed, original) + }) +} + exports.mapEntitiesToMedia = entities => { const media = entities.filter(e => e.__type === `wordpress__wp_media`)