Skip to content
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

Merged
merged 9 commits into from
Jan 14, 2018
5 changes: 5 additions & 0 deletions packages/gatsby-source-wordpress/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
searchAndReplaceURLs: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for changing my mind again but how about we name the option & function searchAndReplaceContentUrls? That's super clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think the comment above makes it clear enough, don't you think?

sourceUrl: "https://source-url.com",
replacementUrl: "https://replacement-url.com"
}
},
},
];
Expand Down
7 changes: 7 additions & 0 deletions packages/gatsby-source-wordpress/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ exports.sourceNodes = async (
auth = {},
verboseOutput,
perPage = 100,
searchAndReplaceURLs = {},
}
) => {
const { createNode } = boundActionCreators
Expand Down Expand Up @@ -92,6 +93,12 @@ exports.sourceNodes = async (
createNode,
})

// Search and replace Content Urls
entities = normalize.searchReplaceContentUrls({
entities,
searchAndReplaceURLs,
})

// creates nodes for each entry
normalize.createNodesFromEntities({ entities, createNode })

Expand Down
49 changes: 49 additions & 0 deletions packages/gatsby-source-wordpress/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,55 @@ exports.mapTagsCategoriesToTaxonomies = entities =>
return e
})

exports.searchReplaceContentUrls = function ({ entities, searchAndReplaceURLs }) {

if (
!(_.isPlainObject(searchAndReplaceURLs)) ||
!(_.has(searchAndReplaceURLs, `sourceUrl`)) ||
!(_.has(searchAndReplaceURLs, `replacementUrl`)) ||
typeof searchAndReplaceURLs.sourceUrl !== `string` ||
typeof searchAndReplaceURLs.replacementUrl !== `string`
) {
return entities
}

const { sourceUrl, replacementUrl } = searchAndReplaceURLs

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`)

Expand Down