-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Search and replace the hostname in URLs. #3498
Changes from 7 commits
e8b1300
9ee6877
4985550
cfe0d9d
98d7dae
01b1a5b
4d2e510
829b1f2
4b94bbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ exports.sourceNodes = async ( | |
auth = {}, | ||
verboseOutput, | ||
perPage = 100, | ||
searchReplace = [], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops! missed this 👎 |
||
} | ||
) => { | ||
const { createNode } = boundActionCreators | ||
|
@@ -92,6 +93,12 @@ exports.sourceNodes = async ( | |
createNode, | ||
}) | ||
|
||
// Search and replace Content Urls | ||
entities = normalize.searchReplaceContentUrls({ | ||
entities, | ||
searchReplace, | ||
}) | ||
|
||
// creates nodes for each entry | ||
normalize.createNodesFromEntities({ entities, createNode }) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,6 +234,61 @@ exports.mapTagsCategoriesToTaxonomies = entities => | |
return e | ||
}) | ||
|
||
exports.searchReplaceContentUrls = function ({ entities, searchReplace }) { | ||
|
||
if ( | ||
!(_.isPlainObject(searchReplace)) || | ||
!(_.has(searchReplace, `sourceUrl`)) || | ||
!(_.has(searchReplace, `replacementUrl`)) || | ||
typeof searchReplace.sourceUrl !== `string` || | ||
typeof searchReplace.replacementUrl !== `string` | ||
) { | ||
console.log( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't log if person didn't use this option. |
||
colorized.out( | ||
`Invalid Search and Replace option`, | ||
colorized.color.Font.FgRed | ||
) | ||
) | ||
return entities | ||
} | ||
|
||
const { sourceUrl, replacementUrl } = searchReplace | ||
|
||
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`) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this option
searchAndReplaceURLs
— no reason not to be explicit :-)