sanity-plugin-internationalized-array
Version:
Store localized fields in an array to save on attributes
34 lines (33 loc) • 1.28 kB
JavaScript
import { defineMigration, set } from "sanity/migrate";
/**
* The property which will be used to store the language identifier from v5.
*/
const LANGUAGE_FIELD_NAME = "language";
/**
* Creates a migration that automatically finds all internationalized array
* item objects and moves language identifiers from `_key` to the dedicated
* `language` field, generating new random `_key` values.
*
* Detection is automatic: the `object` handler is called for every object in
* every matching document. If the object has a `_type` matching
* `internationalizedArray*Value` and is missing the `language` field, it is
* migrated.
*/
function migrateToLanguageField(documentTypes) {
return defineMigration({
title: "Migrate internationalized array from _key to language",
documentTypes,
migrate: { object(node) {
let type = node._type, language = node[LANGUAGE_FIELD_NAME];
if (typeof type != "string" || !type.startsWith("internationalizedArray") || !type.endsWith("Value") || language && typeof language == "string") return;
let oldKey = node._key;
if (typeof oldKey == "string" && oldKey.length !== 0) return set({
...node,
_key: void 0,
[LANGUAGE_FIELD_NAME]: oldKey
});
} }
});
}
export { migrateToLanguageField };
//# sourceMappingURL=index.js.map