Skip to content

Commit

Permalink
Search and replace the hostname in URLs. (gatsbyjs#3498)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
moshie authored and jastack committed Jan 24, 2018
1 parent c079cf2 commit 07dd840
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
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
searchAndReplaceContentUrls: {
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,
searchAndReplaceContentUrls = {},
}
) => {
const { createNode } = boundActionCreators
Expand Down Expand Up @@ -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 })

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

Expand Down

0 comments on commit 07dd840

Please sign in to comment.