Skip to content

Commit

Permalink
wip: add exclude keys option
Browse files Browse the repository at this point in the history
  • Loading branch information
WesselSmit committed Aug 26, 2024
1 parent adaf47b commit bdd417b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/entrypoints/ConfigScreen/ConfigScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export default function ConfigScreen({ ctx }: Props) {
selectedTranslationService.value === TranslationService.deepl ||
selectedTranslationService.value === TranslationService.deeplFree

console.warn('pluginParameters', pluginParameters)

return (
<Canvas ctx={ctx}>
<p>
This DatoCMS plugin gives you the ability to translate structured-text,
string and text fields.
string and text fields.!!
</p>

<Form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export default function ConfigScreen({ ctx }: Props) {
pluginGlobalParameters?.translationService ||
translationServiceOptions[0]

console.warn('pluginParameters', pluginParameters)
console.warn('pluginGlobalParameters', pluginGlobalParameters)

return (
<Canvas ctx={ctx}>
<p>This DatoCMS plugin.</p>
Expand Down
9 changes: 9 additions & 0 deletions src/lib/datocms-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,16 @@ export function getValueType(
key: string,
value: any,
currentType: PathType,
excludedKeys?: string,
): PathType {
if (excludedKeys) {
const excludedKeysArray = excludedKeys.split(',').map((key) => key.trim())

if (excludedKeysArray.includes(key)) {
return PathType.exclude
}
}

if (
key === 'itemTypeId' ||
key === 'itemId' ||
Expand Down
19 changes: 18 additions & 1 deletion src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,24 @@ export function paths(
return Object.keys(object).reduce((acc: any[], key: string) => {
const path = `${prev}${prev ? `.${key}` : key}`
const value = object[key]
let valueType = getValueType(key, value, type)
const excludedKeys = 'theme, variant' // TODO is still hardcoded
// Refactor to something like this
/**
*
* export function paths({
object,
prev = '',
type = PathType.text,
excludedKeys,
}: {
object: any
prev?: string
type?: PathType
excludedKeys?: string
}):
*/
let valueType = getValueType(key, value, type, excludedKeys)

if (
pathTypeIsObject.indexOf(valueType) === -1 &&
Expand Down
2 changes: 1 addition & 1 deletion src/lib/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function getRichTextTranslation(
const mappedValue = removePropertyRecursively(value, {
keysToRemove: ['itemId'],
})
const allPaths = paths(mappedValue)
const allPaths = paths(mappedValue, options.excludeKeys)
let translatedArray = mappedValue

for (const path of allPaths) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export type TranslationOptions = {
maxTokens: number
topP: number
}
excludeKeys?: string
}

export type Path = {
Expand Down Expand Up @@ -127,6 +128,7 @@ export enum PathType {
seo = 'seo',
slug = 'slug',
meta = 'meta',
exclude = 'exclude',
}

export type Models = Array<{ id: string }>

0 comments on commit bdd417b

Please sign in to comment.