Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(gatsby-source-wordpress): Add searchAndReplace #31091

Merged
merged 17 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
26 changes: 26 additions & 0 deletions packages/gatsby-source-wordpress/__tests__/process-node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getImgSrcRemoteFileMatchesFromNodeString,
getImgTagMatchesWithUrl,
getWpLinkRegex,
searchAndReplaceNodeStrings,
} from "../dist/steps/source-nodes/create-nodes/process-node"

const wpUrl = `wp.fakesite.com`
Expand Down Expand Up @@ -43,3 +44,28 @@ test(`HTML link transformation regex matches links`, async () => {

expect(matches.length).toBe(2)
})

test(`Search and replace node strings using regex matches`, async () => {
const nodeString = `Some stuff in a random string

A new line with some stuff!

We need to test some <a href=\\"https://old-site.com/hi\\" />link</a> as well!`

const result = searchAndReplaceNodeStrings({
nodeString,
node: { __typename: "FakeTypeName", id: "cG9zdDo0OQ==" },
pluginOptions: {
searchAndReplace: [
{ search: "(S|s)ome stuff", replace: "some other thing" },
{ search: "https://old-site\.com", replace: "https://new-site.com" },
]
}
})

expect(result).toBe(`some other thing in a random string

A new line with some other thing!

We need to test some <a href=\\"https://new-site.com/hi\\" />link</a> as well!`)
})
64 changes: 64 additions & 0 deletions packages/gatsby-source-wordpress/docs/plugin-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
- [schema.requestConcurrency](#schemarequestconcurrency)
- [schema.previewRequestConcurrency](#schemapreviewrequestconcurrency)
- [excludeFieldNames](#excludefieldnames)
- [searchAndReplace](#searchandreplace)
- [searchAndReplace[].search](#searchandreplacesearch)
- [searchAndReplace[].replace](#searchandreplacereplace)
- [html](#html)
- [html.useGatsbyImage](#htmlusegatsbyimage)
- [html.imageMaxWidth](#htmlimagemaxwidth)
Expand Down Expand Up @@ -778,6 +781,67 @@ A list of field names to globally exclude from the ingested schema.

```

## searchAndReplace

An array of options to search and replace strings in nodes. See below for options.

**Field type**: `Array`

```js
{
resolve: `gatsby-source-wordpress`,
options: {
searchAndReplace: [
{
search: "https://some-url.com",
replace: "https://some-new-url.com",
},
],
},
}

```

### searchAndReplace[].search

The regex rule used to search a terme.

**Field type**: `String`

```js
{
resolve: `gatsby-source-wordpress`,
options: {
searchAndReplace: [
{
search: "https://some-url.com",
},
],
},
}

```

### searchAndReplace[].replace

The replacement string for each regex match.

**Field type**: `String`

```js
{
resolve: `gatsby-source-wordpress`,
options: {
searchAndReplace: [
{
replace: "https://some-new-url.com",
},
],
},
}

```

## html

Options related to html field processing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,45 @@ When using this option, be sure to gitignore the wordpress-cache directory in th
excludeFieldNames: [\`viewer\`],
`),
}),
searchAndReplace: Joi.array()
.description(`An array of options to search and replace strings in nodes. See below for options.`)
.allow(null)
.items(
Joi.object({
search: Joi.string()
.description(`The regex rule used to search a terme.`)
.meta({
example: wrapOptions(`
searchAndReplace: [
{
search: "https://some-url\.com"
},
],
`),
}),
replace: Joi.string()
.description(`The replacement string for each regex match.`)
.meta({
example: wrapOptions(`
searchAndReplace: [
{
replace: "https://some-new-url.com",
},
],
`),
})
})
)
.meta({
example: wrapOptions(`
searchAndReplace: [
{
search: "https://some-url\.com",
replace: "https://some-new-url.com",
},
],
`),
}),
html: Joi.object({
useGatsbyImage: Joi.boolean()
.default(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,36 @@ const replaceNodeHtmlLinks = ({ wpUrl, nodeString, node }) => {
return nodeString
}

// replaces specific string or regex with a given string from the plugin options config
const searchAndReplaceNodeStrings = ({ nodeString, node, pluginOptions }) => {
if (Array.isArray(pluginOptions?.searchAndReplace)) {
pluginOptions.searchAndReplace.forEach(({ search, replace }) => {
const searchRegex = new RegExp(search, `g`)

const stringMatches = execall(searchRegex, nodeString)

if (stringMatches.length) {
stringMatches.forEach(({ match }) => {
if (match) {
try {
nodeString = nodeString.replace(search, replace)
} catch (e) {
console.error(e)
console.warn(
formatLogMessage(
`Failed to process search and replace string in ${node.__typename} ${node.id}`
)
)
}
}
})
}
})
}

return nodeString
}

const processNodeString = async ({
nodeString,
node,
Expand All @@ -861,6 +891,7 @@ const processNodeString = async ({
replaceNodeHtmlImages,
replaceFileLinks,
replaceNodeHtmlLinks,
searchAndReplaceNodeStrings,
]

for (const nodeStringFilter of nodeStringFilters) {
Expand Down