Skip to content

Commit

Permalink
fix(plugin-seo): return empty if localized fields are only seo (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
thompsonsj authored Jun 8, 2023
1 parent 259a98a commit 02145ac
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
74 changes: 74 additions & 0 deletions src/utilities/getLocalizedFields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,4 +880,78 @@ describe("fn: getLocalizedFields", () => {
})
*/
})

/**
* @see https://github.com/payloadcms/plugin-seo
*
* payloadcms/plugin-seo adds localized fields.
* If there are no other localized fields, we don't
* want to submit to CrowdIn.
*/
describe("payloadcms/plugin-seo tests", () => {
const seoFields: Field[] = [
{
"name": "meta",
"label": "SEO",
"type": "group",
"fields": [
/**{
"name": "overview",
"label": "Overview",
"type": "ui",
"admin": {
"components": {}
}
},*/
{
"name": "title",
"type": "text",
"localized": true,
"admin": {
"components": {}
}
},
{
"name": "description",
"type": "textarea",
"localized": true,
"admin": {
"components": {}
}
},
/**{
"name": "preview",
"label": "Preview",
"type": "ui",
"admin": {
"components": {}
}
}**/
]
}
]

it("excludes payloadcms/plugin-seo localized fields if there are no localized fields on the collection/global", () => {
const nonLocalizedFieldCollection: Field[] = [
{
name: 'textLocalizedField',
type: 'text',
},
{
name: 'richTextLocalizedField',
type: 'richText',
},
{
name: 'textareaLocalizedField',
type: 'text',
},
]

const fields: Field[] = [
...nonLocalizedFieldCollection,
...seoFields,
]
expect(getLocalizedFields({ fields })).toEqual([])
})
})
})
22 changes: 21 additions & 1 deletion src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import deepEqual from 'deep-equal'
import { FieldWithName } from '../types'
import { slateToHtml, payloadSlateToDomConfig } from 'slate-serializers'
import type { Descendant } from 'slate'
import { isEmpty, merge } from "lodash"
import { get, isEmpty, merge } from "lodash"

const localizedFieldTypes = [
'richText',
Expand All @@ -27,6 +27,26 @@ export const getLocalizedFields = ({
fields: Field[],
type?: 'json' | 'html',
localizedParent?: boolean
}): any[] => {
const localizedFields = getLocalizedFieldsRecursive({
fields,
type,
localizedParent,
})
if (localizedFields.length === 1 && get(localizedFields[0], 'name') === 'meta') {
return []
}
return localizedFields
}

export const getLocalizedFieldsRecursive = ({
fields,
type,
localizedParent = false,
}: {
fields: Field[],
type?: 'json' | 'html',
localizedParent?: boolean
}): any[] => ([
...fields
// localized or group fields only.
Expand Down

0 comments on commit 02145ac

Please sign in to comment.