@portabletext/sanity-bridge 2.0.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ArrayDefinition, ArraySchemaType, BlockDecoratorDefinition, BlockListDefinition, BlockStyleDefinition, ObjectSchemaType, PortableTextBlock } from "@sanity/types";
2
- import { Schema, SchemaDefinition } from "@portabletext/schema";
2
+ import { Schema } from "@portabletext/schema";
3
3
  /**
4
4
  * @public
5
5
  * Sanity-specific schema types for Portable Text.
@@ -23,12 +23,6 @@ type PortableTextMemberSchemaTypes = {
23
23
  * schema type.
24
24
  */
25
25
  declare function createPortableTextMemberSchemaTypes(portableTextType: ArraySchemaType<PortableTextBlock>): PortableTextMemberSchemaTypes;
26
- /**
27
- * @public
28
- * Convert Sanity-specific schema types for Portable Text to a first-class
29
- * Portable Text schema.
30
- */
31
- declare function portableTextMemberSchemaTypesToSchema(schema: PortableTextMemberSchemaTypes): Schema;
32
26
  /**
33
27
  * @public
34
28
  * Compile a Sanity schema to a Portable Text `Schema`.
@@ -53,11 +47,5 @@ declare function portableTextMemberSchemaTypesToSchema(schema: PortableTextMembe
53
47
  * ```
54
48
  */
55
49
  declare function sanitySchemaToPortableTextSchema(sanitySchema: ArraySchemaType<unknown> | ArrayDefinition): Schema;
56
- /**
57
- * @public
58
- * Compile a Portable Text schema definition to Sanity-specific schema types for
59
- * Portable Text.
60
- */
61
- declare function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition?: SchemaDefinition): PortableTextMemberSchemaTypes;
62
- export { type PortableTextMemberSchemaTypes, compileSchemaDefinitionToPortableTextMemberSchemaTypes, createPortableTextMemberSchemaTypes, portableTextMemberSchemaTypesToSchema, sanitySchemaToPortableTextSchema };
50
+ export { type PortableTextMemberSchemaTypes, createPortableTextMemberSchemaTypes, sanitySchemaToPortableTextSchema };
63
51
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/portable-text-member-schema-types.ts","../src/portable-text-member-schema-types-to-schema.ts","../src/sanity-schema-to-portable-text-schema.ts","../src/schema-definition-to-portable-text-member-schema-types.ts"],"sourcesContent":[],"mappings":";;;;AAgBA;;AAES,KAFG,6BAAA,GAEH;EACO,WAAA,EAAA,CAFA,gBAEA,GAAA;IACF,YAAA,CAAA,EAAA,MAAA;EACG,CAAA,CAAA,EAAA;EACe,KAAA,EAJvB,gBAIuB;EAAhB,YAAA,EAHA,gBAGA,EAAA;EACR,UAAA,EAHM,wBAGN,EAAA;EACE,aAAA,EAHO,gBAGP,EAAA;EACD,YAAA,EAHO,eAGP,CAHuB,iBAGvB,CAAA;EAAmB,IAAA,EAFpB,gBAEoB;EAQZ,MAAA,EATN,oBASM,EAAA;EACoB,KAAA,EAT3B,mBAS2B,EAAA;CAAhB;;;;AC1BpB;;ACsBgB,iBFGA,mCAAA,CEHgC,gBAAA,EFI5B,eEJ4B,CFIZ,iBEJY,CAAA,CAAA,EFK7C,6BEL6C;;AFdhD;;;;AAIc,iBCZE,qCAAA,CDYF,MAAA,ECXJ,6BDWI,CAAA,ECVX,MDUW;;AAJd;;;;;;;;;;;;AAiBA;;;;;;ACzBA;;ACsBA;;AAC2C,iBAD3B,gCAAA,CAC2B,YAAA,EAA3B,eAA2B,CAAA,OAAA,CAAA,GAAA,eAAA,CAAA,EACxC,MADwC;;AFf3C;;;;AAIc,iBGME,sDAAA,CHNF,UAAA,CAAA,EGOC,gBHPD,CAAA,EGQX,6BHRW"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/portable-text-member-schema-types.ts","../src/sanity-schema-to-portable-text-schema.ts"],"sourcesContent":[],"mappings":";;;;AAgBA;;AAES,KAFG,6BAAA,GAEH;EACO,WAAA,EAAA,CAFA,gBAEA,GAAA;IACF,YAAA,CAAA,EAAA,MAAA;EACG,CAAA,CAAA,EAAA;EACe,KAAA,EAJvB,gBAIuB;EAAhB,YAAA,EAHA,gBAGA,EAAA;EACR,UAAA,EAHM,wBAGN,EAAA;EACE,aAAA,EAHO,gBAGP,EAAA;EACD,YAAA,EAHO,eAGP,CAHuB,iBAGvB,CAAA;EAAmB,IAAA,EAFpB,gBAEoB;EAQZ,MAAA,EATN,oBASM,EAAA;EACoB,KAAA,EAT3B,mBAS2B,EAAA;CAAhB;;;;ACKpB;;AAC2C,iBDP3B,mCAAA,CCO2B,gBAAA,EDNvB,eCMuB,CDNP,iBCMO,CAAA,CAAA,EDLxC,6BCKwC;;ADxB3C;;;;;;;;;;;;AAiBA;;;;;;ACMA;;;;AAES,iBAFO,gCAAA,CAEP,YAAA,EADO,eACP,CAAA,OAAA,CAAA,GADkC,eAClC,CAAA,EAAN,MAAM"}
package/dist/index.js CHANGED
@@ -1,10 +1,9 @@
1
1
  import { Schema } from "@sanity/schema";
2
2
  import { builtinTypes } from "@sanity/schema/_internal";
3
- import { defineType, defineField } from "@sanity/types";
4
3
  function createPortableTextMemberSchemaTypes(portableTextType) {
5
4
  if (!portableTextType)
6
5
  throw new Error("Parameter 'portabletextType' missing (required)");
7
- const blockType = portableTextType.of?.find(findBlockType);
6
+ const blockType = portableTextType.of?.find(findBlockType$1);
8
7
  if (!blockType)
9
8
  throw new Error("Block type is not defined in this schema (required)");
10
9
  const childrenField = blockType.fields?.find(
@@ -26,9 +25,9 @@ function createPortableTextMemberSchemaTypes(portableTextType) {
26
25
  (field) => field.name !== blockType.name
27
26
  ) || [];
28
27
  return {
29
- styles: resolveEnabledStyles(blockType),
30
- decorators: resolveEnabledDecorators(spanType),
31
- lists: resolveEnabledListItems(blockType),
28
+ styles: resolveEnabledStyles$1(blockType),
29
+ decorators: resolveEnabledDecorators$1(spanType),
30
+ lists: resolveEnabledListItems$1(blockType),
32
31
  block: blockType,
33
32
  span: spanType,
34
33
  portableText: portableTextType,
@@ -37,7 +36,7 @@ function createPortableTextMemberSchemaTypes(portableTextType) {
37
36
  annotations: spanType.annotations
38
37
  };
39
38
  }
40
- function resolveEnabledStyles(blockType) {
39
+ function resolveEnabledStyles$1(blockType) {
41
40
  const styleField = blockType.fields?.find(
42
41
  (btField) => btField.name === "style"
43
42
  );
@@ -54,10 +53,10 @@ function resolveEnabledStyles(blockType) {
54
53
  );
55
54
  return textStyles;
56
55
  }
57
- function resolveEnabledDecorators(spanType) {
56
+ function resolveEnabledDecorators$1(spanType) {
58
57
  return spanType.decorators;
59
58
  }
60
- function resolveEnabledListItems(blockType) {
59
+ function resolveEnabledListItems$1(blockType) {
61
60
  const listField = blockType.fields?.find(
62
61
  (btField) => btField.name === "listItem"
63
62
  );
@@ -70,66 +69,123 @@ function resolveEnabledListItems(blockType) {
70
69
  throw new Error("The list field need at least to be an empty array");
71
70
  return listItems;
72
71
  }
73
- function findBlockType(type) {
74
- return type.type ? findBlockType(type.type) : type.name === "block" ? type : null;
72
+ function findBlockType$1(type) {
73
+ return type.type ? findBlockType$1(type.type) : type.name === "block" ? type : null;
75
74
  }
76
- function portableTextMemberSchemaTypesToSchema(schema) {
75
+ function sanitySchemaToPortableTextSchema(sanitySchema) {
76
+ const compiled = sanitySchema.hasOwnProperty("jsonType") ? sanitySchema : compileType(sanitySchema);
77
+ return sanitySchemaTypeToSchema(compiled);
78
+ }
79
+ function sanitySchemaTypeToSchema(portableTextType) {
80
+ if (!portableTextType)
81
+ throw new Error("Parameter 'portableTextType' missing (required)");
82
+ const blockType = portableTextType.of?.find(findBlockType);
83
+ if (!blockType)
84
+ throw new Error("Block type is not defined in this schema (required)");
85
+ const childrenField = blockType.fields?.find(
86
+ (field) => field.name === "children"
87
+ );
88
+ if (!childrenField)
89
+ throw new Error("Children field for block type found in schema (required)");
90
+ const ofType = childrenField.type.of;
91
+ if (!ofType)
92
+ throw new Error(
93
+ "Valid types for block children not found in schema (required)"
94
+ );
95
+ const spanType = ofType.find((memberType) => memberType.name === "span");
96
+ if (!spanType)
97
+ throw new Error("Span type not found in schema (required)");
98
+ const inlineObjectTypes = ofType.filter(
99
+ (memberType) => memberType.name !== "span"
100
+ ) || [], blockObjectTypes = portableTextType.of?.filter(
101
+ (field) => field.name !== blockType.name
102
+ ) || [], styles = resolveEnabledStyles(blockType), decorators = resolveEnabledDecorators(spanType), lists = resolveEnabledListItems(blockType), annotations = spanType.annotations;
77
103
  return {
78
- annotations: schema.annotations.map((annotation) => ({
104
+ block: {
105
+ name: blockType.name
106
+ },
107
+ span: {
108
+ name: spanType.name
109
+ },
110
+ styles: styles.map((style) => ({
111
+ name: style.value,
112
+ title: style.title,
113
+ value: style.value
114
+ })),
115
+ lists: lists.map((list) => ({
116
+ name: list.value,
117
+ title: list.title,
118
+ value: list.value
119
+ })),
120
+ decorators: decorators.map((decorator) => ({
121
+ name: decorator.value,
122
+ title: decorator.title,
123
+ value: decorator.value
124
+ })),
125
+ annotations: annotations.map((annotation) => ({
79
126
  name: annotation.name,
127
+ title: annotation.title,
80
128
  fields: annotation.fields.map((field) => ({
81
129
  name: field.name,
82
130
  type: field.type.jsonType,
83
131
  title: field.type.title
84
- })),
85
- title: annotation.title
132
+ }))
86
133
  })),
87
- block: {
88
- name: schema.block.name
89
- },
90
- blockObjects: schema.blockObjects.map((blockObject) => ({
134
+ blockObjects: blockObjectTypes.map((blockObject) => ({
91
135
  name: blockObject.name,
136
+ title: blockObject.title,
92
137
  fields: blockObject.fields.map((field) => ({
93
138
  name: field.name,
94
139
  type: field.type.jsonType,
95
140
  title: field.type.title
96
- })),
97
- title: blockObject.title
98
- })),
99
- decorators: schema.decorators.map((decorator) => ({
100
- name: decorator.value,
101
- title: decorator.title,
102
- value: decorator.value
141
+ }))
103
142
  })),
104
- inlineObjects: schema.inlineObjects.map((inlineObject) => ({
143
+ inlineObjects: inlineObjectTypes.map((inlineObject) => ({
105
144
  name: inlineObject.name,
145
+ title: inlineObject.title,
106
146
  fields: inlineObject.fields.map((field) => ({
107
147
  name: field.name,
108
148
  type: field.type.jsonType,
109
149
  title: field.type.title
110
- })),
111
- title: inlineObject.title
112
- })),
113
- span: {
114
- name: schema.span.name
115
- },
116
- styles: schema.styles.map((style) => ({
117
- name: style.value,
118
- title: style.title,
119
- value: style.value
120
- })),
121
- lists: schema.lists.map((list) => ({
122
- name: list.value,
123
- title: list.title,
124
- value: list.value
150
+ }))
125
151
  }))
126
152
  };
127
153
  }
128
- function sanitySchemaToPortableTextSchema(sanitySchema) {
129
- const portableTextMemberSchemaTypes = createPortableTextMemberSchemaTypes(
130
- sanitySchema.hasOwnProperty("jsonType") ? sanitySchema : compileType(sanitySchema)
154
+ function resolveEnabledStyles(blockType) {
155
+ const styleField = blockType.fields?.find(
156
+ (btField) => btField.name === "style"
131
157
  );
132
- return portableTextMemberSchemaTypesToSchema(portableTextMemberSchemaTypes);
158
+ if (!styleField)
159
+ throw new Error(
160
+ "A field with name 'style' is not defined in the block type (required)."
161
+ );
162
+ const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter(
163
+ (style) => style.value
164
+ );
165
+ if (!textStyles || textStyles.length === 0)
166
+ throw new Error(
167
+ "The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}."
168
+ );
169
+ return textStyles;
170
+ }
171
+ function resolveEnabledDecorators(spanType) {
172
+ return spanType.decorators;
173
+ }
174
+ function resolveEnabledListItems(blockType) {
175
+ const listField = blockType.fields?.find(
176
+ (btField) => btField.name === "listItem"
177
+ );
178
+ if (!listField)
179
+ throw new Error(
180
+ "A field with name 'listItem' is not defined in the block type (required)."
181
+ );
182
+ const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
183
+ if (!listItems)
184
+ throw new Error("The list field need at least to be an empty array");
185
+ return listItems;
186
+ }
187
+ function findBlockType(type) {
188
+ return type.type ? findBlockType(type.type) : type.name === "block" ? type : null;
133
189
  }
134
190
  function compileType(rawType) {
135
191
  return Schema.compile({
@@ -137,134 +193,8 @@ function compileType(rawType) {
137
193
  types: [rawType, ...builtinTypes]
138
194
  }).get(rawType.name);
139
195
  }
140
- const keyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
141
- let table;
142
- return () => {
143
- if (table)
144
- return table;
145
- table = [];
146
- for (let i = 0; i < 256; ++i)
147
- table[i] = (i + 256).toString(16).slice(1);
148
- return table;
149
- };
150
- })();
151
- function whatwgRNG(length = 16) {
152
- const rnds8 = new Uint8Array(length);
153
- return crypto.getRandomValues(rnds8), rnds8;
154
- }
155
- function randomKey(length) {
156
- const table = getByteHexTable();
157
- return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
158
- }
159
- function startCase(str) {
160
- return str.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/[_-]+/g, " ").trim().split(/\s+/).filter(Boolean).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
161
- }
162
- const defaultObjectTitles = {
163
- image: "Image",
164
- url: "URL"
165
- }, sanityBuiltinNames = /* @__PURE__ */ new Set(["file", "geopoint", "image", "slug", "url"]);
166
- function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
167
- const blockObjectDefs = definition?.blockObjects ?? [], inlineObjectDefs = definition?.inlineObjects ?? [], blockObjectNameSet = new Set(
168
- blockObjectDefs.map((blockObject) => blockObject.name)
169
- ), inlineObjectNameSet = new Set(
170
- inlineObjectDefs.map((inlineObject) => inlineObject.name)
171
- ), temporaryBlockObjectNames = {}, temporaryInlineObjectNames = {}, blockObjectNames = {}, inlineObjectNames = {};
172
- for (const name of blockObjectNameSet)
173
- if (sanityBuiltinNames.has(name) || inlineObjectNameSet.has(name)) {
174
- const tmpName = `tmp-${keyGenerator()}-${name}`;
175
- temporaryBlockObjectNames[name] = tmpName, blockObjectNames[tmpName] = name;
176
- }
177
- for (const name of inlineObjectNameSet)
178
- if (sanityBuiltinNames.has(name) || blockObjectNameSet.has(name)) {
179
- const tmpName = `tmp-${keyGenerator()}-${name}`;
180
- temporaryInlineObjectNames[name] = tmpName, inlineObjectNames[tmpName] = name;
181
- }
182
- const blockObjects = blockObjectDefs.map(
183
- (blockObject) => defineType({
184
- type: "object",
185
- // Use temporary names to work around `SanitySchema.compile` adding
186
- // default fields to objects with certain names, and to avoid duplicate
187
- // type names when a type appears in both blockObjects and inlineObjects.
188
- name: temporaryBlockObjectNames[blockObject.name] ?? blockObject.name,
189
- title: blockObject.title === void 0 ? (
190
- // This avoids the default title which is a title case of the object name
191
- defaultObjectTitles[blockObject.name]
192
- ) : blockObject.title,
193
- fields: blockObject.fields?.map((field) => ({
194
- name: field.name,
195
- type: field.type,
196
- title: field.title ?? startCase(field.name)
197
- })) ?? []
198
- })
199
- ), inlineObjects = inlineObjectDefs.map(
200
- (inlineObject) => defineType({
201
- type: "object",
202
- // Use temporary names to work around `SanitySchema.compile` adding
203
- // default fields to objects with certain names, and to avoid duplicate
204
- // type names when a type appears in both blockObjects and inlineObjects.
205
- name: temporaryInlineObjectNames[inlineObject.name] ?? inlineObject.name,
206
- title: inlineObject.title === void 0 ? (
207
- // This avoids the default title which is a title case of the object name
208
- defaultObjectTitles[inlineObject.name]
209
- ) : inlineObject.title,
210
- fields: inlineObject.fields?.map((field) => ({
211
- name: field.name,
212
- type: field.type,
213
- title: field.title ?? startCase(field.name)
214
- })) ?? []
215
- })
216
- ), portableTextSchema = defineField({
217
- type: "array",
218
- name: "portable-text",
219
- of: [
220
- ...blockObjects.map((blockObject) => ({ type: blockObject.name })),
221
- {
222
- type: "block",
223
- name: "block",
224
- of: inlineObjects.map((inlineObject) => ({ type: inlineObject.name })),
225
- marks: {
226
- decorators: definition?.decorators?.map((decorator) => ({
227
- title: decorator.title ?? startCase(decorator.name),
228
- value: decorator.name
229
- })) ?? [],
230
- annotations: definition?.annotations?.map((annotation) => ({
231
- name: annotation.name,
232
- type: "object",
233
- title: annotation.title,
234
- fields: annotation.fields?.map((field) => ({
235
- name: field.name,
236
- title: field.title ?? startCase(field.name),
237
- type: field.type
238
- })) ?? []
239
- })) ?? []
240
- },
241
- lists: definition?.lists?.map((list) => ({
242
- value: list.name,
243
- title: list.title ?? startCase(list.name)
244
- })) ?? [],
245
- styles: definition?.styles?.map((style) => ({
246
- value: style.name,
247
- title: style.title ?? startCase(style.name)
248
- })) ?? []
249
- }
250
- ]
251
- }), schema = Schema.compile({
252
- types: [portableTextSchema, ...blockObjects, ...inlineObjects]
253
- }).get("portable-text"), pteSchema = createPortableTextMemberSchemaTypes(schema);
254
- for (const blockObject of pteSchema.blockObjects) {
255
- const originalName = blockObjectNames[blockObject.name];
256
- originalName !== void 0 && (blockObject.name = originalName, blockObject.type && (blockObject.type = { ...blockObject.type, name: originalName }));
257
- }
258
- for (const inlineObject of pteSchema.inlineObjects) {
259
- const originalName = inlineObjectNames[inlineObject.name];
260
- originalName !== void 0 && (inlineObject.name = originalName, inlineObject.type && (inlineObject.type = { ...inlineObject.type, name: originalName }));
261
- }
262
- return pteSchema;
263
- }
264
196
  export {
265
- compileSchemaDefinitionToPortableTextMemberSchemaTypes,
266
197
  createPortableTextMemberSchemaTypes,
267
- portableTextMemberSchemaTypesToSchema,
268
198
  sanitySchemaToPortableTextSchema
269
199
  };
270
200
  //# sourceMappingURL=index.js.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/start-case.ts","../src/schema-definition-to-portable-text-member-schema-types.ts"],"sourcesContent":["import type {\n ArraySchemaType,\n BlockDecoratorDefinition,\n BlockListDefinition,\n BlockSchemaType,\n BlockStyleDefinition,\n ObjectSchemaType,\n PortableTextBlock,\n SchemaType,\n SpanSchemaType,\n} from '@sanity/types'\n\n/**\n * @public\n * Sanity-specific schema types for Portable Text.\n */\nexport type PortableTextMemberSchemaTypes = {\n annotations: (ObjectSchemaType & {i18nTitleKey?: string})[]\n block: ObjectSchemaType\n blockObjects: ObjectSchemaType[]\n decorators: BlockDecoratorDefinition[]\n inlineObjects: ObjectSchemaType[]\n portableText: ArraySchemaType<PortableTextBlock>\n span: ObjectSchemaType\n styles: BlockStyleDefinition[]\n lists: BlockListDefinition[]\n}\n\n/**\n * @public\n * Create Sanity-specific schema types for Portable Text from a Sanity array\n * schema type.\n */\nexport function createPortableTextMemberSchemaTypes(\n portableTextType: ArraySchemaType<PortableTextBlock>,\n): PortableTextMemberSchemaTypes {\n if (!portableTextType) {\n throw new Error(\"Parameter 'portabletextType' missing (required)\")\n }\n const blockType = portableTextType.of?.find(findBlockType) as\n | BlockSchemaType\n | undefined\n if (!blockType) {\n throw new Error('Block type is not defined in this schema (required)')\n }\n const childrenField = blockType.fields?.find(\n (field) => field.name === 'children',\n ) as {type: ArraySchemaType} | undefined\n if (!childrenField) {\n throw new Error('Children field for block type found in schema (required)')\n }\n const ofType = childrenField.type.of\n if (!ofType) {\n throw new Error(\n 'Valid types for block children not found in schema (required)',\n )\n }\n const spanType = ofType.find((memberType) => memberType.name === 'span') as\n | ObjectSchemaType\n | undefined\n if (!spanType) {\n throw new Error('Span type not found in schema (required)')\n }\n const inlineObjectTypes = (ofType.filter(\n (memberType) => memberType.name !== 'span',\n ) || []) as ObjectSchemaType[]\n const blockObjectTypes = (portableTextType.of?.filter(\n (field) => field.name !== blockType.name,\n ) || []) as ObjectSchemaType[]\n return {\n styles: resolveEnabledStyles(blockType),\n decorators: resolveEnabledDecorators(spanType),\n lists: resolveEnabledListItems(blockType),\n block: blockType,\n span: spanType,\n portableText: portableTextType,\n inlineObjects: inlineObjectTypes,\n blockObjects: blockObjectTypes,\n annotations: (spanType as SpanSchemaType).annotations,\n }\n}\n\nfunction resolveEnabledStyles(blockType: ObjectSchemaType) {\n const styleField = blockType.fields?.find(\n (btField) => btField.name === 'style',\n )\n if (!styleField) {\n throw new Error(\n \"A field with name 'style' is not defined in the block type (required).\",\n )\n }\n const textStyles =\n styleField.type.options?.list &&\n styleField.type.options.list?.filter(\n (style: {value: string}) => style.value,\n )\n if (!textStyles || textStyles.length === 0) {\n throw new Error(\n 'The style fields need at least one style ' +\n \"defined. I.e: {title: 'Normal', value: 'normal'}.\",\n )\n }\n return textStyles\n}\n\nfunction resolveEnabledDecorators(spanType: ObjectSchemaType) {\n return (spanType as any).decorators\n}\n\nfunction resolveEnabledListItems(blockType: ObjectSchemaType) {\n const listField = blockType.fields?.find(\n (btField) => btField.name === 'listItem',\n )\n if (!listField) {\n throw new Error(\n \"A field with name 'listItem' is not defined in the block type (required).\",\n )\n }\n const listItems =\n listField.type.options?.list &&\n listField.type.options.list.filter((list: {value: string}) => list.value)\n if (!listItems) {\n throw new Error('The list field need at least to be an empty array')\n }\n return listItems\n}\n\nfunction findBlockType(type: SchemaType): BlockSchemaType | null {\n if (type.type) {\n return findBlockType(type.type)\n }\n\n if (type.name === 'block') {\n return type as BlockSchemaType\n }\n\n return null\n}\n","import type {Schema} from '@portabletext/schema'\nimport type {PortableTextMemberSchemaTypes} from './portable-text-member-schema-types'\n\n/**\n * @public\n * Convert Sanity-specific schema types for Portable Text to a first-class\n * Portable Text schema.\n */\nexport function portableTextMemberSchemaTypesToSchema(\n schema: PortableTextMemberSchemaTypes,\n): Schema {\n return {\n annotations: schema.annotations.map((annotation) => ({\n name: annotation.name,\n fields: annotation.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: annotation.title,\n })),\n block: {\n name: schema.block.name,\n },\n blockObjects: schema.blockObjects.map((blockObject) => ({\n name: blockObject.name,\n fields: blockObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: blockObject.title,\n })),\n decorators: schema.decorators.map((decorator) => ({\n name: decorator.value,\n title: decorator.title,\n value: decorator.value,\n })),\n inlineObjects: schema.inlineObjects.map((inlineObject) => ({\n name: inlineObject.name,\n fields: inlineObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: inlineObject.title,\n })),\n span: {\n name: schema.span.name,\n },\n styles: schema.styles.map((style) => ({\n name: style.value,\n title: style.title,\n value: style.value,\n })),\n lists: schema.lists.map((list) => ({\n name: list.value,\n title: list.title,\n value: list.value,\n })),\n }\n}\n","import type {Schema} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport {builtinTypes} from '@sanity/schema/_internal'\nimport type {ArrayDefinition, ArraySchemaType} from '@sanity/types'\nimport {createPortableTextMemberSchemaTypes} from './portable-text-member-schema-types'\nimport {portableTextMemberSchemaTypesToSchema} from './portable-text-member-schema-types-to-schema'\n\n/**\n * @public\n * Compile a Sanity schema to a Portable Text `Schema`.\n *\n * A Portable Text `Schema` is compatible with a Portable Text\n * `SchemaDefinition` and can be used as configuration for the Portable Text\n * Editor.\n *\n * @example\n * ```tsx\n * const schema = sanitySchemaToPortableTextSchema(sanitySchema)\n *\n * return (\n * <EditorProvider\n * initialConfig={{\n * // ...\n * schemaDefinition: schema,\n * }}\n * >\n * // ...\n * </EditorProvider>\n * ```\n */\nexport function sanitySchemaToPortableTextSchema(\n sanitySchema: ArraySchemaType<unknown> | ArrayDefinition,\n): Schema {\n const portableTextMemberSchemaTypes = createPortableTextMemberSchemaTypes(\n sanitySchema.hasOwnProperty('jsonType')\n ? sanitySchema\n : compileType(sanitySchema),\n )\n\n return portableTextMemberSchemaTypesToSchema(portableTextMemberSchemaTypes)\n}\n\nfunction compileType(rawType: any) {\n return SanitySchema.compile({\n name: 'blockTypeSchema',\n types: [rawType, ...builtinTypes],\n }).get(rawType.name)\n}\n","export const keyGenerator = (): string => randomKey(12)\n\nconst getByteHexTable = (() => {\n let table: any[]\n return () => {\n if (table) {\n return table\n }\n\n table = []\n for (let i = 0; i < 256; ++i) {\n table[i] = (i + 0x100).toString(16).slice(1)\n }\n return table\n }\n})()\n\n// WHATWG crypto RNG - https://w3c.github.io/webcrypto/Overview.html\nfunction whatwgRNG(length = 16) {\n const rnds8 = new Uint8Array(length)\n crypto.getRandomValues(rnds8)\n return rnds8\n}\n\nfunction randomKey(length?: number): string {\n const table = getByteHexTable()\n return whatwgRNG(length)\n .reduce((str, n) => str + table[n], '')\n .slice(0, length)\n}\n","export function startCase(str: string): string {\n return (\n str\n // Insert space before uppercase letters in camelCase (e.g., 'fooBar' -> 'foo Bar')\n .replace(/([a-z])([A-Z])/g, '$1 $2')\n // Replace underscores and dashes with spaces\n .replace(/[_-]+/g, ' ')\n // Trim and split on whitespace\n .trim()\n .split(/\\s+/)\n .filter(Boolean)\n // Capitalize first letter of each word, preserve rest\n .map((word) => word.charAt(0).toUpperCase() + word.slice(1))\n .join(' ')\n )\n}\n","import type {SchemaDefinition} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport {defineField, defineType} from '@sanity/types'\nimport {keyGenerator} from './key-generator'\nimport {\n createPortableTextMemberSchemaTypes,\n type PortableTextMemberSchemaTypes,\n} from './portable-text-member-schema-types'\nimport {startCase} from './start-case'\n\nconst defaultObjectTitles: Record<string, string> = {\n image: 'Image',\n url: 'URL',\n}\n\n/**\n * Names that conflict with Sanity's built-in schema types and need temporary\n * names during `SanitySchema.compile` to avoid getting default fields added.\n */\nconst sanityBuiltinNames = new Set(['file', 'geopoint', 'image', 'slug', 'url'])\n\n/**\n * @public\n * Compile a Portable Text schema definition to Sanity-specific schema types for\n * Portable Text.\n */\nexport function compileSchemaDefinitionToPortableTextMemberSchemaTypes(\n definition?: SchemaDefinition,\n): PortableTextMemberSchemaTypes {\n const blockObjectDefs = definition?.blockObjects ?? []\n const inlineObjectDefs = definition?.inlineObjects ?? []\n\n // Collect names that appear in both blockObjects and inlineObjects, or that\n // conflict with Sanity built-in types. These need temporary names so that\n // `SanitySchema.compile` doesn't see duplicate type registrations.\n const blockObjectNameSet = new Set(\n blockObjectDefs.map((blockObject) => blockObject.name),\n )\n const inlineObjectNameSet = new Set(\n inlineObjectDefs.map((inlineObject) => inlineObject.name),\n )\n\n const temporaryBlockObjectNames: Record<string, string> = {}\n const temporaryInlineObjectNames: Record<string, string> = {}\n const blockObjectNames: Record<string, string> = {}\n const inlineObjectNames: Record<string, string> = {}\n\n for (const name of blockObjectNameSet) {\n if (sanityBuiltinNames.has(name) || inlineObjectNameSet.has(name)) {\n const tmpName = `tmp-${keyGenerator()}-${name}`\n temporaryBlockObjectNames[name] = tmpName\n blockObjectNames[tmpName] = name\n }\n }\n\n for (const name of inlineObjectNameSet) {\n if (sanityBuiltinNames.has(name) || blockObjectNameSet.has(name)) {\n const tmpName = `tmp-${keyGenerator()}-${name}`\n temporaryInlineObjectNames[name] = tmpName\n inlineObjectNames[tmpName] = name\n }\n }\n\n const blockObjects = blockObjectDefs.map((blockObject) =>\n defineType({\n type: 'object',\n // Use temporary names to work around `SanitySchema.compile` adding\n // default fields to objects with certain names, and to avoid duplicate\n // type names when a type appears in both blockObjects and inlineObjects.\n name: temporaryBlockObjectNames[blockObject.name] ?? blockObject.name,\n title:\n blockObject.title === undefined\n ? // This avoids the default title which is a title case of the object name\n defaultObjectTitles[blockObject.name]\n : blockObject.title,\n fields:\n blockObject.fields?.map((field) => ({\n name: field.name,\n type: field.type,\n title: field.title ?? startCase(field.name),\n })) ?? [],\n }),\n )\n\n const inlineObjects = inlineObjectDefs.map((inlineObject) =>\n defineType({\n type: 'object',\n // Use temporary names to work around `SanitySchema.compile` adding\n // default fields to objects with certain names, and to avoid duplicate\n // type names when a type appears in both blockObjects and inlineObjects.\n name: temporaryInlineObjectNames[inlineObject.name] ?? inlineObject.name,\n\n title:\n inlineObject.title === undefined\n ? // This avoids the default title which is a title case of the object name\n defaultObjectTitles[inlineObject.name]\n : inlineObject.title,\n fields:\n inlineObject.fields?.map((field) => ({\n name: field.name,\n type: field.type,\n title: field.title ?? startCase(field.name),\n })) ?? [],\n }),\n )\n\n const portableTextSchema = defineField({\n type: 'array',\n name: 'portable-text',\n of: [\n ...blockObjects.map((blockObject) => ({type: blockObject.name})),\n {\n type: 'block',\n name: 'block',\n of: inlineObjects.map((inlineObject) => ({type: inlineObject.name})),\n marks: {\n decorators:\n definition?.decorators?.map((decorator) => ({\n title: decorator.title ?? startCase(decorator.name),\n value: decorator.name,\n })) ?? [],\n annotations:\n definition?.annotations?.map((annotation) => ({\n name: annotation.name,\n type: 'object',\n title: annotation.title,\n fields:\n annotation.fields?.map((field) => ({\n name: field.name,\n title: field.title ?? startCase(field.name),\n type: field.type,\n })) ?? [],\n })) ?? [],\n },\n lists:\n definition?.lists?.map((list) => ({\n value: list.name,\n title: list.title ?? startCase(list.name),\n })) ?? [],\n styles:\n definition?.styles?.map((style) => ({\n value: style.name,\n title: style.title ?? startCase(style.name),\n })) ?? [],\n },\n ],\n })\n\n const schema = SanitySchema.compile({\n types: [portableTextSchema, ...blockObjects, ...inlineObjects],\n }).get('portable-text')\n\n const pteSchema = createPortableTextMemberSchemaTypes(schema)\n\n // Restore original names on blockObjects and inlineObjects.\n // These are shared references with portableText.of, so mutating them\n // also restores names in the portableText array type's nested structure.\n for (const blockObject of pteSchema.blockObjects) {\n const originalName = blockObjectNames[blockObject.name]\n if (originalName !== undefined) {\n blockObject.name = originalName\n if (blockObject.type) {\n blockObject.type = {...blockObject.type, name: originalName}\n }\n }\n }\n\n for (const inlineObject of pteSchema.inlineObjects) {\n const originalName = inlineObjectNames[inlineObject.name]\n if (originalName !== undefined) {\n inlineObject.name = originalName\n if (inlineObject.type) {\n inlineObject.type = {...inlineObject.type, name: originalName}\n }\n }\n }\n\n return pteSchema\n}\n"],"names":["SanitySchema"],"mappings":";;;AAiCO,SAAS,oCACd,kBAC+B;AAC/B,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,iDAAiD;AAEnE,QAAM,YAAY,iBAAiB,IAAI,KAAK,aAAa;AAGzD,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,qDAAqD;AAEvE,QAAM,gBAAgB,UAAU,QAAQ;AAAA,IACtC,CAAC,UAAU,MAAM,SAAS;AAAA,EAAA;AAE5B,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,0DAA0D;AAE5E,QAAM,SAAS,cAAc,KAAK;AAClC,MAAI,CAAC;AACH,UAAM,IAAI;AAAA,MACR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ACjB,MAAM;AAAA,YACN,OAAO,WAAW;AAAA,YAClB,QACE,WAAW,QAAQ,IAAI,CAAC,WAAW;AAAA,cACjC,MAAM,MAAM;AAAA,cACZ,OAAO,MAAM,SAAS,UAAU,MAAM,IAAI;AAAA,cAC1C,MAAM,MAAM;AAAA,YAAA,EACZ,KAAK,CAAA;AAAA,UAAC,EACV,KAAK,CAAA;AAAA,QAAC;AAAA,QAEZ,OACE,YAAY,OAAO,IAAI,CAAC,UAAU;AAAA,UAChC,OAAO,KAAK;AAAA,UACZ,OAAO,KAAK,SAAS,UAAU,KAAK,IAAI;AAAA,QAAA,EACxC,KAAK,CAAA;AAAA,QACT,QACE,YAAY,QAAQ,IAAI,CAAC,WAAW;AAAA,UAClC,OAAO,MAAM;AAAA,UACb,OAAO,MAAM,SAAS,UAAU,MAAM,IAAI;AAAA,QAAA,EAC1C,KAAK,CAAA;AAAA,MAAC;AAAA,IACZ;AAAA,EACF,CACD,GAEK,SAASA,OAAa,QAAQ;AAAA,IAClC,OAAO,CAAC,oBAAoB,GAAG,cAAc,GAAG,aAAa;AAAA,EAAA,CAC9D,EAAE,IAAI,eAAe,GAEhB,YAAY,oCAAoC,MAAM;AAK5D,aAAW,eAAe,UAAU,cAAc;AAChD,UAAM,eAAe,iBAAiB,YAAY,IAAI;AAClD,qBAAiB,WACnB,YAAY,OAAO,cACf,YAAY,SACd,YAAY,OAAO,EAAC,GAAG,YAAY,MAAM,MAAM,aAAA;AAAA,EAGrD;AAEA,aAAW,gBAAgB,UAAU,eAAe;AAClD,UAAM,eAAe,kBAAkB,aAAa,IAAI;AACpD,qBAAiB,WACnB,aAAa,OAAO,cAChB,aAAa,SACf,aAAa,OAAO,EAAC,GAAG,aAAa,MAAM,MAAM,aAAA;AAAA,EAGvD;AAEA,SAAO;AACT;"}
1
+ {"version":3,"file":"index.js","sources":["../src/portable-text-member-schema-types.ts","../src/sanity-schema-to-portable-text-schema.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 {Schema as SanitySchema} from '@sanity/schema'\nimport {builtinTypes} from '@sanity/schema/_internal'\nimport type {\n ArrayDefinition,\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 * 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 compiled = sanitySchema.hasOwnProperty('jsonType')\n ? (sanitySchema as ArraySchemaType<PortableTextBlock>)\n : compileType(sanitySchema)\n\n return sanitySchemaTypeToSchema(compiled)\n}\n\nfunction sanitySchemaTypeToSchema(\n portableTextType: ArraySchemaType<PortableTextBlock>,\n): Schema {\n if (!portableTextType) {\n throw new Error(\"Parameter 'portableTextType' missing (required)\")\n }\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\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\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\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\n const inlineObjectTypes = (ofType.filter(\n (memberType) => memberType.name !== 'span',\n ) || []) as ObjectSchemaType[]\n\n const blockObjectTypes = (portableTextType.of?.filter(\n (field) => field.name !== blockType.name,\n ) || []) as ObjectSchemaType[]\n\n const styles = resolveEnabledStyles(blockType)\n const decorators = resolveEnabledDecorators(spanType)\n const lists = resolveEnabledListItems(blockType)\n const annotations = (spanType as SpanSchemaType).annotations\n\n return {\n block: {\n name: blockType.name,\n },\n span: {\n name: spanType.name,\n },\n styles: styles.map((style: BlockStyleDefinition) => ({\n name: style.value,\n title: style.title,\n value: style.value,\n })),\n lists: lists.map((list: BlockListDefinition) => ({\n name: list.value,\n title: list.title,\n value: list.value,\n })),\n decorators: decorators.map((decorator: BlockDecoratorDefinition) => ({\n name: decorator.value,\n title: decorator.title,\n value: decorator.value,\n })),\n annotations: annotations.map((annotation) => ({\n name: annotation.name,\n title: annotation.title,\n fields: annotation.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n })),\n blockObjects: blockObjectTypes.map((blockObject) => ({\n name: blockObject.name,\n title: blockObject.title,\n fields: blockObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n })),\n inlineObjects: inlineObjectTypes.map((inlineObject) => ({\n name: inlineObject.name,\n title: inlineObject.title,\n fields: inlineObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n })),\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\nfunction compileType(rawType: any) {\n return SanitySchema.compile({\n name: 'blockTypeSchema',\n types: [rawType, ...builtinTypes],\n }).get(rawType.name)\n}\n"],"names":["findBlockType","resolveEnabledStyles","resolveEnabledDecorators","resolveEnabledListItems","SanitySchema"],"mappings":";;AAiCO,SAAS,oCACd,kBAC+B;AAC/B,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,iDAAiD;AAEnE,QAAM,YAAY,iBAAiB,IAAI,KAAKA,eAAa;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,QAAQC,uBAAqB,SAAS;AAAA,IACtC,YAAYC,2BAAyB,QAAQ;AAAA,IAC7C,OAAOC,0BAAwB,SAAS;AAAA,IACxC,OAAO;AAAA,IACP,MAAM;AAAA,IACN,cAAc;AAAA,IACd,eAAe;AAAA,IACf,cAAc;AAAA,IACd,aAAc,SAA4B;AAAA,EAAA;AAE9C;AAEA,SAASF,uBAAqB,WAA6B;AACzD,QAAM,aAAa,UAAU,QAAQ;AAAA,IACnC,CAAC,YAAY,QAAQ,SAAS;AAAA,EAAA;AAEhC,MAAI,CAAC;AACH,UAAM,IAAI;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M,mDAAmD;AAErE,SAAO;AACT;AAEA,SAAS,cAAc,MAA0C;AAC/D,SAAI,KAAK,OACA,cAAc,KAAK,IAAI,IAG5B,KAAK,SAAS,UACT,OAGF;AACT;AAEA,SAAS,YAAY,SAAc;AACjC,SAAOI,OAAa,QAAQ;AAAA,IAC1B,MAAM;AAAA,IACN,OAAO,CAAC,SAAS,GAAG,YAAY;AAAA,EAAA,CACjC,EAAE,IAAI,QAAQ,IAAI;AACrB;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/sanity-bridge",
3
- "version": "2.0.2",
3
+ "version": "3.0.0",
4
4
  "description": "Convert a Sanity Schema to a Portable Text Schema",
5
5
  "keywords": [
6
6
  "sanity",
@@ -37,7 +37,7 @@
37
37
  "@sanity/pkg-utils": "^10.2.1",
38
38
  "@sanity/tsconfig": "^2.1.0",
39
39
  "typescript": "5.9.3",
40
- "vitest": "^4.0.16"
40
+ "vitest": "^4.0.18"
41
41
  },
42
42
  "engines": {
43
43
  "node": ">=20.19 <22 || >=22.12"