@portabletext/sanity-bridge 1.1.3 → 1.1.5
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.cjs +7 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +7 -15
- package/dist/index.js.map +1 -1
- package/package.json +8 -5
- package/src/sanity-schema-to-portable-text-schema.test.ts +202 -0
- package/src/schema-definition-to-portable-text-member-schema-types.test.ts +383 -0
- package/src/schema-definition-to-portable-text-member-schema-types.ts +16 -25
package/dist/index.cjs
CHANGED
|
@@ -255,22 +255,14 @@ function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
|
255
255
|
if (!types.isObjectSchemaType(schemaType))
|
|
256
256
|
return schemaType;
|
|
257
257
|
const nameMapping = blockObjectNames[schemaType.name];
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
name
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
of: field.type.of.map((ofSchemaType) => {
|
|
265
|
-
const nameMapping2 = inlineObjectNames[ofSchemaType.name];
|
|
266
|
-
return nameMapping2 ? {
|
|
267
|
-
...ofSchemaType,
|
|
268
|
-
name: nameMapping2
|
|
269
|
-
} : ofSchemaType;
|
|
270
|
-
})
|
|
258
|
+
schemaType.name = nameMapping ?? schemaType.name;
|
|
259
|
+
for (const field of schemaType.fields)
|
|
260
|
+
if (!(field.name !== "children" || !types.isArraySchemaType(field.type)))
|
|
261
|
+
for (const ofSchemaType of field.type.of) {
|
|
262
|
+
const nameMapping2 = inlineObjectNames[ofSchemaType.name];
|
|
263
|
+
nameMapping2 && (ofSchemaType.name = nameMapping2);
|
|
271
264
|
}
|
|
272
|
-
|
|
273
|
-
};
|
|
265
|
+
return schemaType;
|
|
274
266
|
})
|
|
275
267
|
},
|
|
276
268
|
blockObjects: pteSchema.blockObjects.map(
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","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/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 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],\n }).get(rawType.name)\n}\n","import getRandomValues from 'get-random-values-esm'\n\nexport 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 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","import type {SchemaDefinition} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport {\n defineField,\n defineType,\n isArraySchemaType,\n isObjectSchemaType,\n type ObjectField,\n type ObjectSchemaType,\n} from '@sanity/types'\nimport startCase from 'lodash.startcase'\nimport {keyGenerator} from './key-generator'\nimport {\n createPortableTextMemberSchemaTypes,\n type PortableTextMemberSchemaTypes,\n} from './portable-text-member-schema-types'\n\nconst temporaryImageBlockObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlBlockObjectName = `tmp-${keyGenerator()}-url`\nconst temporaryImageInlineObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlInlineObjectName = `tmp-${keyGenerator()}-url`\n\nconst temporaryBlockObjectNames: Record<string, string> = {\n image: temporaryImageBlockObjectName,\n url: temporaryUrlBlockObjectName,\n}\n\nconst temporaryInlineObjectNames: Record<string, string> = {\n image: temporaryImageInlineObjectName,\n url: temporaryUrlInlineObjectName,\n}\n\nconst blockObjectNames: Record<string, string> = {\n [temporaryImageBlockObjectName]: 'image',\n [temporaryUrlBlockObjectName]: 'url',\n}\n\nconst inlineObjectNames: Record<string, string> = {\n [temporaryImageInlineObjectName]: 'image',\n [temporaryUrlInlineObjectName]: 'url',\n}\n\nconst defaultObjectTitles: Record<string, string> = {\n image: 'Image',\n url: 'URL',\n}\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 blockObjects =\n definition?.blockObjects?.map((blockObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\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 =\n definition?.inlineObjects?.map((inlineObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\n name:\n 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 return {\n ...schemaType,\n name: nameMapping ?? schemaType.name,\n fields: schemaType.fields.map((field) => {\n if (field.name !== 'children' || !isArraySchemaType(field.type)) {\n return field\n }\n\n return {\n ...field,\n type: {\n of: field.type.of.map((ofSchemaType) => {\n const nameMapping = inlineObjectNames[ofSchemaType.name]\n\n if (!nameMapping) {\n return ofSchemaType\n }\n\n return {\n ...ofSchemaType,\n name: nameMapping,\n }\n }),\n },\n } as ObjectField\n }),\n }\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":["schema","SanitySchema","getRandomValues","defineType","startCase","defineField","isObjectSchemaType","isArraySchemaType","nameMapping"],"mappings":";;;;;;;AAiCO,SAAS,oCACd,kBAC+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;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.cjs","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/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 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],\n }).get(rawType.name)\n}\n","import getRandomValues from 'get-random-values-esm'\n\nexport 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 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","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 startCase from 'lodash.startcase'\nimport {keyGenerator} from './key-generator'\nimport {\n createPortableTextMemberSchemaTypes,\n type PortableTextMemberSchemaTypes,\n} from './portable-text-member-schema-types'\n\nconst temporaryImageBlockObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlBlockObjectName = `tmp-${keyGenerator()}-url`\nconst temporaryImageInlineObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlInlineObjectName = `tmp-${keyGenerator()}-url`\n\nconst temporaryBlockObjectNames: Record<string, string> = {\n image: temporaryImageBlockObjectName,\n url: temporaryUrlBlockObjectName,\n}\n\nconst temporaryInlineObjectNames: Record<string, string> = {\n image: temporaryImageInlineObjectName,\n url: temporaryUrlInlineObjectName,\n}\n\nconst blockObjectNames: Record<string, string> = {\n [temporaryImageBlockObjectName]: 'image',\n [temporaryUrlBlockObjectName]: 'url',\n}\n\nconst inlineObjectNames: Record<string, string> = {\n [temporaryImageInlineObjectName]: 'image',\n [temporaryUrlInlineObjectName]: 'url',\n}\n\nconst defaultObjectTitles: Record<string, string> = {\n image: 'Image',\n url: 'URL',\n}\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 blockObjects =\n definition?.blockObjects?.map((blockObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\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 =\n definition?.inlineObjects?.map((inlineObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\n name:\n 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":["schema","SanitySchema","getRandomValues","defineType","startCase","defineField","isObjectSchemaType","isArraySchemaType","nameMapping"],"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,CAAC;AACH,UAAM,IAAI,MAAM,0DAA0D;AAE5E,QAAM,SAAS,cAAc,KAAK;AAClC,MAAI,CAAC;AACH,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAGJ,QAAM,WAAW,OAAO,KAAK,CAAC,eAAe,WAAW,SAAS,MAAM;AAGvE,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,0CAA0C;AAE5D,QAAM,oBAAqB,OAAO;AAAA,IAChC,CAAC,eAAe,WAAW,SAAS;AAAA,EAAA,KACjC,IACC,mBAAoB,iBAAiB,IAAI;AAAA,IAC7C,CAAC,UAAU,MAAM,SAAS,UAAU;AAAA,EAAA,KACjC,CAAA;AACL,SAAO;AAAA,IACL,QAAQ,qBAAqB,SAAS;AAAA,IACtC,YAAY,yBAAyB,QAAQ;AAAA,IAC7C,OAAO,wBAAwB,SAAS;AAAA,IACxC,OAAO;AAAA,IACP,MAAM;AAAA,I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S,WAAW;AAC7B,cAAI,QAAM,SAAS,cAAc,CAACC,wBAAkB,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;;;;;"}
|
package/dist/index.js
CHANGED
|
@@ -252,22 +252,14 @@ function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
|
252
252
|
if (!isObjectSchemaType(schemaType))
|
|
253
253
|
return schemaType;
|
|
254
254
|
const nameMapping = blockObjectNames[schemaType.name];
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
name
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
of: field.type.of.map((ofSchemaType) => {
|
|
262
|
-
const nameMapping2 = inlineObjectNames[ofSchemaType.name];
|
|
263
|
-
return nameMapping2 ? {
|
|
264
|
-
...ofSchemaType,
|
|
265
|
-
name: nameMapping2
|
|
266
|
-
} : ofSchemaType;
|
|
267
|
-
})
|
|
255
|
+
schemaType.name = nameMapping ?? schemaType.name;
|
|
256
|
+
for (const field of schemaType.fields)
|
|
257
|
+
if (!(field.name !== "children" || !isArraySchemaType(field.type)))
|
|
258
|
+
for (const ofSchemaType of field.type.of) {
|
|
259
|
+
const nameMapping2 = inlineObjectNames[ofSchemaType.name];
|
|
260
|
+
nameMapping2 && (ofSchemaType.name = nameMapping2);
|
|
268
261
|
}
|
|
269
|
-
|
|
270
|
-
};
|
|
262
|
+
return schemaType;
|
|
271
263
|
})
|
|
272
264
|
},
|
|
273
265
|
blockObjects: pteSchema.blockObjects.map(
|
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/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 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],\n }).get(rawType.name)\n}\n","import getRandomValues from 'get-random-values-esm'\n\nexport 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 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","import type {SchemaDefinition} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport {\n defineField,\n defineType,\n isArraySchemaType,\n isObjectSchemaType,\n type ObjectField,\n type ObjectSchemaType,\n} from '@sanity/types'\nimport startCase from 'lodash.startcase'\nimport {keyGenerator} from './key-generator'\nimport {\n createPortableTextMemberSchemaTypes,\n type PortableTextMemberSchemaTypes,\n} from './portable-text-member-schema-types'\n\nconst temporaryImageBlockObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlBlockObjectName = `tmp-${keyGenerator()}-url`\nconst temporaryImageInlineObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlInlineObjectName = `tmp-${keyGenerator()}-url`\n\nconst temporaryBlockObjectNames: Record<string, string> = {\n image: temporaryImageBlockObjectName,\n url: temporaryUrlBlockObjectName,\n}\n\nconst temporaryInlineObjectNames: Record<string, string> = {\n image: temporaryImageInlineObjectName,\n url: temporaryUrlInlineObjectName,\n}\n\nconst blockObjectNames: Record<string, string> = {\n [temporaryImageBlockObjectName]: 'image',\n [temporaryUrlBlockObjectName]: 'url',\n}\n\nconst inlineObjectNames: Record<string, string> = {\n [temporaryImageInlineObjectName]: 'image',\n [temporaryUrlInlineObjectName]: 'url',\n}\n\nconst defaultObjectTitles: Record<string, string> = {\n image: 'Image',\n url: 'URL',\n}\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 blockObjects =\n definition?.blockObjects?.map((blockObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\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 =\n definition?.inlineObjects?.map((inlineObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\n name:\n 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 return {\n ...schemaType,\n name: nameMapping ?? schemaType.name,\n fields: schemaType.fields.map((field) => {\n if (field.name !== 'children' || !isArraySchemaType(field.type)) {\n return field\n }\n\n return {\n ...field,\n type: {\n of: field.type.of.map((ofSchemaType) => {\n const nameMapping = inlineObjectNames[ofSchemaType.name]\n\n if (!nameMapping) {\n return ofSchemaType\n }\n\n return {\n ...ofSchemaType,\n name: nameMapping,\n }\n }),\n },\n } as ObjectField\n }),\n }\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,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,CAAC;AACH,UAAM,IAAI,MAAM,0DAA0D;AAE5E,QAAM,SAAS,cAAc,KAAK;AAClC,MAAI,CAAC;AACH,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAGJ,QAAM,WAAW,OAAO,KAAK,CAAC,eAAe,WAAW,SAAS,MAAM;AAGvE,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,0CAA0C;AAE5D,QAAM,oBAAqB,OAAO;AAAA,IAChC,CAAC,eAAe,WAAW,SAAS;AAAA,EAAA,KACjC,IACC,mBAAoB,iBAAiB,IAAI;AAAA,IAC7C,CAAC,UAAU,MAAM,SAAS,UAAU;AAAA,EAAA,KACjC,CAAA;AACL,SAAO;AAAA,IACL,QAAQ,qBAAqB,SAAS;AAAA,IACtC,YAAY,yBAAyB,QAAQ;AAAA,IAC7C,OAAO,wBAAwB,SAAS;AAAA,IACxC,OAAO;AAAA,IACP,MAAM;AAAA,IACN,cAAc;AAAA,IACd,eAAe;AAAA,IACf,cAAc;AAAA,IACd,aAAc,SAA4B;AAAA,EAAA;AAE9C;AAEA,SAAS,qBAAqB,WAA6B;AACzD,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aAAa,IAAI;AAEvD,uBAAKA,eAIE;AAAA,kBACL,GAAG;AAAA,kBACH,MAAMA;AAAAA,gBAAA,IALC;AAAA,cAOX,CAAC;AAAA,YAAA;AAAA,UACH,CAEH;AAAA,QAAA;AAAA,MAEL,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/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 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],\n }).get(rawType.name)\n}\n","import getRandomValues from 'get-random-values-esm'\n\nexport 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 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","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 startCase from 'lodash.startcase'\nimport {keyGenerator} from './key-generator'\nimport {\n createPortableTextMemberSchemaTypes,\n type PortableTextMemberSchemaTypes,\n} from './portable-text-member-schema-types'\n\nconst temporaryImageBlockObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlBlockObjectName = `tmp-${keyGenerator()}-url`\nconst temporaryImageInlineObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlInlineObjectName = `tmp-${keyGenerator()}-url`\n\nconst temporaryBlockObjectNames: Record<string, string> = {\n image: temporaryImageBlockObjectName,\n url: temporaryUrlBlockObjectName,\n}\n\nconst temporaryInlineObjectNames: Record<string, string> = {\n image: temporaryImageInlineObjectName,\n url: temporaryUrlInlineObjectName,\n}\n\nconst blockObjectNames: Record<string, string> = {\n [temporaryImageBlockObjectName]: 'image',\n [temporaryUrlBlockObjectName]: 'url',\n}\n\nconst inlineObjectNames: Record<string, string> = {\n [temporaryImageInlineObjectName]: 'image',\n [temporaryUrlInlineObjectName]: 'url',\n}\n\nconst defaultObjectTitles: Record<string, string> = {\n image: 'Image',\n url: 'URL',\n}\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 blockObjects =\n definition?.blockObjects?.map((blockObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\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 =\n definition?.inlineObjects?.map((inlineObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\n name:\n 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,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,CAAC;AACH,UAAM,IAAI,MAAM,0DAA0D;AAE5E,QAAM,SAAS,cAAc,KAAK;AAClC,MAAI,CAAC;AACH,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAGJ,QAAM,WAAW,OAAO,KAAK,CAAC,eAAe,WAAW,SAAS,MAAM;AAGvE,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,0CAA0C;AAE5D,QAAM,oBAAqB,OAAO;AAAA,IAChC,CAAC,eAAe,WAAW,SAAS;AAAA,EAAA,KACjC,IACC,mBAAoB,iBAAiB,IAAI;AAAA,IAC7C,CAAC,UAAU,MAAM,SAAS,UAAU;AAAA,EAAA,KACjC,CAAA;AACL,SAAO;AAAA,IACL,QAAQ,qBAAq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,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;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portabletext/sanity-bridge",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "Convert a Sanity Schema to a Portable Text Schema",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -38,14 +38,15 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"get-random-values-esm": "^1.0.2",
|
|
40
40
|
"lodash.startcase": "^4.4.0",
|
|
41
|
-
"@portabletext/schema": "^1.0
|
|
41
|
+
"@portabletext/schema": "^1.1.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@sanity/pkg-utils": "^7.11.
|
|
44
|
+
"@sanity/pkg-utils": "^7.11.9",
|
|
45
45
|
"@sanity/schema": "^4.5.0",
|
|
46
46
|
"@sanity/types": "^4.5.0",
|
|
47
47
|
"@types/lodash.startcase": "^4.4.9",
|
|
48
|
-
"typescript": "^5.9.2"
|
|
48
|
+
"typescript": "^5.9.2",
|
|
49
|
+
"vitest": "^3.2.4"
|
|
49
50
|
},
|
|
50
51
|
"peerDependencies": {
|
|
51
52
|
"@sanity/schema": "^4.5.0",
|
|
@@ -64,6 +65,8 @@
|
|
|
64
65
|
"check:types:watch": "tsc --watch",
|
|
65
66
|
"clean": "del .turbo && del lib && del node_modules",
|
|
66
67
|
"dev": "pkg-utils watch",
|
|
67
|
-
"lint:fix": "biome lint --write ."
|
|
68
|
+
"lint:fix": "biome lint --write .",
|
|
69
|
+
"test:unit": "vitest run",
|
|
70
|
+
"test:unit:watch": "vitest watch"
|
|
68
71
|
}
|
|
69
72
|
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import type {Schema} from '@portabletext/schema'
|
|
2
|
+
import {Schema as SanitySchema} from '@sanity/schema'
|
|
3
|
+
import {
|
|
4
|
+
defineArrayMember,
|
|
5
|
+
defineField,
|
|
6
|
+
defineType,
|
|
7
|
+
type ArrayDefinition,
|
|
8
|
+
} from '@sanity/types'
|
|
9
|
+
import {describe, expect, test} from 'vitest'
|
|
10
|
+
import {createPortableTextMemberSchemaTypes} from './portable-text-member-schema-types'
|
|
11
|
+
import {portableTextMemberSchemaTypesToSchema} from './portable-text-member-schema-types-to-schema'
|
|
12
|
+
import {sanitySchemaToPortableTextSchema} from './sanity-schema-to-portable-text-schema'
|
|
13
|
+
import {compileSchemaDefinitionToPortableTextMemberSchemaTypes} from './schema-definition-to-portable-text-member-schema-types'
|
|
14
|
+
|
|
15
|
+
describe(sanitySchemaToPortableTextSchema.name, () => {
|
|
16
|
+
const defaultSchema: Schema = {
|
|
17
|
+
block: {
|
|
18
|
+
name: 'block',
|
|
19
|
+
},
|
|
20
|
+
span: {
|
|
21
|
+
name: 'span',
|
|
22
|
+
},
|
|
23
|
+
styles: [
|
|
24
|
+
{
|
|
25
|
+
name: 'normal',
|
|
26
|
+
value: 'normal',
|
|
27
|
+
title: 'Normal',
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
name: 'h1',
|
|
31
|
+
value: 'h1',
|
|
32
|
+
title: 'Heading 1',
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: 'h2',
|
|
36
|
+
value: 'h2',
|
|
37
|
+
title: 'Heading 2',
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: 'h3',
|
|
41
|
+
value: 'h3',
|
|
42
|
+
title: 'Heading 3',
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: 'h4',
|
|
46
|
+
value: 'h4',
|
|
47
|
+
title: 'Heading 4',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: 'h5',
|
|
51
|
+
value: 'h5',
|
|
52
|
+
title: 'Heading 5',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'h6',
|
|
56
|
+
value: 'h6',
|
|
57
|
+
title: 'Heading 6',
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
name: 'blockquote',
|
|
61
|
+
value: 'blockquote',
|
|
62
|
+
title: 'Quote',
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
lists: [
|
|
66
|
+
{
|
|
67
|
+
name: 'bullet',
|
|
68
|
+
value: 'bullet',
|
|
69
|
+
title: 'Bulleted list',
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: 'number',
|
|
73
|
+
value: 'number',
|
|
74
|
+
title: 'Numbered list',
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
decorators: [
|
|
78
|
+
{
|
|
79
|
+
name: 'strong',
|
|
80
|
+
value: 'strong',
|
|
81
|
+
title: 'Strong',
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: 'em',
|
|
85
|
+
value: 'em',
|
|
86
|
+
title: 'Italic',
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'code',
|
|
90
|
+
value: 'code',
|
|
91
|
+
title: 'Code',
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
name: 'underline',
|
|
95
|
+
value: 'underline',
|
|
96
|
+
title: 'Underline',
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
name: 'strike-through',
|
|
100
|
+
value: 'strike-through',
|
|
101
|
+
title: 'Strike',
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
annotations: [
|
|
105
|
+
{
|
|
106
|
+
name: 'link',
|
|
107
|
+
title: 'Link',
|
|
108
|
+
fields: [
|
|
109
|
+
{
|
|
110
|
+
name: 'href',
|
|
111
|
+
type: 'string',
|
|
112
|
+
title: 'Link',
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
blockObjects: [],
|
|
118
|
+
inlineObjects: [],
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
test('simple compiled schema', () => {
|
|
122
|
+
const sanitySchema = SanitySchema.compile({
|
|
123
|
+
name: 'test',
|
|
124
|
+
types: [
|
|
125
|
+
defineArrayMember({
|
|
126
|
+
type: 'array',
|
|
127
|
+
name: 'content',
|
|
128
|
+
of: [defineField({type: 'block', name: 'block'})],
|
|
129
|
+
}),
|
|
130
|
+
],
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
expect(
|
|
134
|
+
sanitySchemaToPortableTextSchema(sanitySchema.get('content')),
|
|
135
|
+
).toEqual(defaultSchema)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('simple array definition', () => {
|
|
139
|
+
const sanitySchema: ArrayDefinition = {
|
|
140
|
+
type: 'array',
|
|
141
|
+
name: 'content',
|
|
142
|
+
of: [defineField({type: 'block', name: 'block'})],
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
expect(sanitySchemaToPortableTextSchema(sanitySchema)).toEqual(
|
|
146
|
+
defaultSchema,
|
|
147
|
+
)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('compiled back and forth', () => {
|
|
151
|
+
const imageType = defineType({
|
|
152
|
+
name: 'custom image',
|
|
153
|
+
type: 'object',
|
|
154
|
+
fields: [
|
|
155
|
+
defineField({
|
|
156
|
+
name: 'url',
|
|
157
|
+
type: 'string',
|
|
158
|
+
}),
|
|
159
|
+
],
|
|
160
|
+
})
|
|
161
|
+
const stockTickerType = defineType({
|
|
162
|
+
name: 'stock ticker',
|
|
163
|
+
type: 'object',
|
|
164
|
+
fields: [defineField({name: 'symbol', type: 'string'})],
|
|
165
|
+
})
|
|
166
|
+
const portableTextType = defineType({
|
|
167
|
+
type: 'array',
|
|
168
|
+
name: 'body',
|
|
169
|
+
of: [
|
|
170
|
+
{
|
|
171
|
+
type: 'block',
|
|
172
|
+
name: 'block',
|
|
173
|
+
of: [{type: 'stock ticker'}],
|
|
174
|
+
},
|
|
175
|
+
{type: 'custom image'},
|
|
176
|
+
],
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
const sanitySchema = SanitySchema.compile({
|
|
180
|
+
types: [portableTextType, imageType, stockTickerType],
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
const portableTextMemberSchemaTypesFromSanitySchema =
|
|
184
|
+
createPortableTextMemberSchemaTypes(sanitySchema.get('body'))
|
|
185
|
+
|
|
186
|
+
const portableTextSchema = sanitySchemaToPortableTextSchema(
|
|
187
|
+
sanitySchema.get('body'),
|
|
188
|
+
)
|
|
189
|
+
const portableTextMemberSchemaTypesFromPortableTextSchema =
|
|
190
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(portableTextSchema)
|
|
191
|
+
|
|
192
|
+
expect(
|
|
193
|
+
portableTextMemberSchemaTypesToSchema(
|
|
194
|
+
portableTextMemberSchemaTypesFromPortableTextSchema,
|
|
195
|
+
),
|
|
196
|
+
).toEqual(
|
|
197
|
+
portableTextMemberSchemaTypesToSchema(
|
|
198
|
+
portableTextMemberSchemaTypesFromSanitySchema,
|
|
199
|
+
),
|
|
200
|
+
)
|
|
201
|
+
})
|
|
202
|
+
})
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import type {Schema} from '@portabletext/schema'
|
|
2
|
+
import {describe, expect, test} from 'vitest'
|
|
3
|
+
import {portableTextMemberSchemaTypesToSchema} from './portable-text-member-schema-types-to-schema'
|
|
4
|
+
import {compileSchemaDefinitionToPortableTextMemberSchemaTypes} from './schema-definition-to-portable-text-member-schema-types'
|
|
5
|
+
|
|
6
|
+
describe(compileSchemaDefinitionToPortableTextMemberSchemaTypes.name, () => {
|
|
7
|
+
test("image object doesn't get Sanity-specific fields", () => {
|
|
8
|
+
expect(
|
|
9
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes({
|
|
10
|
+
blockObjects: [{name: 'image'}],
|
|
11
|
+
}).blockObjects,
|
|
12
|
+
).toMatchObject([
|
|
13
|
+
{
|
|
14
|
+
name: 'image',
|
|
15
|
+
fields: [],
|
|
16
|
+
},
|
|
17
|
+
])
|
|
18
|
+
|
|
19
|
+
expect(
|
|
20
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes({
|
|
21
|
+
blockObjects: [
|
|
22
|
+
{name: 'image', fields: [{name: 'src', type: 'string'}]},
|
|
23
|
+
],
|
|
24
|
+
}).blockObjects,
|
|
25
|
+
).toMatchObject([
|
|
26
|
+
{
|
|
27
|
+
name: 'image',
|
|
28
|
+
fields: [
|
|
29
|
+
{
|
|
30
|
+
name: 'src',
|
|
31
|
+
type: {
|
|
32
|
+
jsonType: 'string',
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
])
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test('back and forth test', () => {
|
|
41
|
+
const schema: Schema = {
|
|
42
|
+
annotations: [
|
|
43
|
+
{
|
|
44
|
+
name: 'link',
|
|
45
|
+
fields: [
|
|
46
|
+
{
|
|
47
|
+
name: 'href',
|
|
48
|
+
type: 'string',
|
|
49
|
+
title: 'URL',
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
title: 'Link',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'canvasComment',
|
|
56
|
+
fields: [
|
|
57
|
+
{
|
|
58
|
+
name: 'commentId',
|
|
59
|
+
type: 'string',
|
|
60
|
+
title: 'Comment ID',
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
title: 'Comment',
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name: 'canvasMapping',
|
|
67
|
+
fields: [
|
|
68
|
+
{
|
|
69
|
+
name: 'documentPath',
|
|
70
|
+
type: 'string',
|
|
71
|
+
title: 'Document path',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: 'typePath',
|
|
75
|
+
type: 'string',
|
|
76
|
+
title: 'Type path',
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: 'locked',
|
|
80
|
+
type: 'boolean',
|
|
81
|
+
title: 'Locked',
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
title: 'Mapping',
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
block: {
|
|
88
|
+
name: 'block',
|
|
89
|
+
},
|
|
90
|
+
blockObjects: [
|
|
91
|
+
{
|
|
92
|
+
name: 'canvasDivider',
|
|
93
|
+
fields: [
|
|
94
|
+
{
|
|
95
|
+
name: 'orientation',
|
|
96
|
+
type: 'string',
|
|
97
|
+
title: 'Orientation',
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
title: 'Divider',
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: 'canvasFile',
|
|
104
|
+
fields: [
|
|
105
|
+
{
|
|
106
|
+
name: 'asset',
|
|
107
|
+
type: 'object',
|
|
108
|
+
title: 'Asset',
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: 'media',
|
|
112
|
+
type: 'object',
|
|
113
|
+
title: 'Media',
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: 'title',
|
|
117
|
+
type: 'string',
|
|
118
|
+
title: 'Title',
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
name: 'filename',
|
|
122
|
+
type: 'string',
|
|
123
|
+
title: 'Filename',
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
name: 'type',
|
|
127
|
+
type: 'string',
|
|
128
|
+
title: 'Type',
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
name: 'summary',
|
|
132
|
+
type: 'string',
|
|
133
|
+
title: 'Summary',
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
name: 'large',
|
|
137
|
+
type: 'boolean',
|
|
138
|
+
title: 'Large',
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
title: 'File',
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
name: 'canvasImage',
|
|
145
|
+
fields: [
|
|
146
|
+
{
|
|
147
|
+
name: 'asset',
|
|
148
|
+
type: 'object',
|
|
149
|
+
title: 'Asset',
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
name: 'media',
|
|
153
|
+
type: 'object',
|
|
154
|
+
title: 'Media',
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
name: 'hotspot',
|
|
158
|
+
type: 'object',
|
|
159
|
+
title: 'Hotspot',
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
name: 'crop',
|
|
163
|
+
type: 'object',
|
|
164
|
+
title: 'Crop',
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
name: 'title',
|
|
168
|
+
type: 'string',
|
|
169
|
+
title: 'Title',
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
name: 'alt',
|
|
173
|
+
type: 'string',
|
|
174
|
+
title: 'Alt',
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
name: 'imageDimensions',
|
|
178
|
+
type: 'object',
|
|
179
|
+
title: 'Image Dimensions',
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: 'sourceImageId',
|
|
183
|
+
type: 'string',
|
|
184
|
+
title: 'Source Image Id',
|
|
185
|
+
},
|
|
186
|
+
],
|
|
187
|
+
title: 'Image',
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
name: 'canvasUrlBlock',
|
|
191
|
+
fields: [
|
|
192
|
+
{
|
|
193
|
+
name: 'url',
|
|
194
|
+
type: 'string',
|
|
195
|
+
title: 'Url',
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
name: 'title',
|
|
199
|
+
type: 'string',
|
|
200
|
+
title: 'Title',
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
name: 'favicon',
|
|
204
|
+
type: 'string',
|
|
205
|
+
title: 'Favicon',
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
name: 'description',
|
|
209
|
+
type: 'string',
|
|
210
|
+
title: 'Description',
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
name: 'content',
|
|
214
|
+
type: 'array',
|
|
215
|
+
title: 'Content',
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
name: 'error',
|
|
219
|
+
type: 'string',
|
|
220
|
+
title: 'Error',
|
|
221
|
+
},
|
|
222
|
+
],
|
|
223
|
+
title: 'Url block',
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
name: 'canvasAiTask',
|
|
227
|
+
fields: [
|
|
228
|
+
{
|
|
229
|
+
name: 'instruction',
|
|
230
|
+
type: 'string',
|
|
231
|
+
title: 'Instruction',
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
name: 'input',
|
|
235
|
+
type: 'array',
|
|
236
|
+
title: 'Input',
|
|
237
|
+
},
|
|
238
|
+
],
|
|
239
|
+
title: 'AI task',
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
name: 'canvasCode',
|
|
243
|
+
fields: [
|
|
244
|
+
{
|
|
245
|
+
name: 'language',
|
|
246
|
+
type: 'string',
|
|
247
|
+
title: 'Language',
|
|
248
|
+
},
|
|
249
|
+
{
|
|
250
|
+
name: 'code',
|
|
251
|
+
type: 'array',
|
|
252
|
+
title: 'Code',
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
name: 'suggestions',
|
|
256
|
+
type: 'object',
|
|
257
|
+
title: 'Suggestions',
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
title: 'Code',
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
name: 'canvasGlobalRef',
|
|
264
|
+
fields: [
|
|
265
|
+
{
|
|
266
|
+
name: 'target',
|
|
267
|
+
type: 'object',
|
|
268
|
+
title: 'Target',
|
|
269
|
+
},
|
|
270
|
+
],
|
|
271
|
+
title: 'Reference for mapping',
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
name: 'canvasPlaceholder',
|
|
275
|
+
fields: [
|
|
276
|
+
{
|
|
277
|
+
name: 'title',
|
|
278
|
+
type: 'string',
|
|
279
|
+
title: 'Title',
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
name: 'data',
|
|
283
|
+
type: 'string',
|
|
284
|
+
title: 'Data',
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
name: 'map',
|
|
288
|
+
type: 'object',
|
|
289
|
+
title: 'Map',
|
|
290
|
+
},
|
|
291
|
+
],
|
|
292
|
+
title: 'Placeholder block',
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
decorators: [
|
|
296
|
+
{
|
|
297
|
+
name: 'strong',
|
|
298
|
+
title: 'Bold',
|
|
299
|
+
value: 'strong',
|
|
300
|
+
},
|
|
301
|
+
{
|
|
302
|
+
name: 'em',
|
|
303
|
+
title: 'Italic',
|
|
304
|
+
value: 'em',
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
name: 'strike-through',
|
|
308
|
+
title: 'Strike',
|
|
309
|
+
value: 'strike-through',
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
name: 'code',
|
|
313
|
+
title: 'Code',
|
|
314
|
+
value: 'code',
|
|
315
|
+
},
|
|
316
|
+
],
|
|
317
|
+
inlineObjects: [],
|
|
318
|
+
span: {
|
|
319
|
+
name: 'span',
|
|
320
|
+
},
|
|
321
|
+
styles: [
|
|
322
|
+
{
|
|
323
|
+
name: 'normal',
|
|
324
|
+
title: 'Text',
|
|
325
|
+
value: 'normal',
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
name: 'h1',
|
|
329
|
+
title: 'Heading 1',
|
|
330
|
+
value: 'h1',
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
name: 'h2',
|
|
334
|
+
title: 'Heading 2',
|
|
335
|
+
value: 'h2',
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
name: 'h3',
|
|
339
|
+
title: 'Heading 3',
|
|
340
|
+
value: 'h3',
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
name: 'h4',
|
|
344
|
+
title: 'Heading 4',
|
|
345
|
+
value: 'h4',
|
|
346
|
+
},
|
|
347
|
+
{
|
|
348
|
+
name: 'h5',
|
|
349
|
+
title: 'Heading 5',
|
|
350
|
+
value: 'h5',
|
|
351
|
+
},
|
|
352
|
+
{
|
|
353
|
+
name: 'h6',
|
|
354
|
+
title: 'Heading 6',
|
|
355
|
+
value: 'h6',
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
name: 'blockquote',
|
|
359
|
+
title: 'Blockquote',
|
|
360
|
+
value: 'blockquote',
|
|
361
|
+
},
|
|
362
|
+
],
|
|
363
|
+
lists: [
|
|
364
|
+
{
|
|
365
|
+
name: 'number',
|
|
366
|
+
title: 'Numbered list',
|
|
367
|
+
value: 'number',
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
name: 'bullet',
|
|
371
|
+
title: 'Bulleted list',
|
|
372
|
+
value: 'bullet',
|
|
373
|
+
},
|
|
374
|
+
],
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
expect(
|
|
378
|
+
portableTextMemberSchemaTypesToSchema(
|
|
379
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(schema),
|
|
380
|
+
),
|
|
381
|
+
).toEqual(schema)
|
|
382
|
+
})
|
|
383
|
+
})
|
|
@@ -5,7 +5,6 @@ import {
|
|
|
5
5
|
defineType,
|
|
6
6
|
isArraySchemaType,
|
|
7
7
|
isObjectSchemaType,
|
|
8
|
-
type ObjectField,
|
|
9
8
|
type ObjectSchemaType,
|
|
10
9
|
} from '@sanity/types'
|
|
11
10
|
import startCase from 'lodash.startcase'
|
|
@@ -156,33 +155,25 @@ export function compileSchemaDefinitionToPortableTextMemberSchemaTypes(
|
|
|
156
155
|
|
|
157
156
|
const nameMapping = blockObjectNames[schemaType.name]
|
|
158
157
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
158
|
+
schemaType.name = nameMapping ?? schemaType.name
|
|
159
|
+
|
|
160
|
+
for (const field of schemaType.fields) {
|
|
161
|
+
if (field.name !== 'children' || !isArraySchemaType(field.type)) {
|
|
162
|
+
continue
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
for (const ofSchemaType of field.type.of) {
|
|
166
|
+
const nameMapping = inlineObjectNames[ofSchemaType.name]
|
|
167
|
+
|
|
168
|
+
if (!nameMapping) {
|
|
169
|
+
continue
|
|
165
170
|
}
|
|
166
171
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
type: {
|
|
170
|
-
of: field.type.of.map((ofSchemaType) => {
|
|
171
|
-
const nameMapping = inlineObjectNames[ofSchemaType.name]
|
|
172
|
-
|
|
173
|
-
if (!nameMapping) {
|
|
174
|
-
return ofSchemaType
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return {
|
|
178
|
-
...ofSchemaType,
|
|
179
|
-
name: nameMapping,
|
|
180
|
-
}
|
|
181
|
-
}),
|
|
182
|
-
},
|
|
183
|
-
} as ObjectField
|
|
184
|
-
}),
|
|
172
|
+
ofSchemaType.name = nameMapping
|
|
173
|
+
}
|
|
185
174
|
}
|
|
175
|
+
|
|
176
|
+
return schemaType
|
|
186
177
|
}),
|
|
187
178
|
},
|
|
188
179
|
blockObjects: pteSchema.blockObjects.map((blockObject) =>
|