Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/gatsby-source-wordpress/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ 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
searchReplace: ["https://source-url.com", "https://replacement-url.com"]
Copy link
Contributor

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.

searchReplace: {
  sourceUrl: `https://source-url.com`,
  replacementUrl: `https://replacement-url.com`,
},

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.

Copy link
Contributor Author

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

},
},
];
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,
searchReplace = [],
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be {}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops! missed this 👎

}
) => {
const { createNode } = boundActionCreators
Expand Down Expand Up @@ -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 })

Expand Down
43 changes: 43 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,49 @@ exports.mapTagsCategoriesToTaxonomies = entities =>
return e
})

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

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++) {
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(search, `g`), replace)
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