Skip to content

Commit 07dd840

Browse files
moshiejastack
authored andcommitted
Search and replace the hostname in URLs. (gatsbyjs#3498)
* Search and replace the hostname in URLs. gatsbyjs#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
1 parent c079cf2 commit 07dd840

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

packages/gatsby-source-wordpress/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ plugins: [
8383
// Set verboseOutput to true to display a verbose output on `npm run develop` or `npm run build`
8484
// It can help you debug specific API Endpoints problems
8585
verboseOutput: false,
86+
// Search and Replace Urls across WordPress content
87+
searchAndReplaceContentUrls: {
88+
sourceUrl: "https://source-url.com",
89+
replacementUrl: "https://replacement-url.com"
90+
}
8691
},
8792
},
8893
];

packages/gatsby-source-wordpress/src/gatsby-node.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ exports.sourceNodes = async (
2727
auth = {},
2828
verboseOutput,
2929
perPage = 100,
30+
searchAndReplaceContentUrls = {},
3031
}
3132
) => {
3233
const { createNode } = boundActionCreators
@@ -92,6 +93,12 @@ exports.sourceNodes = async (
9293
createNode,
9394
})
9495

96+
// Search and replace Content Urls
97+
entities = normalize.searchReplaceContentUrls({
98+
entities,
99+
searchAndReplaceContentUrls,
100+
})
101+
95102
// creates nodes for each entry
96103
normalize.createNodesFromEntities({ entities, createNode })
97104

packages/gatsby-source-wordpress/src/normalize.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,55 @@ exports.mapTagsCategoriesToTaxonomies = entities =>
234234
return e
235235
})
236236

237+
exports.searchReplaceContentUrls = function ({ entities, searchAndReplaceContentUrls }) {
238+
239+
if (
240+
!(_.isPlainObject(searchAndReplaceContentUrls)) ||
241+
!(_.has(searchAndReplaceContentUrls, `sourceUrl`)) ||
242+
!(_.has(searchAndReplaceContentUrls, `replacementUrl`)) ||
243+
typeof searchAndReplaceContentUrls.sourceUrl !== `string` ||
244+
typeof searchAndReplaceContentUrls.replacementUrl !== `string`
245+
) {
246+
return entities
247+
}
248+
249+
const { sourceUrl, replacementUrl } = searchAndReplaceContentUrls
250+
251+
const _blacklist = [
252+
`_links`,
253+
`__type`,
254+
]
255+
256+
const blacklistProperties = function (obj = {}, blacklist = []) {
257+
for (var i = 0; i < blacklist.length; i++) {
258+
delete obj[blacklist[i]]
259+
}
260+
261+
return obj
262+
}
263+
264+
return entities.map(function (entity) {
265+
const original = Object.assign({}, entity)
266+
267+
try {
268+
var whiteList = blacklistProperties(entity, _blacklist)
269+
var replaceable = JSON.stringify(whiteList)
270+
var replaced = replaceable.replace(new RegExp(sourceUrl, `g`), replacementUrl)
271+
var parsed = JSON.parse(replaced)
272+
} catch (e) {
273+
console.log(
274+
colorized.out(
275+
e.message,
276+
colorized.color.Font.FgRed
277+
)
278+
)
279+
return original
280+
}
281+
282+
return _.defaultsDeep(parsed, original)
283+
})
284+
}
285+
237286
exports.mapEntitiesToMedia = entities => {
238287
const media = entities.filter(e => e.__type === `wordpress__wp_media`)
239288

0 commit comments

Comments
 (0)