@portabletext/sanity-bridge 1.1.10 → 1.1.12

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var schema = require("@sanity/schema"), types = require("@sanity/types"), startCase = require("lodash.startcase"), getRandomValues = require("get-random-values-esm");
3
+ var schema = require("@sanity/schema"), _internal = require("@sanity/schema/_internal"), types = require("@sanity/types"), startCase = require("lodash.startcase"), getRandomValues = require("get-random-values-esm");
4
4
  function _interopDefaultCompat(e) {
5
5
  return e && typeof e == "object" && "default" in e ? e : { default: e };
6
6
  }
@@ -138,7 +138,7 @@ function sanitySchemaToPortableTextSchema(sanitySchema) {
138
138
  function compileType(rawType) {
139
139
  return schema.Schema.compile({
140
140
  name: "blockTypeSchema",
141
- types: [rawType]
141
+ types: [rawType, ..._internal.builtinTypes]
142
142
  }).get(rawType.name);
143
143
  }
144
144
  const keyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
@@ -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 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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AAAA,IACH,cAAc;AAAA,MACZ,GAAG,UAAU;AAAA,MACb,IAAI,UAAU,aAAa,GAAG,IAAI,CAAC,eAAe;AAChD,YAAI,CAACM,MAAAA,mBAAmB,UAAU;AAChC,iBAAO;AAGT,cAAM,cAAc,iBAAiB,WAAW,IAAI;AAEpD,mBAAW,OAAO,eAAe,WAAW;AAE5C,mBAAW,SAAS,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;;;;;"}
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 {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","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","builtinTypes","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,cAA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CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Schema } from "@sanity/schema";
2
+ import { builtinTypes } from "@sanity/schema/_internal";
2
3
  import { defineType, defineField, isObjectSchemaType, isArraySchemaType } from "@sanity/types";
3
4
  import startCase from "lodash.startcase";
4
5
  import getRandomValues from "get-random-values-esm";
@@ -135,7 +136,7 @@ function sanitySchemaToPortableTextSchema(sanitySchema) {
135
136
  function compileType(rawType) {
136
137
  return Schema.compile({
137
138
  name: "blockTypeSchema",
138
- types: [rawType]
139
+ types: [rawType, ...builtinTypes]
139
140
  }).get(rawType.name);
140
141
  }
141
142
  const keyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
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 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,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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tB;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/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","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,qBAAqB,SAAS;AAAA,IACtC,YAAY,yBAAyB,QAAQ;AAAA,IAC7C,OAAO,wBAAwB,SAAS;AAAA,IACxC,OAAO;AAAA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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.10",
3
+ "version": "1.1.12",
4
4
  "description": "Convert a Sanity Schema to a Portable Text Schema",
5
5
  "keywords": [
6
6
  "sanity",
@@ -41,16 +41,16 @@
41
41
  "@portabletext/schema": "^1.2.0"
42
42
  },
43
43
  "devDependencies": {
44
- "@sanity/pkg-utils": "^8.1.4",
45
- "@sanity/schema": "^4.9.0",
46
- "@sanity/types": "^4.9.0",
44
+ "@sanity/pkg-utils": "^8.1.14",
45
+ "@sanity/schema": "^4.10.1",
46
+ "@sanity/types": "^4.10.1",
47
47
  "@types/lodash.startcase": "^4.4.9",
48
48
  "typescript": "5.9.2",
49
49
  "vitest": "^3.2.4"
50
50
  },
51
51
  "peerDependencies": {
52
- "@sanity/schema": "^4.9.0",
53
- "@sanity/types": "^4.9.0"
52
+ "@sanity/schema": "^4.10.1",
53
+ "@sanity/types": "^4.10.1"
54
54
  },
55
55
  "engines": {
56
56
  "node": ">=20.19 <22 || >=22.12"
@@ -200,6 +200,50 @@ describe(sanitySchemaToPortableTextSchema.name, () => {
200
200
  )
201
201
  })
202
202
 
203
+ test('array definition with image', () => {
204
+ const sanitySchema: ArrayDefinition = {
205
+ type: 'array',
206
+ name: 'content',
207
+ of: [
208
+ defineField({type: 'block', name: 'block'}),
209
+ defineField({type: 'image', name: 'image'}),
210
+ ],
211
+ }
212
+
213
+ expect(sanitySchemaToPortableTextSchema(sanitySchema)).toEqual({
214
+ ...defaultSchema,
215
+ blockObjects: [
216
+ ...defaultSchema.blockObjects,
217
+ {
218
+ name: 'image',
219
+ title: 'Image',
220
+ fields: [
221
+ {
222
+ name: 'asset',
223
+ title: 'Asset',
224
+ type: 'object',
225
+ },
226
+ {
227
+ name: 'media',
228
+ title: 'Media',
229
+ type: 'object',
230
+ },
231
+ {
232
+ name: 'hotspot',
233
+ title: 'Hotspot',
234
+ type: 'object',
235
+ },
236
+ {
237
+ name: 'crop',
238
+ title: 'Crop',
239
+ type: 'object',
240
+ },
241
+ ],
242
+ },
243
+ ],
244
+ })
245
+ })
246
+
203
247
  test('compiled back and forth', () => {
204
248
  const imageType = defineType({
205
249
  name: 'custom image',
@@ -1,5 +1,6 @@
1
1
  import type {Schema} from '@portabletext/schema'
2
2
  import {Schema as SanitySchema} from '@sanity/schema'
3
+ import {builtinTypes} from '@sanity/schema/_internal'
3
4
  import type {ArrayDefinition, ArraySchemaType} from '@sanity/types'
4
5
  import {createPortableTextMemberSchemaTypes} from './portable-text-member-schema-types'
5
6
  import {portableTextMemberSchemaTypesToSchema} from './portable-text-member-schema-types-to-schema'
@@ -42,6 +43,6 @@ export function sanitySchemaToPortableTextSchema(
42
43
  function compileType(rawType: any) {
43
44
  return SanitySchema.compile({
44
45
  name: 'blockTypeSchema',
45
- types: [rawType],
46
+ types: [rawType, ...builtinTypes],
46
47
  }).get(rawType.name)
47
48
  }