@portabletext/sanity-bridge 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -37
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/portable-text-member-schema-types.ts","../src/portable-text-member-schema-types-to-schema.ts","../src/sanity-schema-to-portable-text-schema.ts","../src/schema-definition-to-portable-text-member-schema-types.ts"],"sourcesContent":[],"mappings":";;;;AAgBA;;AAES,KAFG,6BAAA,GAEH;EACO,WAAA,EAAA,CAFA,gBAEA,GAAA;IACF,YAAA,CAAA,EAAA,MAAA;EACG,CAAA,CAAA,EAAA;EACe,KAAA,EAJvB,gBAIuB;EAAhB,YAAA,EAHA,gBAGA,EAAA;EACR,UAAA,EAHM,wBAGN,EAAA;EACE,aAAA,EAHO,gBAGP,EAAA;EACD,YAAA,EAHO,eAGP,CAHuB,iBAGvB,CAAA;EAAmB,IAAA,EAFpB,gBAEoB;EAQZ,MAAA,EATN,oBASM,EAAA;EACoB,KAAA,EAT3B,mBAS2B,EAAA;CAAhB;;;;AC1BpB;;ACsBgB,iBFGA,mCAAA,CEHgC,gBAAA,EFI5B,eEJ4B,CFIZ,iBEJY,CAAA,CAAA,EFK7C,6BEL6C;;AFdhD;;;;AAIc,iBCZE,qCAAA,CDYF,MAAA,ECXJ,6BDWI,CAAA,ECVX,MDUW;;AAJd;;;;;;;;;;;;AAiBA;;;;;;ACzBA;;ACsBA;;AAC2C,iBAD3B,gCAAA,CAC2B,YAAA,EAA3B,eAA2B,CAAA,OAAA,CAAA,GAAA,eAAA,CAAA,EACxC,MADwC;;AFf3C;;;;AAIc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/portable-text-member-schema-types.ts","../src/portable-text-member-schema-types-to-schema.ts","../src/sanity-schema-to-portable-text-schema.ts","../src/schema-definition-to-portable-text-member-schema-types.ts"],"sourcesContent":[],"mappings":";;;;AAgBA;;AAES,KAFG,6BAAA,GAEH;EACO,WAAA,EAAA,CAFA,gBAEA,GAAA;IACF,YAAA,CAAA,EAAA,MAAA;EACG,CAAA,CAAA,EAAA;EACe,KAAA,EAJvB,gBAIuB;EAAhB,YAAA,EAHA,gBAGA,EAAA;EACR,UAAA,EAHM,wBAGN,EAAA;EACE,aAAA,EAHO,gBAGP,EAAA;EACD,YAAA,EAHO,eAGP,CAHuB,iBAGvB,CAAA;EAAmB,IAAA,EAFpB,gBAEoB;EAQZ,MAAA,EATN,oBASM,EAAA;EACoB,KAAA,EAT3B,mBAS2B,EAAA;CAAhB;;;;AC1BpB;;ACsBgB,iBFGA,mCAAA,CEHgC,gBAAA,EFI5B,eEJ4B,CFIZ,iBEJY,CAAA,CAAA,EFK7C,6BEL6C;;AFdhD;;;;AAIc,iBCZE,qCAAA,CDYF,MAAA,ECXJ,6BDWI,CAAA,ECVX,MDUW;;AAJd;;;;;;;;;;;;AAiBA;;;;;;ACzBA;;ACsBA;;AAC2C,iBAD3B,gCAAA,CAC2B,YAAA,EAA3B,eAA2B,CAAA,OAAA,CAAA,GAAA,eAAA,CAAA,EACxC,MADwC;;AFf3C;;;;AAIc,iBGME,sDAAA,CHNF,UAAA,CAAA,EGOC,gBHPD,CAAA,EGQX,6BHRW"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Schema } from "@sanity/schema";
|
|
2
2
|
import { builtinTypes } from "@sanity/schema/_internal";
|
|
3
|
-
import { defineType, defineField
|
|
3
|
+
import { defineType, defineField } from "@sanity/types";
|
|
4
4
|
function createPortableTextMemberSchemaTypes(portableTextType) {
|
|
5
5
|
if (!portableTextType)
|
|
6
6
|
throw new Error("Parameter 'portabletextType' missing (required)");
|
|
@@ -162,7 +162,7 @@ function startCase(str) {
|
|
|
162
162
|
const defaultObjectTitles = {
|
|
163
163
|
image: "Image",
|
|
164
164
|
url: "URL"
|
|
165
|
-
}, sanityBuiltinNames = /* @__PURE__ */ new Set(["image", "url"]);
|
|
165
|
+
}, sanityBuiltinNames = /* @__PURE__ */ new Set(["file", "geopoint", "image", "slug", "url"]);
|
|
166
166
|
function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
167
167
|
const blockObjectDefs = definition?.blockObjects ?? [], inlineObjectDefs = definition?.inlineObjects ?? [], blockObjectNameSet = new Set(
|
|
168
168
|
blockObjectDefs.map((blockObject) => blockObject.name)
|
|
@@ -251,41 +251,15 @@ function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
|
251
251
|
}), schema = Schema.compile({
|
|
252
252
|
types: [portableTextSchema, ...blockObjects, ...inlineObjects]
|
|
253
253
|
}).get("portable-text"), pteSchema = createPortableTextMemberSchemaTypes(schema);
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
for (const field of schemaType.fields)
|
|
264
|
-
if (!(field.name !== "children" || !isArraySchemaType(field.type)))
|
|
265
|
-
for (const ofSchemaType of field.type.of) {
|
|
266
|
-
const nameMapping2 = inlineObjectNames[ofSchemaType.name];
|
|
267
|
-
nameMapping2 && (ofSchemaType.name = nameMapping2);
|
|
268
|
-
}
|
|
269
|
-
return schemaType;
|
|
270
|
-
})
|
|
271
|
-
},
|
|
272
|
-
blockObjects: pteSchema.blockObjects.map(
|
|
273
|
-
(blockObject) => blockObjectNames[blockObject.name] !== void 0 ? {
|
|
274
|
-
...blockObject,
|
|
275
|
-
name: blockObjectNames[blockObject.name],
|
|
276
|
-
type: {
|
|
277
|
-
...blockObject.type,
|
|
278
|
-
name: blockObjectNames[blockObject.name]
|
|
279
|
-
}
|
|
280
|
-
} : blockObject
|
|
281
|
-
),
|
|
282
|
-
inlineObjects: pteSchema.inlineObjects.map(
|
|
283
|
-
(inlineObject) => inlineObjectNames[inlineObject.name] !== void 0 ? {
|
|
284
|
-
...inlineObject,
|
|
285
|
-
name: inlineObjectNames[inlineObject.name]
|
|
286
|
-
} : inlineObject
|
|
287
|
-
)
|
|
288
|
-
};
|
|
254
|
+
for (const blockObject of pteSchema.blockObjects) {
|
|
255
|
+
const originalName = blockObjectNames[blockObject.name];
|
|
256
|
+
originalName !== void 0 && (blockObject.name = originalName, blockObject.type && (blockObject.type = { ...blockObject.type, name: originalName }));
|
|
257
|
+
}
|
|
258
|
+
for (const inlineObject of pteSchema.inlineObjects) {
|
|
259
|
+
const originalName = inlineObjectNames[inlineObject.name];
|
|
260
|
+
originalName !== void 0 && (inlineObject.name = originalName, inlineObject.type && (inlineObject.type = { ...inlineObject.type, name: originalName }));
|
|
261
|
+
}
|
|
262
|
+
return pteSchema;
|
|
289
263
|
}
|
|
290
264
|
export {
|
|
291
265
|
compileSchemaDefinitionToPortableTextMemberSchemaTypes,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/portable-text-member-schema-types.ts","../src/portable-text-member-schema-types-to-schema.ts","../src/sanity-schema-to-portable-text-schema.ts","../src/key-generator.ts","../src/start-case.ts","../src/schema-definition-to-portable-text-member-schema-types.ts"],"sourcesContent":["import type {\n ArraySchemaType,\n BlockDecoratorDefinition,\n BlockListDefinition,\n BlockSchemaType,\n BlockStyleDefinition,\n ObjectSchemaType,\n PortableTextBlock,\n SchemaType,\n SpanSchemaType,\n} from '@sanity/types'\n\n/**\n * @public\n * Sanity-specific schema types for Portable Text.\n */\nexport type PortableTextMemberSchemaTypes = {\n annotations: (ObjectSchemaType & {i18nTitleKey?: string})[]\n block: ObjectSchemaType\n blockObjects: ObjectSchemaType[]\n decorators: BlockDecoratorDefinition[]\n inlineObjects: ObjectSchemaType[]\n portableText: ArraySchemaType<PortableTextBlock>\n span: ObjectSchemaType\n styles: BlockStyleDefinition[]\n lists: BlockListDefinition[]\n}\n\n/**\n * @public\n * Create Sanity-specific schema types for Portable Text from a Sanity array\n * schema type.\n */\nexport function createPortableTextMemberSchemaTypes(\n portableTextType: ArraySchemaType<PortableTextBlock>,\n): PortableTextMemberSchemaTypes {\n if (!portableTextType) {\n throw new Error(\"Parameter 'portabletextType' missing (required)\")\n }\n const blockType = portableTextType.of?.find(findBlockType) as\n | BlockSchemaType\n | undefined\n if (!blockType) {\n throw new Error('Block type is not defined in this schema (required)')\n }\n const childrenField = blockType.fields?.find(\n (field) => field.name === 'children',\n ) as {type: ArraySchemaType} | undefined\n if (!childrenField) {\n throw new Error('Children field for block type found in schema (required)')\n }\n const ofType = childrenField.type.of\n if (!ofType) {\n throw new Error(\n 'Valid types for block children not found in schema (required)',\n )\n }\n const spanType = ofType.find((memberType) => memberType.name === 'span') as\n | ObjectSchemaType\n | undefined\n if (!spanType) {\n throw new Error('Span type not found in schema (required)')\n }\n const inlineObjectTypes = (ofType.filter(\n (memberType) => memberType.name !== 'span',\n ) || []) as ObjectSchemaType[]\n const blockObjectTypes = (portableTextType.of?.filter(\n (field) => field.name !== blockType.name,\n ) || []) as ObjectSchemaType[]\n return {\n styles: resolveEnabledStyles(blockType),\n decorators: resolveEnabledDecorators(spanType),\n lists: resolveEnabledListItems(blockType),\n block: blockType,\n span: spanType,\n portableText: portableTextType,\n inlineObjects: inlineObjectTypes,\n blockObjects: blockObjectTypes,\n annotations: (spanType as SpanSchemaType).annotations,\n }\n}\n\nfunction resolveEnabledStyles(blockType: ObjectSchemaType) {\n const styleField = blockType.fields?.find(\n (btField) => btField.name === 'style',\n )\n if (!styleField) {\n throw new Error(\n \"A field with name 'style' is not defined in the block type (required).\",\n )\n }\n const textStyles =\n styleField.type.options?.list &&\n styleField.type.options.list?.filter(\n (style: {value: string}) => style.value,\n )\n if (!textStyles || textStyles.length === 0) {\n throw new Error(\n 'The style fields need at least one style ' +\n \"defined. I.e: {title: 'Normal', value: 'normal'}.\",\n )\n }\n return textStyles\n}\n\nfunction resolveEnabledDecorators(spanType: ObjectSchemaType) {\n return (spanType as any).decorators\n}\n\nfunction resolveEnabledListItems(blockType: ObjectSchemaType) {\n const listField = blockType.fields?.find(\n (btField) => btField.name === 'listItem',\n )\n if (!listField) {\n throw new Error(\n \"A field with name 'listItem' is not defined in the block type (required).\",\n )\n }\n const listItems =\n listField.type.options?.list &&\n listField.type.options.list.filter((list: {value: string}) => list.value)\n if (!listItems) {\n throw new Error('The list field need at least to be an empty array')\n }\n return listItems\n}\n\nfunction findBlockType(type: SchemaType): BlockSchemaType | null {\n if (type.type) {\n return findBlockType(type.type)\n }\n\n if (type.name === 'block') {\n return type as BlockSchemaType\n }\n\n return null\n}\n","import type {Schema} from '@portabletext/schema'\nimport type {PortableTextMemberSchemaTypes} from './portable-text-member-schema-types'\n\n/**\n * @public\n * Convert Sanity-specific schema types for Portable Text to a first-class\n * Portable Text schema.\n */\nexport function portableTextMemberSchemaTypesToSchema(\n schema: PortableTextMemberSchemaTypes,\n): Schema {\n return {\n annotations: schema.annotations.map((annotation) => ({\n name: annotation.name,\n fields: annotation.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: annotation.title,\n })),\n block: {\n name: schema.block.name,\n },\n blockObjects: schema.blockObjects.map((blockObject) => ({\n name: blockObject.name,\n fields: blockObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: blockObject.title,\n })),\n decorators: schema.decorators.map((decorator) => ({\n name: decorator.value,\n title: decorator.title,\n value: decorator.value,\n })),\n inlineObjects: schema.inlineObjects.map((inlineObject) => ({\n name: inlineObject.name,\n fields: inlineObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: inlineObject.title,\n })),\n span: {\n name: schema.span.name,\n },\n styles: schema.styles.map((style) => ({\n name: style.value,\n title: style.title,\n value: style.value,\n })),\n lists: schema.lists.map((list) => ({\n name: list.value,\n title: list.title,\n value: list.value,\n })),\n }\n}\n","import type {Schema} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport {builtinTypes} from '@sanity/schema/_internal'\nimport type {ArrayDefinition, ArraySchemaType} from '@sanity/types'\nimport {createPortableTextMemberSchemaTypes} from './portable-text-member-schema-types'\nimport {portableTextMemberSchemaTypesToSchema} from './portable-text-member-schema-types-to-schema'\n\n/**\n * @public\n * Compile a Sanity schema to a Portable Text `Schema`.\n *\n * A Portable Text `Schema` is compatible with a Portable Text\n * `SchemaDefinition` and can be used as configuration for the Portable Text\n * Editor.\n *\n * @example\n * ```tsx\n * const schema = sanitySchemaToPortableTextSchema(sanitySchema)\n *\n * return (\n * <EditorProvider\n * initialConfig={{\n * // ...\n * schemaDefinition: schema,\n * }}\n * >\n * // ...\n * </EditorProvider>\n * ```\n */\nexport function sanitySchemaToPortableTextSchema(\n sanitySchema: ArraySchemaType<unknown> | ArrayDefinition,\n): Schema {\n const portableTextMemberSchemaTypes = createPortableTextMemberSchemaTypes(\n sanitySchema.hasOwnProperty('jsonType')\n ? sanitySchema\n : compileType(sanitySchema),\n )\n\n return portableTextMemberSchemaTypesToSchema(portableTextMemberSchemaTypes)\n}\n\nfunction compileType(rawType: any) {\n return SanitySchema.compile({\n name: 'blockTypeSchema',\n types: [rawType, ...builtinTypes],\n }).get(rawType.name)\n}\n","export const keyGenerator = (): string => randomKey(12)\n\nconst getByteHexTable = (() => {\n let table: any[]\n return () => {\n if (table) {\n return table\n }\n\n table = []\n for (let i = 0; i < 256; ++i) {\n table[i] = (i + 0x100).toString(16).slice(1)\n }\n return table\n }\n})()\n\n// WHATWG crypto RNG - https://w3c.github.io/webcrypto/Overview.html\nfunction whatwgRNG(length = 16) {\n const rnds8 = new Uint8Array(length)\n crypto.getRandomValues(rnds8)\n return rnds8\n}\n\nfunction randomKey(length?: number): string {\n const table = getByteHexTable()\n return whatwgRNG(length)\n .reduce((str, n) => str + table[n], '')\n .slice(0, length)\n}\n","export function startCase(str: string): string {\n return (\n str\n // Insert space before uppercase letters in camelCase (e.g., 'fooBar' -> 'foo Bar')\n .replace(/([a-z])([A-Z])/g, '$1 $2')\n // Replace underscores and dashes with spaces\n .replace(/[_-]+/g, ' ')\n // Trim and split on whitespace\n .trim()\n .split(/\\s+/)\n .filter(Boolean)\n // Capitalize first letter of each word, preserve rest\n .map((word) => word.charAt(0).toUpperCase() + word.slice(1))\n .join(' ')\n )\n}\n","import type {SchemaDefinition} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport {\n defineField,\n defineType,\n isArraySchemaType,\n isObjectSchemaType,\n type ObjectSchemaType,\n} from '@sanity/types'\nimport {keyGenerator} from './key-generator'\nimport {\n createPortableTextMemberSchemaTypes,\n type PortableTextMemberSchemaTypes,\n} from './portable-text-member-schema-types'\nimport {startCase} from './start-case'\n\nconst defaultObjectTitles: Record<string, string> = {\n image: 'Image',\n url: 'URL',\n}\n\n/**\n * Names that conflict with Sanity's built-in schema types and need temporary\n * names during `SanitySchema.compile` to avoid getting default fields added.\n */\nconst sanityBuiltinNames = new Set(['image', 'url'])\n\n/**\n * @public\n * Compile a Portable Text schema definition to Sanity-specific schema types for\n * Portable Text.\n */\nexport function compileSchemaDefinitionToPortableTextMemberSchemaTypes(\n definition?: SchemaDefinition,\n): PortableTextMemberSchemaTypes {\n const blockObjectDefs = definition?.blockObjects ?? []\n const inlineObjectDefs = definition?.inlineObjects ?? []\n\n // Collect names that appear in both blockObjects and inlineObjects, or that\n // conflict with Sanity built-in types. These need temporary names so that\n // `SanitySchema.compile` doesn't see duplicate type registrations.\n const blockObjectNameSet = new Set(\n blockObjectDefs.map((blockObject) => blockObject.name),\n )\n const inlineObjectNameSet = new Set(\n inlineObjectDefs.map((inlineObject) => inlineObject.name),\n )\n\n const temporaryBlockObjectNames: Record<string, string> = {}\n const temporaryInlineObjectNames: Record<string, string> = {}\n const blockObjectNames: Record<string, string> = {}\n const inlineObjectNames: Record<string, string> = {}\n\n for (const name of blockObjectNameSet) {\n if (sanityBuiltinNames.has(name) || inlineObjectNameSet.has(name)) {\n const tmpName = `tmp-${keyGenerator()}-${name}`\n temporaryBlockObjectNames[name] = tmpName\n blockObjectNames[tmpName] = name\n }\n }\n\n for (const name of inlineObjectNameSet) {\n if (sanityBuiltinNames.has(name) || blockObjectNameSet.has(name)) {\n const tmpName = `tmp-${keyGenerator()}-${name}`\n temporaryInlineObjectNames[name] = tmpName\n inlineObjectNames[tmpName] = name\n }\n }\n\n const blockObjects = blockObjectDefs.map((blockObject) =>\n defineType({\n type: 'object',\n // Use temporary names to work around `SanitySchema.compile` adding\n // default fields to objects with certain names, and to avoid duplicate\n // type names when a type appears in both blockObjects and inlineObjects.\n name: temporaryBlockObjectNames[blockObject.name] ?? blockObject.name,\n title:\n blockObject.title === undefined\n ? // This avoids the default title which is a title case of the object name\n defaultObjectTitles[blockObject.name]\n : blockObject.title,\n fields:\n blockObject.fields?.map((field) => ({\n name: field.name,\n type: field.type,\n title: field.title ?? startCase(field.name),\n })) ?? [],\n }),\n )\n\n const inlineObjects = inlineObjectDefs.map((inlineObject) =>\n defineType({\n type: 'object',\n // Use temporary names to work around `SanitySchema.compile` adding\n // default fields to objects with certain names, and to avoid duplicate\n // type names when a type appears in both blockObjects and inlineObjects.\n name: temporaryInlineObjectNames[inlineObject.name] ?? inlineObject.name,\n\n title:\n inlineObject.title === undefined\n ? // This avoids the default title which is a title case of the object name\n defaultObjectTitles[inlineObject.name]\n : inlineObject.title,\n fields:\n inlineObject.fields?.map((field) => ({\n name: field.name,\n type: field.type,\n title: field.title ?? startCase(field.name),\n })) ?? [],\n }),\n )\n\n const portableTextSchema = defineField({\n type: 'array',\n name: 'portable-text',\n of: [\n ...blockObjects.map((blockObject) => ({type: blockObject.name})),\n {\n type: 'block',\n name: 'block',\n of: inlineObjects.map((inlineObject) => ({type: inlineObject.name})),\n marks: {\n decorators:\n definition?.decorators?.map((decorator) => ({\n title: decorator.title ?? startCase(decorator.name),\n value: decorator.name,\n })) ?? [],\n annotations:\n definition?.annotations?.map((annotation) => ({\n name: annotation.name,\n type: 'object',\n title: annotation.title,\n fields:\n annotation.fields?.map((field) => ({\n name: field.name,\n title: field.title ?? startCase(field.name),\n type: field.type,\n })) ?? [],\n })) ?? [],\n },\n lists:\n definition?.lists?.map((list) => ({\n value: list.name,\n title: list.title ?? startCase(list.name),\n })) ?? [],\n styles:\n definition?.styles?.map((style) => ({\n value: style.name,\n title: style.title ?? startCase(style.name),\n })) ?? [],\n },\n ],\n })\n\n const schema = SanitySchema.compile({\n types: [portableTextSchema, ...blockObjects, ...inlineObjects],\n }).get('portable-text')\n\n const pteSchema = createPortableTextMemberSchemaTypes(schema)\n\n return {\n ...pteSchema,\n portableText: {\n ...pteSchema.portableText,\n of: pteSchema.portableText.of.map((schemaType) => {\n if (!isObjectSchemaType(schemaType)) {\n return schemaType\n }\n\n const nameMapping = blockObjectNames[schemaType.name]\n\n schemaType.name = nameMapping ?? schemaType.name\n\n for (const field of schemaType.fields) {\n if (field.name !== 'children' || !isArraySchemaType(field.type)) {\n continue\n }\n\n for (const ofSchemaType of field.type.of) {\n const nameMapping = inlineObjectNames[ofSchemaType.name]\n\n if (!nameMapping) {\n continue\n }\n\n ofSchemaType.name = nameMapping\n }\n }\n\n return schemaType\n }),\n },\n blockObjects: pteSchema.blockObjects.map((blockObject) =>\n blockObjectNames[blockObject.name] !== undefined\n ? ({\n ...blockObject,\n name: blockObjectNames[blockObject.name],\n type: {\n ...blockObject.type,\n name: blockObjectNames[blockObject.name],\n },\n } as ObjectSchemaType)\n : blockObject,\n ),\n inlineObjects: pteSchema.inlineObjects.map((inlineObject) =>\n inlineObjectNames[inlineObject.name] !== undefined\n ? ({\n ...inlineObject,\n name: inlineObjectNames[inlineObject.name],\n } as ObjectSchemaType)\n : inlineObject,\n ),\n } satisfies PortableTextMemberSchemaTypes\n}\n"],"names":["SanitySchema","nameMapping"],"mappings":";;;AAiCO,SAAS,oCACd,kBA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B,UAAU;AAChC,iBAAO;AAGT,cAAM,cAAc,iBAAiB,WAAW,IAAI;AAEpD,mBAAW,OAAO,eAAe,WAAW;AAE5C,mBAAW,SAAS,WAAW;AAC7B,cAAI,QAAM,SAAS,cAAc,CAAC,kBAAkB,MAAM,IAAI;AAI9D,uBAAW,gBAAgB,MAAM,KAAK,IAAI;AACxC,oBAAMC,eAAc,kBAAkB,aAAa,IAAI;AAElDA,+BAIL,aAAa,OAAOA;AAAAA,YACtB;AAGF,eAAO;AAAA,MACT,CAAC;AAAA,IAAA;AAAA,IAEH,cAAc,UAAU,aAAa;AAAA,MAAI,CAAC,gBACxC,iBAAiB,YAAY,IAAI,MAAM,SAClC;AAAA,QACC,GAAG;AAAA,QACH,MAAM,iBAAiB,YAAY,IAAI;AAAA,QACvC,MAAM;AAAA,UACJ,GAAG,YAAY;AAAA,UACf,MAAM,iBAAiB,YAAY,IAAI;AAAA,QAAA;AAAA,MACzC,IAEF;AAAA,IAAA;AAAA,IAEN,eAAe,UAAU,cAAc;AAAA,MAAI,CAAC,iBAC1C,kBAAkB,aAAa,IAAI,MAAM,SACpC;AAAA,QACC,GAAG;AAAA,QACH,MAAM,kBAAkB,aAAa,IAAI;AAAA,MAAA,IAE3C;AAAA,IAAA;AAAA,EACN;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/portable-text-member-schema-types.ts","../src/portable-text-member-schema-types-to-schema.ts","../src/sanity-schema-to-portable-text-schema.ts","../src/key-generator.ts","../src/start-case.ts","../src/schema-definition-to-portable-text-member-schema-types.ts"],"sourcesContent":["import type {\n ArraySchemaType,\n BlockDecoratorDefinition,\n BlockListDefinition,\n BlockSchemaType,\n BlockStyleDefinition,\n ObjectSchemaType,\n PortableTextBlock,\n SchemaType,\n SpanSchemaType,\n} from '@sanity/types'\n\n/**\n * @public\n * Sanity-specific schema types for Portable Text.\n */\nexport type PortableTextMemberSchemaTypes = {\n annotations: (ObjectSchemaType & {i18nTitleKey?: string})[]\n block: ObjectSchemaType\n blockObjects: ObjectSchemaType[]\n decorators: BlockDecoratorDefinition[]\n inlineObjects: ObjectSchemaType[]\n portableText: ArraySchemaType<PortableTextBlock>\n span: ObjectSchemaType\n styles: BlockStyleDefinition[]\n lists: BlockListDefinition[]\n}\n\n/**\n * @public\n * Create Sanity-specific schema types for Portable Text from a Sanity array\n * schema type.\n */\nexport function createPortableTextMemberSchemaTypes(\n portableTextType: ArraySchemaType<PortableTextBlock>,\n): PortableTextMemberSchemaTypes {\n if (!portableTextType) {\n throw new Error(\"Parameter 'portabletextType' missing (required)\")\n }\n const blockType = portableTextType.of?.find(findBlockType) as\n | BlockSchemaType\n | undefined\n if (!blockType) {\n throw new Error('Block type is not defined in this schema (required)')\n }\n const childrenField = blockType.fields?.find(\n (field) => field.name === 'children',\n ) as {type: ArraySchemaType} | undefined\n if (!childrenField) {\n throw new Error('Children field for block type found in schema (required)')\n }\n const ofType = childrenField.type.of\n if (!ofType) {\n throw new Error(\n 'Valid types for block children not found in schema (required)',\n )\n }\n const spanType = ofType.find((memberType) => memberType.name === 'span') as\n | ObjectSchemaType\n | undefined\n if (!spanType) {\n throw new Error('Span type not found in schema (required)')\n }\n const inlineObjectTypes = (ofType.filter(\n (memberType) => memberType.name !== 'span',\n ) || []) as ObjectSchemaType[]\n const blockObjectTypes = (portableTextType.of?.filter(\n (field) => field.name !== blockType.name,\n ) || []) as ObjectSchemaType[]\n return {\n styles: resolveEnabledStyles(blockType),\n decorators: resolveEnabledDecorators(spanType),\n lists: resolveEnabledListItems(blockType),\n block: blockType,\n span: spanType,\n portableText: portableTextType,\n inlineObjects: inlineObjectTypes,\n blockObjects: blockObjectTypes,\n annotations: (spanType as SpanSchemaType).annotations,\n }\n}\n\nfunction resolveEnabledStyles(blockType: ObjectSchemaType) {\n const styleField = blockType.fields?.find(\n (btField) => btField.name === 'style',\n )\n if (!styleField) {\n throw new Error(\n \"A field with name 'style' is not defined in the block type (required).\",\n )\n }\n const textStyles =\n styleField.type.options?.list &&\n styleField.type.options.list?.filter(\n (style: {value: string}) => style.value,\n )\n if (!textStyles || textStyles.length === 0) {\n throw new Error(\n 'The style fields need at least one style ' +\n \"defined. I.e: {title: 'Normal', value: 'normal'}.\",\n )\n }\n return textStyles\n}\n\nfunction resolveEnabledDecorators(spanType: ObjectSchemaType) {\n return (spanType as any).decorators\n}\n\nfunction resolveEnabledListItems(blockType: ObjectSchemaType) {\n const listField = blockType.fields?.find(\n (btField) => btField.name === 'listItem',\n )\n if (!listField) {\n throw new Error(\n \"A field with name 'listItem' is not defined in the block type (required).\",\n )\n }\n const listItems =\n listField.type.options?.list &&\n listField.type.options.list.filter((list: {value: string}) => list.value)\n if (!listItems) {\n throw new Error('The list field need at least to be an empty array')\n }\n return listItems\n}\n\nfunction findBlockType(type: SchemaType): BlockSchemaType | null {\n if (type.type) {\n return findBlockType(type.type)\n }\n\n if (type.name === 'block') {\n return type as BlockSchemaType\n }\n\n return null\n}\n","import type {Schema} from '@portabletext/schema'\nimport type {PortableTextMemberSchemaTypes} from './portable-text-member-schema-types'\n\n/**\n * @public\n * Convert Sanity-specific schema types for Portable Text to a first-class\n * Portable Text schema.\n */\nexport function portableTextMemberSchemaTypesToSchema(\n schema: PortableTextMemberSchemaTypes,\n): Schema {\n return {\n annotations: schema.annotations.map((annotation) => ({\n name: annotation.name,\n fields: annotation.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: annotation.title,\n })),\n block: {\n name: schema.block.name,\n },\n blockObjects: schema.blockObjects.map((blockObject) => ({\n name: blockObject.name,\n fields: blockObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: blockObject.title,\n })),\n decorators: schema.decorators.map((decorator) => ({\n name: decorator.value,\n title: decorator.title,\n value: decorator.value,\n })),\n inlineObjects: schema.inlineObjects.map((inlineObject) => ({\n name: inlineObject.name,\n fields: inlineObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: inlineObject.title,\n })),\n span: {\n name: schema.span.name,\n },\n styles: schema.styles.map((style) => ({\n name: style.value,\n title: style.title,\n value: style.value,\n })),\n lists: schema.lists.map((list) => ({\n name: list.value,\n title: list.title,\n value: list.value,\n })),\n }\n}\n","import type {Schema} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport {builtinTypes} from '@sanity/schema/_internal'\nimport type {ArrayDefinition, ArraySchemaType} from '@sanity/types'\nimport {createPortableTextMemberSchemaTypes} from './portable-text-member-schema-types'\nimport {portableTextMemberSchemaTypesToSchema} from './portable-text-member-schema-types-to-schema'\n\n/**\n * @public\n * Compile a Sanity schema to a Portable Text `Schema`.\n *\n * A Portable Text `Schema` is compatible with a Portable Text\n * `SchemaDefinition` and can be used as configuration for the Portable Text\n * Editor.\n *\n * @example\n * ```tsx\n * const schema = sanitySchemaToPortableTextSchema(sanitySchema)\n *\n * return (\n * <EditorProvider\n * initialConfig={{\n * // ...\n * schemaDefinition: schema,\n * }}\n * >\n * // ...\n * </EditorProvider>\n * ```\n */\nexport function sanitySchemaToPortableTextSchema(\n sanitySchema: ArraySchemaType<unknown> | ArrayDefinition,\n): Schema {\n const portableTextMemberSchemaTypes = createPortableTextMemberSchemaTypes(\n sanitySchema.hasOwnProperty('jsonType')\n ? sanitySchema\n : compileType(sanitySchema),\n )\n\n return portableTextMemberSchemaTypesToSchema(portableTextMemberSchemaTypes)\n}\n\nfunction compileType(rawType: any) {\n return SanitySchema.compile({\n name: 'blockTypeSchema',\n types: [rawType, ...builtinTypes],\n }).get(rawType.name)\n}\n","export const keyGenerator = (): string => randomKey(12)\n\nconst getByteHexTable = (() => {\n let table: any[]\n return () => {\n if (table) {\n return table\n }\n\n table = []\n for (let i = 0; i < 256; ++i) {\n table[i] = (i + 0x100).toString(16).slice(1)\n }\n return table\n }\n})()\n\n// WHATWG crypto RNG - https://w3c.github.io/webcrypto/Overview.html\nfunction whatwgRNG(length = 16) {\n const rnds8 = new Uint8Array(length)\n crypto.getRandomValues(rnds8)\n return rnds8\n}\n\nfunction randomKey(length?: number): string {\n const table = getByteHexTable()\n return whatwgRNG(length)\n .reduce((str, n) => str + table[n], '')\n .slice(0, length)\n}\n","export function startCase(str: string): string {\n return (\n str\n // Insert space before uppercase letters in camelCase (e.g., 'fooBar' -> 'foo Bar')\n .replace(/([a-z])([A-Z])/g, '$1 $2')\n // Replace underscores and dashes with spaces\n .replace(/[_-]+/g, ' ')\n // Trim and split on whitespace\n .trim()\n .split(/\\s+/)\n .filter(Boolean)\n // Capitalize first letter of each word, preserve rest\n .map((word) => word.charAt(0).toUpperCase() + word.slice(1))\n .join(' ')\n )\n}\n","import type {SchemaDefinition} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport {defineField, defineType} from '@sanity/types'\nimport {keyGenerator} from './key-generator'\nimport {\n createPortableTextMemberSchemaTypes,\n type PortableTextMemberSchemaTypes,\n} from './portable-text-member-schema-types'\nimport {startCase} from './start-case'\n\nconst defaultObjectTitles: Record<string, string> = {\n image: 'Image',\n url: 'URL',\n}\n\n/**\n * Names that conflict with Sanity's built-in schema types and need temporary\n * names during `SanitySchema.compile` to avoid getting default fields added.\n */\nconst sanityBuiltinNames = new Set(['file', 'geopoint', 'image', 'slug', 'url'])\n\n/**\n * @public\n * Compile a Portable Text schema definition to Sanity-specific schema types for\n * Portable Text.\n */\nexport function compileSchemaDefinitionToPortableTextMemberSchemaTypes(\n definition?: SchemaDefinition,\n): PortableTextMemberSchemaTypes {\n const blockObjectDefs = definition?.blockObjects ?? []\n const inlineObjectDefs = definition?.inlineObjects ?? []\n\n // Collect names that appear in both blockObjects and inlineObjects, or that\n // conflict with Sanity built-in types. These need temporary names so that\n // `SanitySchema.compile` doesn't see duplicate type registrations.\n const blockObjectNameSet = new Set(\n blockObjectDefs.map((blockObject) => blockObject.name),\n )\n const inlineObjectNameSet = new Set(\n inlineObjectDefs.map((inlineObject) => inlineObject.name),\n )\n\n const temporaryBlockObjectNames: Record<string, string> = {}\n const temporaryInlineObjectNames: Record<string, string> = {}\n const blockObjectNames: Record<string, string> = {}\n const inlineObjectNames: Record<string, string> = {}\n\n for (const name of blockObjectNameSet) {\n if (sanityBuiltinNames.has(name) || inlineObjectNameSet.has(name)) {\n const tmpName = `tmp-${keyGenerator()}-${name}`\n temporaryBlockObjectNames[name] = tmpName\n blockObjectNames[tmpName] = name\n }\n }\n\n for (const name of inlineObjectNameSet) {\n if (sanityBuiltinNames.has(name) || blockObjectNameSet.has(name)) {\n const tmpName = `tmp-${keyGenerator()}-${name}`\n temporaryInlineObjectNames[name] = tmpName\n inlineObjectNames[tmpName] = name\n }\n }\n\n const blockObjects = blockObjectDefs.map((blockObject) =>\n defineType({\n type: 'object',\n // Use temporary names to work around `SanitySchema.compile` adding\n // default fields to objects with certain names, and to avoid duplicate\n // type names when a type appears in both blockObjects and inlineObjects.\n name: temporaryBlockObjectNames[blockObject.name] ?? blockObject.name,\n title:\n blockObject.title === undefined\n ? // This avoids the default title which is a title case of the object name\n defaultObjectTitles[blockObject.name]\n : blockObject.title,\n fields:\n blockObject.fields?.map((field) => ({\n name: field.name,\n type: field.type,\n title: field.title ?? startCase(field.name),\n })) ?? [],\n }),\n )\n\n const inlineObjects = inlineObjectDefs.map((inlineObject) =>\n defineType({\n type: 'object',\n // Use temporary names to work around `SanitySchema.compile` adding\n // default fields to objects with certain names, and to avoid duplicate\n // type names when a type appears in both blockObjects and inlineObjects.\n name: temporaryInlineObjectNames[inlineObject.name] ?? inlineObject.name,\n\n title:\n inlineObject.title === undefined\n ? // This avoids the default title which is a title case of the object name\n defaultObjectTitles[inlineObject.name]\n : inlineObject.title,\n fields:\n inlineObject.fields?.map((field) => ({\n name: field.name,\n type: field.type,\n title: field.title ?? startCase(field.name),\n })) ?? [],\n }),\n )\n\n const portableTextSchema = defineField({\n type: 'array',\n name: 'portable-text',\n of: [\n ...blockObjects.map((blockObject) => ({type: blockObject.name})),\n {\n type: 'block',\n name: 'block',\n of: inlineObjects.map((inlineObject) => ({type: inlineObject.name})),\n marks: {\n decorators:\n definition?.decorators?.map((decorator) => ({\n title: decorator.title ?? startCase(decorator.name),\n value: decorator.name,\n })) ?? [],\n annotations:\n definition?.annotations?.map((annotation) => ({\n name: annotation.name,\n type: 'object',\n title: annotation.title,\n fields:\n annotation.fields?.map((field) => ({\n name: field.name,\n title: field.title ?? startCase(field.name),\n type: field.type,\n })) ?? [],\n })) ?? [],\n },\n lists:\n definition?.lists?.map((list) => ({\n value: list.name,\n title: list.title ?? startCase(list.name),\n })) ?? [],\n styles:\n definition?.styles?.map((style) => ({\n value: style.name,\n title: style.title ?? startCase(style.name),\n })) ?? [],\n },\n ],\n })\n\n const schema = SanitySchema.compile({\n types: [portableTextSchema, ...blockObjects, ...inlineObjects],\n }).get('portable-text')\n\n const pteSchema = createPortableTextMemberSchemaTypes(schema)\n\n // Restore original names on blockObjects and inlineObjects.\n // These are shared references with portableText.of, so mutating them\n // also restores names in the portableText array type's nested structure.\n for (const blockObject of pteSchema.blockObjects) {\n const originalName = blockObjectNames[blockObject.name]\n if (originalName !== undefined) {\n blockObject.name = originalName\n if (blockObject.type) {\n blockObject.type = {...blockObject.type, name: originalName}\n }\n }\n }\n\n for (const inlineObject of pteSchema.inlineObjects) {\n const originalName = inlineObjectNames[inlineObject.name]\n if (originalName !== undefined) {\n inlineObject.name = originalName\n if (inlineObject.type) {\n inlineObject.type = {...inlineObject.type, name: originalName}\n }\n }\n }\n\n return pteSchema\n}\n"],"names":["SanitySchema"],"mappings":";;;AAiCO,SAAS,oCACd,kBAC+B;AAC/B,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,iDAAiD;AAEnE,QAAM,YAAY,iBAAiB,IAAI,KAAK,aAAa;AAGzD,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,qDAAqD;AAEvE,QAAM,gBAAgB,UAAU,QAAQ;AAAA,IACtC,CAAC,UAAU,MAAM,SAAS;AAAA,EAAA;AAE5B,MAAI,CA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