-
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 4 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 = await normalize.searchReplaceContentUrls({ | ||
entities, | ||
searchReplace, | ||
}) | ||
|
||
// creates nodes for each entry | ||
normalize.createNodesFromEntities({ entities, createNode }) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
const { setTimeout } = require(`timers`) | ||
const crypto = require(`crypto`) | ||
const deepMapKeys = require(`deep-map-keys`) | ||
const _ = require(`lodash`) | ||
|
@@ -234,6 +235,40 @@ exports.mapTagsCategoriesToTaxonomies = entities => | |
return e | ||
}) | ||
|
||
exports.searchReplaceContentUrls = async function ({ entities, 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. these functions don't need to be async — unless you're doing file or network IO this should be a normal synchronous function. 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. Your right, I am normally used to writing non blocking code for node.. I'll make this change 👍 |
||
|
||
if (!Array.isArray(searchReplace) && searchReplace.length !== 2) { | ||
return entities | ||
} | ||
|
||
const [search, replace] = searchReplace | ||
|
||
const _blacklist = [ | ||
`_links`, | ||
`__type`, | ||
] | ||
|
||
const blacklistProperties = function (obj = {}, blacklist = {}) { | ||
for (var i = 0; i < blacklist.length; i++) { | ||
eval(`delete obj.${blacklist[i]}`) | ||
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. you could just do 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. I used eval here because I wanted to account for a scenario where the blacklist goes deeper than one level for example: If you think we will never have to blacklist data in the object deeper than level one I can make this change :) 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. If we extend the blacklist we could change the code. |
||
} | ||
|
||
return obj | ||
} | ||
|
||
const final = entities.map(async (entity) => new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
var whiteListedEntities = JSON.stringify(blacklistProperties(entity, _blacklist)) | ||
var replacedString = whiteListedEntities.replace(new RegExp(search, `g`), replace) | ||
var parsed = JSON.parse(replacedString) | ||
resolve(_.defaultsDeep(parsed, entity)) | ||
}, 0) | ||
}) | ||
) | ||
|
||
return await Promise.all(final) | ||
} | ||
|
||
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.
Could we change this to use named fields e.g.
I find array-based APIs really ambiguous as the position of an item in an array isn't much of a cue to how you should use it or later, what it even means.
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.
Search & Replace option has been updated to accept an object rather than an array