@portabletext/sanity-bridge 1.0.0 → 1.1.1
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/README.md +91 -8
- package/dist/index.cjs +56 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +26 -2
- package/dist/index.d.ts +26 -2
- package/dist/index.js +58 -15
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/index.ts +1 -0
- package/src/sanity-schema-to-portable-text-schema.ts +47 -0
- package/src/schema-definition-to-portable-text-member-schema-types.ts +74 -16
package/README.md
CHANGED
|
@@ -19,16 +19,84 @@ npm install @sanity/schema @sanity/types
|
|
|
19
19
|
### Convert Sanity Schema to Portable Text Schema
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
-
portableTextMemberSchemaTypesToSchema,
|
|
25
|
-
} from '@portabletext/sanity-bridge'
|
|
22
|
+
import {Schema} from '@sanity/schema'
|
|
23
|
+
import {defineField, defineType} from '@sanity/types'
|
|
26
24
|
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Define the Sanity Schema
|
|
27
|
+
*/
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
const imageType = defineType({
|
|
30
|
+
name: 'custom image',
|
|
31
|
+
title: 'Image',
|
|
32
|
+
type: 'object',
|
|
33
|
+
fields: [
|
|
34
|
+
defineField({
|
|
35
|
+
name: 'url',
|
|
36
|
+
type: 'string',
|
|
37
|
+
}),
|
|
38
|
+
],
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const stockTickerType = defineType({
|
|
42
|
+
name: 'stock-ticker',
|
|
43
|
+
type: 'object',
|
|
44
|
+
fields: [
|
|
45
|
+
defineField({
|
|
46
|
+
name: 'symbol',
|
|
47
|
+
type: 'string',
|
|
48
|
+
}),
|
|
49
|
+
],
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const portableTextType = defineType({
|
|
53
|
+
type: 'array',
|
|
54
|
+
name: 'body',
|
|
55
|
+
of: [
|
|
56
|
+
{
|
|
57
|
+
type: 'block',
|
|
58
|
+
name: 'block',
|
|
59
|
+
styles: [
|
|
60
|
+
{title: 'Normal', value: 'normal'},
|
|
61
|
+
{title: 'H1', value: 'h1'},
|
|
62
|
+
{title: 'H2', value: 'h2'},
|
|
63
|
+
{title: 'H3', value: 'h3'},
|
|
64
|
+
{title: 'H4', value: 'h4'},
|
|
65
|
+
{title: 'H5', value: 'h5'},
|
|
66
|
+
{title: 'H6', value: 'h6'},
|
|
67
|
+
{title: 'Quote', value: 'blockquote'},
|
|
68
|
+
],
|
|
69
|
+
marks: {
|
|
70
|
+
annotations: [
|
|
71
|
+
{
|
|
72
|
+
name: 'comment',
|
|
73
|
+
type: 'object',
|
|
74
|
+
fields: [{type: 'string', name: 'text'}],
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: 'link',
|
|
78
|
+
type: 'object',
|
|
79
|
+
fields: [{type: 'string', name: 'href'}],
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
of: [{type: 'stock-ticker'}],
|
|
84
|
+
},
|
|
85
|
+
{type: 'custom image'},
|
|
86
|
+
],
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Compile the Sanity Schema
|
|
91
|
+
*/
|
|
92
|
+
const sanitySchema = Schema.compile({
|
|
93
|
+
types: [portableTextType, imageType, stockTickerType],
|
|
94
|
+
}).get('body')
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Turn the Sanity Schema into a Portable Text Schema
|
|
98
|
+
*/
|
|
99
|
+
const portableTextSchema = sanitySchemaToPortableTextSchema(sanitySchema)
|
|
32
100
|
```
|
|
33
101
|
|
|
34
102
|
### Convert Portable Text Schema to Sanity Schema
|
|
@@ -45,3 +113,18 @@ const sanityMemberTypes =
|
|
|
45
113
|
blockObjects: [{name: 'image', fields: [{name: 'url', type: 'url'}]}],
|
|
46
114
|
})
|
|
47
115
|
```
|
|
116
|
+
|
|
117
|
+
### Additional helper functions
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import {
|
|
121
|
+
createPortableTextMemberSchemaTypes,
|
|
122
|
+
portableTextMemberSchemaTypesToSchema,
|
|
123
|
+
} from '@portabletext/sanity-bridge'
|
|
124
|
+
|
|
125
|
+
// Extract Portable Text member types from Sanity schema
|
|
126
|
+
const memberTypes = createPortableTextMemberSchemaTypes(sanityPortableTextType)
|
|
127
|
+
|
|
128
|
+
// Convert to first-class Portable Text schema
|
|
129
|
+
const portableTextSchema = portableTextMemberSchemaTypesToSchema(memberTypes)
|
|
130
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -129,6 +129,18 @@ function portableTextMemberSchemaTypesToSchema(schema2) {
|
|
|
129
129
|
}))
|
|
130
130
|
};
|
|
131
131
|
}
|
|
132
|
+
function sanitySchemaToPortableTextSchema(sanitySchema) {
|
|
133
|
+
const portableTextMemberSchemaTypes = createPortableTextMemberSchemaTypes(
|
|
134
|
+
sanitySchema.hasOwnProperty("jsonType") ? sanitySchema : compileType(sanitySchema)
|
|
135
|
+
);
|
|
136
|
+
return portableTextMemberSchemaTypesToSchema(portableTextMemberSchemaTypes);
|
|
137
|
+
}
|
|
138
|
+
function compileType(rawType) {
|
|
139
|
+
return schema.Schema.compile({
|
|
140
|
+
name: "blockTypeSchema",
|
|
141
|
+
types: [rawType]
|
|
142
|
+
}).get(rawType.name);
|
|
143
|
+
}
|
|
132
144
|
const keyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
|
|
133
145
|
let table;
|
|
134
146
|
return () => {
|
|
@@ -148,12 +160,18 @@ function randomKey(length) {
|
|
|
148
160
|
const table = getByteHexTable();
|
|
149
161
|
return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
|
|
150
162
|
}
|
|
151
|
-
const
|
|
152
|
-
image:
|
|
153
|
-
url:
|
|
154
|
-
},
|
|
155
|
-
|
|
156
|
-
|
|
163
|
+
const temporaryImageBlockObjectName = `tmp-${keyGenerator()}-image`, temporaryUrlBlockObjectName = `tmp-${keyGenerator()}-url`, temporaryImageInlineObjectName = `tmp-${keyGenerator()}-image`, temporaryUrlInlineObjectName = `tmp-${keyGenerator()}-url`, temporaryBlockObjectNames = {
|
|
164
|
+
image: temporaryImageBlockObjectName,
|
|
165
|
+
url: temporaryUrlBlockObjectName
|
|
166
|
+
}, temporaryInlineObjectNames = {
|
|
167
|
+
image: temporaryImageInlineObjectName,
|
|
168
|
+
url: temporaryUrlInlineObjectName
|
|
169
|
+
}, blockObjectNames = {
|
|
170
|
+
[temporaryImageBlockObjectName]: "image",
|
|
171
|
+
[temporaryUrlBlockObjectName]: "url"
|
|
172
|
+
}, inlineObjectNames = {
|
|
173
|
+
[temporaryImageInlineObjectName]: "image",
|
|
174
|
+
[temporaryUrlInlineObjectName]: "url"
|
|
157
175
|
}, defaultObjectTitles = {
|
|
158
176
|
image: "Image",
|
|
159
177
|
url: "URL"
|
|
@@ -164,7 +182,7 @@ function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
|
164
182
|
type: "object",
|
|
165
183
|
// Very naive way to work around `SanitySchema.compile` adding default
|
|
166
184
|
// fields to objects with certain names.
|
|
167
|
-
name:
|
|
185
|
+
name: temporaryBlockObjectNames[blockObject.name] ?? blockObject.name,
|
|
168
186
|
title: blockObject.title === void 0 ? (
|
|
169
187
|
// This avoids the default title which is a title case of the object name
|
|
170
188
|
defaultObjectTitles[blockObject.name]
|
|
@@ -180,7 +198,7 @@ function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
|
180
198
|
type: "object",
|
|
181
199
|
// Very naive way to work around `SanitySchema.compile` adding default
|
|
182
200
|
// fields to objects with certain names.
|
|
183
|
-
name:
|
|
201
|
+
name: temporaryInlineObjectNames[inlineObject.name] ?? inlineObject.name,
|
|
184
202
|
title: inlineObject.title === void 0 ? (
|
|
185
203
|
// This avoids the default title which is a title case of the object name
|
|
186
204
|
defaultObjectTitles[inlineObject.name]
|
|
@@ -231,20 +249,44 @@ function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
|
231
249
|
}).get("portable-text"), pteSchema = createPortableTextMemberSchemaTypes(schema$1);
|
|
232
250
|
return {
|
|
233
251
|
...pteSchema,
|
|
252
|
+
portableText: {
|
|
253
|
+
...pteSchema.portableText,
|
|
254
|
+
of: pteSchema.portableText.of.map((schemaType) => {
|
|
255
|
+
if (!types.isObjectSchemaType(schemaType))
|
|
256
|
+
return schemaType;
|
|
257
|
+
const nameMapping = blockObjectNames[schemaType.name];
|
|
258
|
+
return {
|
|
259
|
+
...schemaType,
|
|
260
|
+
name: nameMapping ?? schemaType.name,
|
|
261
|
+
fields: schemaType.fields.map((field) => field.name !== "children" || !types.isArraySchemaType(field.type) ? field : {
|
|
262
|
+
...field,
|
|
263
|
+
type: {
|
|
264
|
+
of: field.type.of.map((ofSchemaType) => {
|
|
265
|
+
const nameMapping2 = inlineObjectNames[ofSchemaType.name];
|
|
266
|
+
return nameMapping2 ? {
|
|
267
|
+
...ofSchemaType,
|
|
268
|
+
name: nameMapping2
|
|
269
|
+
} : ofSchemaType;
|
|
270
|
+
})
|
|
271
|
+
}
|
|
272
|
+
})
|
|
273
|
+
};
|
|
274
|
+
})
|
|
275
|
+
},
|
|
234
276
|
blockObjects: pteSchema.blockObjects.map(
|
|
235
|
-
(blockObject) =>
|
|
277
|
+
(blockObject) => blockObjectNames[blockObject.name] !== void 0 ? {
|
|
236
278
|
...blockObject,
|
|
237
|
-
name:
|
|
279
|
+
name: blockObjectNames[blockObject.name],
|
|
238
280
|
type: {
|
|
239
281
|
...blockObject.type,
|
|
240
|
-
name:
|
|
282
|
+
name: blockObjectNames[blockObject.name]
|
|
241
283
|
}
|
|
242
284
|
} : blockObject
|
|
243
285
|
),
|
|
244
286
|
inlineObjects: pteSchema.inlineObjects.map(
|
|
245
|
-
(inlineObject) =>
|
|
287
|
+
(inlineObject) => inlineObjectNames[inlineObject.name] !== void 0 ? {
|
|
246
288
|
...inlineObject,
|
|
247
|
-
name:
|
|
289
|
+
name: inlineObjectNames[inlineObject.name]
|
|
248
290
|
} : inlineObject
|
|
249
291
|
)
|
|
250
292
|
};
|
|
@@ -252,4 +294,5 @@ function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
|
252
294
|
exports.compileSchemaDefinitionToPortableTextMemberSchemaTypes = compileSchemaDefinitionToPortableTextMemberSchemaTypes;
|
|
253
295
|
exports.createPortableTextMemberSchemaTypes = createPortableTextMemberSchemaTypes;
|
|
254
296
|
exports.portableTextMemberSchemaTypesToSchema = portableTextMemberSchemaTypesToSchema;
|
|
297
|
+
exports.sanitySchemaToPortableTextSchema = sanitySchemaToPortableTextSchema;
|
|
255
298
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/portable-text-member-schema-types.ts","../src/portable-text-member-schema-types-to-schema.ts","../src/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 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 {defineField, defineType, type ObjectSchemaType} 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 temporaryImageName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlName = `tmp-${keyGenerator()}-url`\n\nconst temporaryObjectNames: Record<string, string> = {\n image: temporaryImageName,\n url: temporaryUrlName,\n}\n\nconst objectNames: Record<string, string> = {\n [temporaryImageName]: 'image',\n [temporaryUrlName]: '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: temporaryObjectNames[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: temporaryObjectNames[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 blockObjects: pteSchema.blockObjects.map((blockObject) =>\n objectNames[blockObject.name] !== undefined\n ? ({\n ...blockObject,\n name: objectNames[blockObject.name],\n type: {\n ...blockObject.type,\n name: objectNames[blockObject.name],\n },\n } as ObjectSchemaType)\n : blockObject,\n ),\n inlineObjects: pteSchema.inlineObjects.map((inlineObject) =>\n objectNames[inlineObject.name] !== undefined\n ? ({\n ...inlineObject,\n name: objectNames[inlineObject.name],\n } as ObjectSchemaType)\n : inlineObject,\n ),\n } satisfies PortableTextMemberSchemaTypes\n}\n"],"names":["schema","getRandomValues","defineType","startCase","defineField","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;AAAA,IAAA;AAGJ,QAAM,WAAW,OAAO,KAAK,CAAC,eAAe,WAAW,SAAS,MAAM;AAGvE,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,0CAA0C;AAE5D,QAAM,oBAAqB,OAAO;AAAA,IAChC,CAAC,eAAe,WAAW,SAAS;AAAA,EAAA,KACjC,IACC,mBAAoB,iBAAiB,IAAI;AAAA,IAC7C,CAAC,UAAU,MAAM,SAAS,UAAU;AAAA,EAAA,KACjC,CAAA;AACL,SAAO;AAAA,IACL,QAAQ,qBAAqB,SAAS;AAAA,IACtC,YAAY,yBAAyB,QAAQ;AAAA,IAC7C,OAAO,wBAAwB,SAAS;AAAA,IACxC,OAAO;AAAA,IACP,MAAM;AAAA,IACN,cAAc;AAAA,IACd,eAAe;AAAA,IACf,cAAc;AAAA,IACd,aAAc,SAA4B;AAAA,EAAA;AAE9C;AAEA,SAAS,qBAAqB,WAA6B;AACzD,QAAM,aAAa,UAAU,QAAQ;AAAA,IACnC,CAAC,YAAY,QAAQ,SAAS;AAAA,E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;AAAA,UACb,OAAO,MAAM,SAASA,mBAAAA,QAAU,MAAM,IAAI;AAAA,QAAA,EAC1C,KAAK,CAAA;AAAA,MAAC;AAAA,IACZ;AAAA,EACF,CACD,GAEKH,WAASK,OAAAA,OAAa,QAAQ;AAAA,IAClC,OAAO,CAAC,oBAAoB,GAAG,cAAc,GAAG,aAAa;AAAA,EAAA,CAC9D,EAAE,IAAI,eAAe,GAEhB,YAAY,oCAAoCL,QAAM;AAE5D,SAAO;AAAA,IACL,GAAG;AAAA,IACH,cAAc,UAAU,aAAa;AAAA,MAAI,CAAC,gBACxC,YAAY,YAAY,IAAI,MAAM,SAC7B;AAAA,QACC,GAAG;AAAA,QACH,MAAM,YAAY,YAAY,IAAI;AAAA,QAClC,MAAM;AAAA,UACJ,GAAG,YAAY;AAAA,UACf,MAAM,YAAY,YAAY,IAAI;AAAA,QAAA;AAAA,MACpC,IAEF;AAAA,IAAA;AAAA,IAEN,eAAe,UAAU,cAAc;AAAA,MAAI,CAAC,iBAC1C,YAAY,aAAa,IAAI,MAAM,SAC9B;AAAA,QACC,GAAG;AAAA,QACH,MAAM,YAAY,aAAa,IAAI;AAAA,MAAA,IAErC;AAAA,IAAA;AAAA,EACN;AAEJ;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/portable-text-member-schema-types.ts","../src/portable-text-member-schema-types-to-schema.ts","../src/sanity-schema-to-portable-text-schema.ts","../src/key-generator.ts","../src/schema-definition-to-portable-text-member-schema-types.ts"],"sourcesContent":["import type {\n ArraySchemaType,\n BlockDecoratorDefinition,\n BlockListDefinition,\n BlockSchemaType,\n BlockStyleDefinition,\n ObjectSchemaType,\n PortableTextBlock,\n SchemaType,\n SpanSchemaType,\n} from '@sanity/types'\n\n/**\n * @public\n * Sanity-specific schema types for Portable Text.\n */\nexport type PortableTextMemberSchemaTypes = {\n annotations: (ObjectSchemaType & {i18nTitleKey?: string})[]\n block: ObjectSchemaType\n blockObjects: ObjectSchemaType[]\n decorators: BlockDecoratorDefinition[]\n inlineObjects: ObjectSchemaType[]\n portableText: ArraySchemaType<PortableTextBlock>\n span: ObjectSchemaType\n styles: BlockStyleDefinition[]\n lists: BlockListDefinition[]\n}\n\n/**\n * @public\n * Create Sanity-specific schema types for Portable Text from a Sanity array\n * schema type.\n */\nexport function createPortableTextMemberSchemaTypes(\n portableTextType: ArraySchemaType<PortableTextBlock>,\n): PortableTextMemberSchemaTypes {\n if (!portableTextType) {\n throw new Error(\"Parameter 'portabletextType' missing (required)\")\n }\n const blockType = portableTextType.of?.find(findBlockType) as\n | BlockSchemaType\n | undefined\n if (!blockType) {\n throw new Error('Block type is not defined in this schema (required)')\n }\n const childrenField = blockType.fields?.find(\n (field) => field.name === 'children',\n ) as {type: ArraySchemaType} | undefined\n if (!childrenField) {\n throw new Error('Children field for block type found in schema (required)')\n }\n const ofType = childrenField.type.of\n if (!ofType) {\n throw new Error(\n 'Valid types for block children not found in schema (required)',\n )\n }\n const spanType = ofType.find((memberType) => memberType.name === 'span') as\n | ObjectSchemaType\n | undefined\n if (!spanType) {\n throw new Error('Span type not found in schema (required)')\n }\n const inlineObjectTypes = (ofType.filter(\n (memberType) => memberType.name !== 'span',\n ) || []) as ObjectSchemaType[]\n const blockObjectTypes = (portableTextType.of?.filter(\n (field) => field.name !== blockType.name,\n ) || []) as ObjectSchemaType[]\n return {\n styles: resolveEnabledStyles(blockType),\n decorators: resolveEnabledDecorators(spanType),\n lists: resolveEnabledListItems(blockType),\n block: blockType,\n span: spanType,\n portableText: portableTextType,\n inlineObjects: inlineObjectTypes,\n blockObjects: blockObjectTypes,\n annotations: (spanType as SpanSchemaType).annotations,\n }\n}\n\nfunction resolveEnabledStyles(blockType: ObjectSchemaType) {\n const styleField = blockType.fields?.find(\n (btField) => btField.name === 'style',\n )\n if (!styleField) {\n throw new Error(\n \"A field with name 'style' is not defined in the block type (required).\",\n )\n }\n const textStyles =\n styleField.type.options?.list &&\n styleField.type.options.list?.filter(\n (style: {value: string}) => style.value,\n )\n if (!textStyles || textStyles.length === 0) {\n throw new Error(\n 'The style fields need at least one style ' +\n \"defined. I.e: {title: 'Normal', value: 'normal'}.\",\n )\n }\n return textStyles\n}\n\nfunction resolveEnabledDecorators(spanType: ObjectSchemaType) {\n return (spanType as any).decorators\n}\n\nfunction resolveEnabledListItems(blockType: ObjectSchemaType) {\n const listField = blockType.fields?.find(\n (btField) => btField.name === 'listItem',\n )\n if (!listField) {\n throw new Error(\n \"A field with name 'listItem' is not defined in the block type (required).\",\n )\n }\n const listItems =\n listField.type.options?.list &&\n listField.type.options.list.filter((list: {value: string}) => list.value)\n if (!listItems) {\n throw new Error('The list field need at least to be an empty array')\n }\n return listItems\n}\n\nfunction findBlockType(type: SchemaType): BlockSchemaType | null {\n if (type.type) {\n return findBlockType(type.type)\n }\n\n if (type.name === 'block') {\n return type as BlockSchemaType\n }\n\n return null\n}\n","import type {Schema} from '@portabletext/schema'\nimport type {PortableTextMemberSchemaTypes} from './portable-text-member-schema-types'\n\n/**\n * @public\n * Convert Sanity-specific schema types for Portable Text to a first-class\n * Portable Text schema.\n */\nexport function portableTextMemberSchemaTypesToSchema(\n schema: PortableTextMemberSchemaTypes,\n): Schema {\n return {\n annotations: schema.annotations.map((annotation) => ({\n name: annotation.name,\n fields: annotation.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: annotation.title,\n })),\n block: {\n name: schema.block.name,\n },\n blockObjects: schema.blockObjects.map((blockObject) => ({\n name: blockObject.name,\n fields: blockObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: blockObject.title,\n })),\n decorators: schema.decorators.map((decorator) => ({\n name: decorator.value,\n title: decorator.title,\n value: decorator.value,\n })),\n inlineObjects: schema.inlineObjects.map((inlineObject) => ({\n name: inlineObject.name,\n fields: inlineObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: inlineObject.title,\n })),\n span: {\n name: schema.span.name,\n },\n styles: schema.styles.map((style) => ({\n name: style.value,\n title: style.title,\n value: style.value,\n })),\n lists: schema.lists.map((list) => ({\n name: list.value,\n title: list.title,\n value: list.value,\n })),\n }\n}\n","import type {Schema} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport type {ArrayDefinition, ArraySchemaType} from '@sanity/types'\nimport {createPortableTextMemberSchemaTypes} from './portable-text-member-schema-types'\nimport {portableTextMemberSchemaTypesToSchema} from './portable-text-member-schema-types-to-schema'\n\n/**\n * @public\n * Compile a Sanity schema to a Portable Text `Schema`.\n *\n * A Portable Text `Schema` is compatible with a Portable Text\n * `SchemaDefinition` and can be used as configuration for the Portable Text\n * Editor.\n *\n * @example\n * ```tsx\n * const schema = sanitySchemaToPortableTextSchema(sanitySchema)\n *\n * return (\n * <EditorProvider\n * initialConfig={{\n * // ...\n * schemaDefinition: schema,\n * }}\n * >\n * // ...\n * </EditorProvider>\n * ```\n */\nexport function sanitySchemaToPortableTextSchema(\n sanitySchema: ArraySchemaType<unknown> | ArrayDefinition,\n): Schema {\n const portableTextMemberSchemaTypes = createPortableTextMemberSchemaTypes(\n sanitySchema.hasOwnProperty('jsonType')\n ? sanitySchema\n : compileType(sanitySchema),\n )\n\n return portableTextMemberSchemaTypesToSchema(portableTextMemberSchemaTypes)\n}\n\nfunction compileType(rawType: any) {\n return SanitySchema.compile({\n name: 'blockTypeSchema',\n types: [rawType],\n }).get(rawType.name)\n}\n","import getRandomValues from 'get-random-values-esm'\n\nexport const keyGenerator = (): string => randomKey(12)\n\nconst getByteHexTable = (() => {\n let table: any[]\n return () => {\n if (table) {\n return table\n }\n\n table = []\n for (let i = 0; i < 256; ++i) {\n table[i] = (i + 0x100).toString(16).slice(1)\n }\n return table\n }\n})()\n\n// WHATWG crypto RNG - https://w3c.github.io/webcrypto/Overview.html\nfunction whatwgRNG(length = 16) {\n const rnds8 = new Uint8Array(length)\n getRandomValues(rnds8)\n return rnds8\n}\n\nfunction randomKey(length?: number): string {\n const table = getByteHexTable()\n return whatwgRNG(length)\n .reduce((str, n) => str + table[n], '')\n .slice(0, length)\n}\n","import type {SchemaDefinition} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport {\n defineField,\n defineType,\n isArraySchemaType,\n isObjectSchemaType,\n type ObjectField,\n type ObjectSchemaType,\n} from '@sanity/types'\nimport startCase from 'lodash.startcase'\nimport {keyGenerator} from './key-generator'\nimport {\n createPortableTextMemberSchemaTypes,\n type PortableTextMemberSchemaTypes,\n} from './portable-text-member-schema-types'\n\nconst temporaryImageBlockObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlBlockObjectName = `tmp-${keyGenerator()}-url`\nconst temporaryImageInlineObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlInlineObjectName = `tmp-${keyGenerator()}-url`\n\nconst temporaryBlockObjectNames: Record<string, string> = {\n image: temporaryImageBlockObjectName,\n url: temporaryUrlBlockObjectName,\n}\n\nconst temporaryInlineObjectNames: Record<string, string> = {\n image: temporaryImageInlineObjectName,\n url: temporaryUrlInlineObjectName,\n}\n\nconst blockObjectNames: Record<string, string> = {\n [temporaryImageBlockObjectName]: 'image',\n [temporaryUrlBlockObjectName]: 'url',\n}\n\nconst inlineObjectNames: Record<string, string> = {\n [temporaryImageInlineObjectName]: 'image',\n [temporaryUrlInlineObjectName]: 'url',\n}\n\nconst defaultObjectTitles: Record<string, string> = {\n image: 'Image',\n url: 'URL',\n}\n\n/**\n * @public\n * Compile a Portable Text schema definition to Sanity-specific schema types for\n * Portable Text.\n */\nexport function compileSchemaDefinitionToPortableTextMemberSchemaTypes(\n definition?: SchemaDefinition,\n): PortableTextMemberSchemaTypes {\n const blockObjects =\n definition?.blockObjects?.map((blockObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\n name: temporaryBlockObjectNames[blockObject.name] ?? blockObject.name,\n title:\n blockObject.title === undefined\n ? // This avoids the default title which is a title case of the object name\n defaultObjectTitles[blockObject.name]\n : blockObject.title,\n fields:\n blockObject.fields?.map((field) => ({\n name: field.name,\n type: field.type,\n title: field.title ?? startCase(field.name),\n })) ?? [],\n }),\n ) ?? []\n\n const inlineObjects =\n definition?.inlineObjects?.map((inlineObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\n name:\n temporaryInlineObjectNames[inlineObject.name] ?? inlineObject.name,\n\n title:\n inlineObject.title === undefined\n ? // This avoids the default title which is a title case of the object name\n defaultObjectTitles[inlineObject.name]\n : inlineObject.title,\n fields:\n inlineObject.fields?.map((field) => ({\n name: field.name,\n type: field.type,\n title: field.title ?? startCase(field.name),\n })) ?? [],\n }),\n ) ?? []\n\n const portableTextSchema = defineField({\n type: 'array',\n name: 'portable-text',\n of: [\n ...blockObjects.map((blockObject) => ({type: blockObject.name})),\n {\n type: 'block',\n name: 'block',\n of: inlineObjects.map((inlineObject) => ({type: inlineObject.name})),\n marks: {\n decorators:\n definition?.decorators?.map((decorator) => ({\n title: decorator.title ?? startCase(decorator.name),\n value: decorator.name,\n })) ?? [],\n annotations:\n definition?.annotations?.map((annotation) => ({\n name: annotation.name,\n type: 'object',\n title: annotation.title,\n fields:\n annotation.fields?.map((field) => ({\n name: field.name,\n title: field.title ?? startCase(field.name),\n type: field.type,\n })) ?? [],\n })) ?? [],\n },\n lists:\n definition?.lists?.map((list) => ({\n value: list.name,\n title: list.title ?? startCase(list.name),\n })) ?? [],\n styles:\n definition?.styles?.map((style) => ({\n value: style.name,\n title: style.title ?? startCase(style.name),\n })) ?? [],\n },\n ],\n })\n\n const schema = SanitySchema.compile({\n types: [portableTextSchema, ...blockObjects, ...inlineObjects],\n }).get('portable-text')\n\n const pteSchema = createPortableTextMemberSchemaTypes(schema)\n\n return {\n ...pteSchema,\n portableText: {\n ...pteSchema.portableText,\n of: pteSchema.portableText.of.map((schemaType) => {\n if (!isObjectSchemaType(schemaType)) {\n return schemaType\n }\n\n const nameMapping = blockObjectNames[schemaType.name]\n\n return {\n ...schemaType,\n name: nameMapping ?? schemaType.name,\n fields: schemaType.fields.map((field) => {\n if (field.name !== 'children' || !isArraySchemaType(field.type)) {\n return field\n }\n\n return {\n ...field,\n type: {\n of: field.type.of.map((ofSchemaType) => {\n const nameMapping = inlineObjectNames[ofSchemaType.name]\n\n if (!nameMapping) {\n return ofSchemaType\n }\n\n return {\n ...ofSchemaType,\n name: nameMapping,\n }\n }),\n },\n } as ObjectField\n }),\n }\n }),\n },\n blockObjects: pteSchema.blockObjects.map((blockObject) =>\n blockObjectNames[blockObject.name] !== undefined\n ? ({\n ...blockObject,\n name: blockObjectNames[blockObject.name],\n type: {\n ...blockObject.type,\n name: blockObjectNames[blockObject.name],\n },\n } as ObjectSchemaType)\n : blockObject,\n ),\n inlineObjects: pteSchema.inlineObjects.map((inlineObject) =>\n inlineObjectNames[inlineObject.name] !== undefined\n ? ({\n ...inlineObject,\n name: inlineObjectNames[inlineObject.name],\n } as ObjectSchemaType)\n : inlineObject,\n ),\n } satisfies PortableTextMemberSchemaTypes\n}\n"],"names":["schema","SanitySchema","getRandomValues","defineType","startCase","defineField","isObjectSchemaType","isArraySchemaType","nameMapping"],"mappings":";;;;;;;AAiCO,SAAS,oCACd,kBAC+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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AAAA,EACN;AAEJ;;;;;"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ArraySchemaType, BlockDecoratorDefinition, BlockListDefinition, BlockStyleDefinition, ObjectSchemaType, PortableTextBlock } from "@sanity/types";
|
|
1
|
+
import { ArrayDefinition, ArraySchemaType, BlockDecoratorDefinition, BlockListDefinition, BlockStyleDefinition, ObjectSchemaType, PortableTextBlock } from "@sanity/types";
|
|
2
2
|
import { Schema, SchemaDefinition } from "@portabletext/schema";
|
|
3
3
|
/**
|
|
4
4
|
* @public
|
|
@@ -29,10 +29,34 @@ declare function createPortableTextMemberSchemaTypes(portableTextType: ArraySche
|
|
|
29
29
|
* Portable Text schema.
|
|
30
30
|
*/
|
|
31
31
|
declare function portableTextMemberSchemaTypesToSchema(schema: PortableTextMemberSchemaTypes): Schema;
|
|
32
|
+
/**
|
|
33
|
+
* @public
|
|
34
|
+
* Compile a Sanity schema to a Portable Text `Schema`.
|
|
35
|
+
*
|
|
36
|
+
* A Portable Text `Schema` is compatible with a Portable Text
|
|
37
|
+
* `SchemaDefinition` and can be used as configuration for the Portable Text
|
|
38
|
+
* Editor.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```tsx
|
|
42
|
+
* const schema = sanitySchemaToPortableTextSchema(sanitySchema)
|
|
43
|
+
*
|
|
44
|
+
* return (
|
|
45
|
+
* <EditorProvider
|
|
46
|
+
* initialConfig={{
|
|
47
|
+
* // ...
|
|
48
|
+
* schemaDefinition: schema,
|
|
49
|
+
* }}
|
|
50
|
+
* >
|
|
51
|
+
* // ...
|
|
52
|
+
* </EditorProvider>
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare function sanitySchemaToPortableTextSchema(sanitySchema: ArraySchemaType<unknown> | ArrayDefinition): Schema;
|
|
32
56
|
/**
|
|
33
57
|
* @public
|
|
34
58
|
* Compile a Portable Text schema definition to Sanity-specific schema types for
|
|
35
59
|
* Portable Text.
|
|
36
60
|
*/
|
|
37
61
|
declare function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition?: SchemaDefinition): PortableTextMemberSchemaTypes;
|
|
38
|
-
export { type PortableTextMemberSchemaTypes, compileSchemaDefinitionToPortableTextMemberSchemaTypes, createPortableTextMemberSchemaTypes, portableTextMemberSchemaTypesToSchema };
|
|
62
|
+
export { type PortableTextMemberSchemaTypes, compileSchemaDefinitionToPortableTextMemberSchemaTypes, createPortableTextMemberSchemaTypes, portableTextMemberSchemaTypesToSchema, sanitySchemaToPortableTextSchema };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ArraySchemaType, BlockDecoratorDefinition, BlockListDefinition, BlockStyleDefinition, ObjectSchemaType, PortableTextBlock } from "@sanity/types";
|
|
1
|
+
import { ArrayDefinition, ArraySchemaType, BlockDecoratorDefinition, BlockListDefinition, BlockStyleDefinition, ObjectSchemaType, PortableTextBlock } from "@sanity/types";
|
|
2
2
|
import { Schema, SchemaDefinition } from "@portabletext/schema";
|
|
3
3
|
/**
|
|
4
4
|
* @public
|
|
@@ -29,10 +29,34 @@ declare function createPortableTextMemberSchemaTypes(portableTextType: ArraySche
|
|
|
29
29
|
* Portable Text schema.
|
|
30
30
|
*/
|
|
31
31
|
declare function portableTextMemberSchemaTypesToSchema(schema: PortableTextMemberSchemaTypes): Schema;
|
|
32
|
+
/**
|
|
33
|
+
* @public
|
|
34
|
+
* Compile a Sanity schema to a Portable Text `Schema`.
|
|
35
|
+
*
|
|
36
|
+
* A Portable Text `Schema` is compatible with a Portable Text
|
|
37
|
+
* `SchemaDefinition` and can be used as configuration for the Portable Text
|
|
38
|
+
* Editor.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```tsx
|
|
42
|
+
* const schema = sanitySchemaToPortableTextSchema(sanitySchema)
|
|
43
|
+
*
|
|
44
|
+
* return (
|
|
45
|
+
* <EditorProvider
|
|
46
|
+
* initialConfig={{
|
|
47
|
+
* // ...
|
|
48
|
+
* schemaDefinition: schema,
|
|
49
|
+
* }}
|
|
50
|
+
* >
|
|
51
|
+
* // ...
|
|
52
|
+
* </EditorProvider>
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare function sanitySchemaToPortableTextSchema(sanitySchema: ArraySchemaType<unknown> | ArrayDefinition): Schema;
|
|
32
56
|
/**
|
|
33
57
|
* @public
|
|
34
58
|
* Compile a Portable Text schema definition to Sanity-specific schema types for
|
|
35
59
|
* Portable Text.
|
|
36
60
|
*/
|
|
37
61
|
declare function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition?: SchemaDefinition): PortableTextMemberSchemaTypes;
|
|
38
|
-
export { type PortableTextMemberSchemaTypes, compileSchemaDefinitionToPortableTextMemberSchemaTypes, createPortableTextMemberSchemaTypes, portableTextMemberSchemaTypesToSchema };
|
|
62
|
+
export { type PortableTextMemberSchemaTypes, compileSchemaDefinitionToPortableTextMemberSchemaTypes, createPortableTextMemberSchemaTypes, portableTextMemberSchemaTypesToSchema, sanitySchemaToPortableTextSchema };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Schema } from "@sanity/schema";
|
|
2
|
-
import { defineType, defineField } from "@sanity/types";
|
|
2
|
+
import { defineType, defineField, isObjectSchemaType, isArraySchemaType } from "@sanity/types";
|
|
3
3
|
import startCase from "lodash.startcase";
|
|
4
4
|
import getRandomValues from "get-random-values-esm";
|
|
5
5
|
function createPortableTextMemberSchemaTypes(portableTextType) {
|
|
@@ -126,6 +126,18 @@ function portableTextMemberSchemaTypesToSchema(schema) {
|
|
|
126
126
|
}))
|
|
127
127
|
};
|
|
128
128
|
}
|
|
129
|
+
function sanitySchemaToPortableTextSchema(sanitySchema) {
|
|
130
|
+
const portableTextMemberSchemaTypes = createPortableTextMemberSchemaTypes(
|
|
131
|
+
sanitySchema.hasOwnProperty("jsonType") ? sanitySchema : compileType(sanitySchema)
|
|
132
|
+
);
|
|
133
|
+
return portableTextMemberSchemaTypesToSchema(portableTextMemberSchemaTypes);
|
|
134
|
+
}
|
|
135
|
+
function compileType(rawType) {
|
|
136
|
+
return Schema.compile({
|
|
137
|
+
name: "blockTypeSchema",
|
|
138
|
+
types: [rawType]
|
|
139
|
+
}).get(rawType.name);
|
|
140
|
+
}
|
|
129
141
|
const keyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
|
|
130
142
|
let table;
|
|
131
143
|
return () => {
|
|
@@ -145,12 +157,18 @@ function randomKey(length) {
|
|
|
145
157
|
const table = getByteHexTable();
|
|
146
158
|
return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
|
|
147
159
|
}
|
|
148
|
-
const
|
|
149
|
-
image:
|
|
150
|
-
url:
|
|
151
|
-
},
|
|
152
|
-
|
|
153
|
-
|
|
160
|
+
const temporaryImageBlockObjectName = `tmp-${keyGenerator()}-image`, temporaryUrlBlockObjectName = `tmp-${keyGenerator()}-url`, temporaryImageInlineObjectName = `tmp-${keyGenerator()}-image`, temporaryUrlInlineObjectName = `tmp-${keyGenerator()}-url`, temporaryBlockObjectNames = {
|
|
161
|
+
image: temporaryImageBlockObjectName,
|
|
162
|
+
url: temporaryUrlBlockObjectName
|
|
163
|
+
}, temporaryInlineObjectNames = {
|
|
164
|
+
image: temporaryImageInlineObjectName,
|
|
165
|
+
url: temporaryUrlInlineObjectName
|
|
166
|
+
}, blockObjectNames = {
|
|
167
|
+
[temporaryImageBlockObjectName]: "image",
|
|
168
|
+
[temporaryUrlBlockObjectName]: "url"
|
|
169
|
+
}, inlineObjectNames = {
|
|
170
|
+
[temporaryImageInlineObjectName]: "image",
|
|
171
|
+
[temporaryUrlInlineObjectName]: "url"
|
|
154
172
|
}, defaultObjectTitles = {
|
|
155
173
|
image: "Image",
|
|
156
174
|
url: "URL"
|
|
@@ -161,7 +179,7 @@ function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
|
161
179
|
type: "object",
|
|
162
180
|
// Very naive way to work around `SanitySchema.compile` adding default
|
|
163
181
|
// fields to objects with certain names.
|
|
164
|
-
name:
|
|
182
|
+
name: temporaryBlockObjectNames[blockObject.name] ?? blockObject.name,
|
|
165
183
|
title: blockObject.title === void 0 ? (
|
|
166
184
|
// This avoids the default title which is a title case of the object name
|
|
167
185
|
defaultObjectTitles[blockObject.name]
|
|
@@ -177,7 +195,7 @@ function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
|
177
195
|
type: "object",
|
|
178
196
|
// Very naive way to work around `SanitySchema.compile` adding default
|
|
179
197
|
// fields to objects with certain names.
|
|
180
|
-
name:
|
|
198
|
+
name: temporaryInlineObjectNames[inlineObject.name] ?? inlineObject.name,
|
|
181
199
|
title: inlineObject.title === void 0 ? (
|
|
182
200
|
// This avoids the default title which is a title case of the object name
|
|
183
201
|
defaultObjectTitles[inlineObject.name]
|
|
@@ -228,20 +246,44 @@ function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
|
228
246
|
}).get("portable-text"), pteSchema = createPortableTextMemberSchemaTypes(schema);
|
|
229
247
|
return {
|
|
230
248
|
...pteSchema,
|
|
249
|
+
portableText: {
|
|
250
|
+
...pteSchema.portableText,
|
|
251
|
+
of: pteSchema.portableText.of.map((schemaType) => {
|
|
252
|
+
if (!isObjectSchemaType(schemaType))
|
|
253
|
+
return schemaType;
|
|
254
|
+
const nameMapping = blockObjectNames[schemaType.name];
|
|
255
|
+
return {
|
|
256
|
+
...schemaType,
|
|
257
|
+
name: nameMapping ?? schemaType.name,
|
|
258
|
+
fields: schemaType.fields.map((field) => field.name !== "children" || !isArraySchemaType(field.type) ? field : {
|
|
259
|
+
...field,
|
|
260
|
+
type: {
|
|
261
|
+
of: field.type.of.map((ofSchemaType) => {
|
|
262
|
+
const nameMapping2 = inlineObjectNames[ofSchemaType.name];
|
|
263
|
+
return nameMapping2 ? {
|
|
264
|
+
...ofSchemaType,
|
|
265
|
+
name: nameMapping2
|
|
266
|
+
} : ofSchemaType;
|
|
267
|
+
})
|
|
268
|
+
}
|
|
269
|
+
})
|
|
270
|
+
};
|
|
271
|
+
})
|
|
272
|
+
},
|
|
231
273
|
blockObjects: pteSchema.blockObjects.map(
|
|
232
|
-
(blockObject) =>
|
|
274
|
+
(blockObject) => blockObjectNames[blockObject.name] !== void 0 ? {
|
|
233
275
|
...blockObject,
|
|
234
|
-
name:
|
|
276
|
+
name: blockObjectNames[blockObject.name],
|
|
235
277
|
type: {
|
|
236
278
|
...blockObject.type,
|
|
237
|
-
name:
|
|
279
|
+
name: blockObjectNames[blockObject.name]
|
|
238
280
|
}
|
|
239
281
|
} : blockObject
|
|
240
282
|
),
|
|
241
283
|
inlineObjects: pteSchema.inlineObjects.map(
|
|
242
|
-
(inlineObject) =>
|
|
284
|
+
(inlineObject) => inlineObjectNames[inlineObject.name] !== void 0 ? {
|
|
243
285
|
...inlineObject,
|
|
244
|
-
name:
|
|
286
|
+
name: inlineObjectNames[inlineObject.name]
|
|
245
287
|
} : inlineObject
|
|
246
288
|
)
|
|
247
289
|
};
|
|
@@ -249,6 +291,7 @@ function compileSchemaDefinitionToPortableTextMemberSchemaTypes(definition) {
|
|
|
249
291
|
export {
|
|
250
292
|
compileSchemaDefinitionToPortableTextMemberSchemaTypes,
|
|
251
293
|
createPortableTextMemberSchemaTypes,
|
|
252
|
-
portableTextMemberSchemaTypesToSchema
|
|
294
|
+
portableTextMemberSchemaTypesToSchema,
|
|
295
|
+
sanitySchemaToPortableTextSchema
|
|
253
296
|
};
|
|
254
297
|
//# 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/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 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 {defineField, defineType, type ObjectSchemaType} 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 temporaryImageName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlName = `tmp-${keyGenerator()}-url`\n\nconst temporaryObjectNames: Record<string, string> = {\n image: temporaryImageName,\n url: temporaryUrlName,\n}\n\nconst objectNames: Record<string, string> = {\n [temporaryImageName]: 'image',\n [temporaryUrlName]: '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: temporaryObjectNames[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: temporaryObjectNames[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 blockObjects: pteSchema.blockObjects.map((blockObject) =>\n objectNames[blockObject.name] !== undefined\n ? ({\n ...blockObject,\n name: objectNames[blockObject.name],\n type: {\n ...blockObject.type,\n name: objectNames[blockObject.name],\n },\n } as ObjectSchemaType)\n : blockObject,\n ),\n inlineObjects: pteSchema.inlineObjects.map((inlineObject) =>\n objectNames[inlineObject.name] !== undefined\n ? ({\n ...inlineObject,\n name: objectNames[inlineObject.name],\n } as ObjectSchemaType)\n : inlineObject,\n ),\n } satisfies PortableTextMemberSchemaTypes\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;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,MA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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;AAE5D,SAAO;AAAA,IACL,GAAG;AAAA,IACH,cAAc,UAAU,aAAa;AAAA,MAAI,CAAC,gBACxC,YAAY,YAAY,IAAI,MAAM,SAC7B;AAAA,QACC,GAAG;AAAA,QACH,MAAM,YAAY,YAAY,IAAI;AAAA,QAClC,MAAM;AAAA,UACJ,GAAG,YAAY;AAAA,UACf,MAAM,YAAY,YAAY,IAAI;AAAA,QAAA;AAAA,MACpC,IAEF;AAAA,IAAA;AAAA,IAEN,eAAe,UAAU,cAAc;AAAA,MAAI,CAAC,iBAC1C,YAAY,aAAa,IAAI,MAAM,SAC9B;AAAA,QACC,GAAG;AAAA,QACH,MAAM,YAAY,aAAa,IAAI;AAAA,MAAA,IAErC;AAAA,IAAA;AAAA,EACN;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/portable-text-member-schema-types.ts","../src/portable-text-member-schema-types-to-schema.ts","../src/sanity-schema-to-portable-text-schema.ts","../src/key-generator.ts","../src/schema-definition-to-portable-text-member-schema-types.ts"],"sourcesContent":["import type {\n ArraySchemaType,\n BlockDecoratorDefinition,\n BlockListDefinition,\n BlockSchemaType,\n BlockStyleDefinition,\n ObjectSchemaType,\n PortableTextBlock,\n SchemaType,\n SpanSchemaType,\n} from '@sanity/types'\n\n/**\n * @public\n * Sanity-specific schema types for Portable Text.\n */\nexport type PortableTextMemberSchemaTypes = {\n annotations: (ObjectSchemaType & {i18nTitleKey?: string})[]\n block: ObjectSchemaType\n blockObjects: ObjectSchemaType[]\n decorators: BlockDecoratorDefinition[]\n inlineObjects: ObjectSchemaType[]\n portableText: ArraySchemaType<PortableTextBlock>\n span: ObjectSchemaType\n styles: BlockStyleDefinition[]\n lists: BlockListDefinition[]\n}\n\n/**\n * @public\n * Create Sanity-specific schema types for Portable Text from a Sanity array\n * schema type.\n */\nexport function createPortableTextMemberSchemaTypes(\n portableTextType: ArraySchemaType<PortableTextBlock>,\n): PortableTextMemberSchemaTypes {\n if (!portableTextType) {\n throw new Error(\"Parameter 'portabletextType' missing (required)\")\n }\n const blockType = portableTextType.of?.find(findBlockType) as\n | BlockSchemaType\n | undefined\n if (!blockType) {\n throw new Error('Block type is not defined in this schema (required)')\n }\n const childrenField = blockType.fields?.find(\n (field) => field.name === 'children',\n ) as {type: ArraySchemaType} | undefined\n if (!childrenField) {\n throw new Error('Children field for block type found in schema (required)')\n }\n const ofType = childrenField.type.of\n if (!ofType) {\n throw new Error(\n 'Valid types for block children not found in schema (required)',\n )\n }\n const spanType = ofType.find((memberType) => memberType.name === 'span') as\n | ObjectSchemaType\n | undefined\n if (!spanType) {\n throw new Error('Span type not found in schema (required)')\n }\n const inlineObjectTypes = (ofType.filter(\n (memberType) => memberType.name !== 'span',\n ) || []) as ObjectSchemaType[]\n const blockObjectTypes = (portableTextType.of?.filter(\n (field) => field.name !== blockType.name,\n ) || []) as ObjectSchemaType[]\n return {\n styles: resolveEnabledStyles(blockType),\n decorators: resolveEnabledDecorators(spanType),\n lists: resolveEnabledListItems(blockType),\n block: blockType,\n span: spanType,\n portableText: portableTextType,\n inlineObjects: inlineObjectTypes,\n blockObjects: blockObjectTypes,\n annotations: (spanType as SpanSchemaType).annotations,\n }\n}\n\nfunction resolveEnabledStyles(blockType: ObjectSchemaType) {\n const styleField = blockType.fields?.find(\n (btField) => btField.name === 'style',\n )\n if (!styleField) {\n throw new Error(\n \"A field with name 'style' is not defined in the block type (required).\",\n )\n }\n const textStyles =\n styleField.type.options?.list &&\n styleField.type.options.list?.filter(\n (style: {value: string}) => style.value,\n )\n if (!textStyles || textStyles.length === 0) {\n throw new Error(\n 'The style fields need at least one style ' +\n \"defined. I.e: {title: 'Normal', value: 'normal'}.\",\n )\n }\n return textStyles\n}\n\nfunction resolveEnabledDecorators(spanType: ObjectSchemaType) {\n return (spanType as any).decorators\n}\n\nfunction resolveEnabledListItems(blockType: ObjectSchemaType) {\n const listField = blockType.fields?.find(\n (btField) => btField.name === 'listItem',\n )\n if (!listField) {\n throw new Error(\n \"A field with name 'listItem' is not defined in the block type (required).\",\n )\n }\n const listItems =\n listField.type.options?.list &&\n listField.type.options.list.filter((list: {value: string}) => list.value)\n if (!listItems) {\n throw new Error('The list field need at least to be an empty array')\n }\n return listItems\n}\n\nfunction findBlockType(type: SchemaType): BlockSchemaType | null {\n if (type.type) {\n return findBlockType(type.type)\n }\n\n if (type.name === 'block') {\n return type as BlockSchemaType\n }\n\n return null\n}\n","import type {Schema} from '@portabletext/schema'\nimport type {PortableTextMemberSchemaTypes} from './portable-text-member-schema-types'\n\n/**\n * @public\n * Convert Sanity-specific schema types for Portable Text to a first-class\n * Portable Text schema.\n */\nexport function portableTextMemberSchemaTypesToSchema(\n schema: PortableTextMemberSchemaTypes,\n): Schema {\n return {\n annotations: schema.annotations.map((annotation) => ({\n name: annotation.name,\n fields: annotation.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: annotation.title,\n })),\n block: {\n name: schema.block.name,\n },\n blockObjects: schema.blockObjects.map((blockObject) => ({\n name: blockObject.name,\n fields: blockObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: blockObject.title,\n })),\n decorators: schema.decorators.map((decorator) => ({\n name: decorator.value,\n title: decorator.title,\n value: decorator.value,\n })),\n inlineObjects: schema.inlineObjects.map((inlineObject) => ({\n name: inlineObject.name,\n fields: inlineObject.fields.map((field) => ({\n name: field.name,\n type: field.type.jsonType,\n title: field.type.title,\n })),\n title: inlineObject.title,\n })),\n span: {\n name: schema.span.name,\n },\n styles: schema.styles.map((style) => ({\n name: style.value,\n title: style.title,\n value: style.value,\n })),\n lists: schema.lists.map((list) => ({\n name: list.value,\n title: list.title,\n value: list.value,\n })),\n }\n}\n","import type {Schema} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport type {ArrayDefinition, ArraySchemaType} from '@sanity/types'\nimport {createPortableTextMemberSchemaTypes} from './portable-text-member-schema-types'\nimport {portableTextMemberSchemaTypesToSchema} from './portable-text-member-schema-types-to-schema'\n\n/**\n * @public\n * Compile a Sanity schema to a Portable Text `Schema`.\n *\n * A Portable Text `Schema` is compatible with a Portable Text\n * `SchemaDefinition` and can be used as configuration for the Portable Text\n * Editor.\n *\n * @example\n * ```tsx\n * const schema = sanitySchemaToPortableTextSchema(sanitySchema)\n *\n * return (\n * <EditorProvider\n * initialConfig={{\n * // ...\n * schemaDefinition: schema,\n * }}\n * >\n * // ...\n * </EditorProvider>\n * ```\n */\nexport function sanitySchemaToPortableTextSchema(\n sanitySchema: ArraySchemaType<unknown> | ArrayDefinition,\n): Schema {\n const portableTextMemberSchemaTypes = createPortableTextMemberSchemaTypes(\n sanitySchema.hasOwnProperty('jsonType')\n ? sanitySchema\n : compileType(sanitySchema),\n )\n\n return portableTextMemberSchemaTypesToSchema(portableTextMemberSchemaTypes)\n}\n\nfunction compileType(rawType: any) {\n return SanitySchema.compile({\n name: 'blockTypeSchema',\n types: [rawType],\n }).get(rawType.name)\n}\n","import getRandomValues from 'get-random-values-esm'\n\nexport const keyGenerator = (): string => randomKey(12)\n\nconst getByteHexTable = (() => {\n let table: any[]\n return () => {\n if (table) {\n return table\n }\n\n table = []\n for (let i = 0; i < 256; ++i) {\n table[i] = (i + 0x100).toString(16).slice(1)\n }\n return table\n }\n})()\n\n// WHATWG crypto RNG - https://w3c.github.io/webcrypto/Overview.html\nfunction whatwgRNG(length = 16) {\n const rnds8 = new Uint8Array(length)\n getRandomValues(rnds8)\n return rnds8\n}\n\nfunction randomKey(length?: number): string {\n const table = getByteHexTable()\n return whatwgRNG(length)\n .reduce((str, n) => str + table[n], '')\n .slice(0, length)\n}\n","import type {SchemaDefinition} from '@portabletext/schema'\nimport {Schema as SanitySchema} from '@sanity/schema'\nimport {\n defineField,\n defineType,\n isArraySchemaType,\n isObjectSchemaType,\n type ObjectField,\n type ObjectSchemaType,\n} from '@sanity/types'\nimport startCase from 'lodash.startcase'\nimport {keyGenerator} from './key-generator'\nimport {\n createPortableTextMemberSchemaTypes,\n type PortableTextMemberSchemaTypes,\n} from './portable-text-member-schema-types'\n\nconst temporaryImageBlockObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlBlockObjectName = `tmp-${keyGenerator()}-url`\nconst temporaryImageInlineObjectName = `tmp-${keyGenerator()}-image`\nconst temporaryUrlInlineObjectName = `tmp-${keyGenerator()}-url`\n\nconst temporaryBlockObjectNames: Record<string, string> = {\n image: temporaryImageBlockObjectName,\n url: temporaryUrlBlockObjectName,\n}\n\nconst temporaryInlineObjectNames: Record<string, string> = {\n image: temporaryImageInlineObjectName,\n url: temporaryUrlInlineObjectName,\n}\n\nconst blockObjectNames: Record<string, string> = {\n [temporaryImageBlockObjectName]: 'image',\n [temporaryUrlBlockObjectName]: 'url',\n}\n\nconst inlineObjectNames: Record<string, string> = {\n [temporaryImageInlineObjectName]: 'image',\n [temporaryUrlInlineObjectName]: 'url',\n}\n\nconst defaultObjectTitles: Record<string, string> = {\n image: 'Image',\n url: 'URL',\n}\n\n/**\n * @public\n * Compile a Portable Text schema definition to Sanity-specific schema types for\n * Portable Text.\n */\nexport function compileSchemaDefinitionToPortableTextMemberSchemaTypes(\n definition?: SchemaDefinition,\n): PortableTextMemberSchemaTypes {\n const blockObjects =\n definition?.blockObjects?.map((blockObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\n name: temporaryBlockObjectNames[blockObject.name] ?? blockObject.name,\n title:\n blockObject.title === undefined\n ? // This avoids the default title which is a title case of the object name\n defaultObjectTitles[blockObject.name]\n : blockObject.title,\n fields:\n blockObject.fields?.map((field) => ({\n name: field.name,\n type: field.type,\n title: field.title ?? startCase(field.name),\n })) ?? [],\n }),\n ) ?? []\n\n const inlineObjects =\n definition?.inlineObjects?.map((inlineObject) =>\n defineType({\n type: 'object',\n // Very naive way to work around `SanitySchema.compile` adding default\n // fields to objects with certain names.\n name:\n temporaryInlineObjectNames[inlineObject.name] ?? inlineObject.name,\n\n title:\n inlineObject.title === undefined\n ? // This avoids the default title which is a title case of the object name\n defaultObjectTitles[inlineObject.name]\n : inlineObject.title,\n fields:\n inlineObject.fields?.map((field) => ({\n name: field.name,\n type: field.type,\n title: field.title ?? startCase(field.name),\n })) ?? [],\n }),\n ) ?? []\n\n const portableTextSchema = defineField({\n type: 'array',\n name: 'portable-text',\n of: [\n ...blockObjects.map((blockObject) => ({type: blockObject.name})),\n {\n type: 'block',\n name: 'block',\n of: inlineObjects.map((inlineObject) => ({type: inlineObject.name})),\n marks: {\n decorators:\n definition?.decorators?.map((decorator) => ({\n title: decorator.title ?? startCase(decorator.name),\n value: decorator.name,\n })) ?? [],\n annotations:\n definition?.annotations?.map((annotation) => ({\n name: annotation.name,\n type: 'object',\n title: annotation.title,\n fields:\n annotation.fields?.map((field) => ({\n name: field.name,\n title: field.title ?? startCase(field.name),\n type: field.type,\n })) ?? [],\n })) ?? [],\n },\n lists:\n definition?.lists?.map((list) => ({\n value: list.name,\n title: list.title ?? startCase(list.name),\n })) ?? [],\n styles:\n definition?.styles?.map((style) => ({\n value: style.name,\n title: style.title ?? startCase(style.name),\n })) ?? [],\n },\n ],\n })\n\n const schema = SanitySchema.compile({\n types: [portableTextSchema, ...blockObjects, ...inlineObjects],\n }).get('portable-text')\n\n const pteSchema = createPortableTextMemberSchemaTypes(schema)\n\n return {\n ...pteSchema,\n portableText: {\n ...pteSchema.portableText,\n of: pteSchema.portableText.of.map((schemaType) => {\n if (!isObjectSchemaType(schemaType)) {\n return schemaType\n }\n\n const nameMapping = blockObjectNames[schemaType.name]\n\n return {\n ...schemaType,\n name: nameMapping ?? schemaType.name,\n fields: schemaType.fields.map((field) => {\n if (field.name !== 'children' || !isArraySchemaType(field.type)) {\n return field\n }\n\n return {\n ...field,\n type: {\n of: field.type.of.map((ofSchemaType) => {\n const nameMapping = inlineObjectNames[ofSchemaType.name]\n\n if (!nameMapping) {\n return ofSchemaType\n }\n\n return {\n ...ofSchemaType,\n name: nameMapping,\n }\n }),\n },\n } as ObjectField\n }),\n }\n }),\n },\n blockObjects: pteSchema.blockObjects.map((blockObject) =>\n blockObjectNames[blockObject.name] !== undefined\n ? ({\n ...blockObject,\n name: blockObjectNames[blockObject.name],\n type: {\n ...blockObject.type,\n name: blockObjectNames[blockObject.name],\n },\n } as ObjectSchemaType)\n : blockObject,\n ),\n inlineObjects: pteSchema.inlineObjects.map((inlineObject) =>\n inlineObjectNames[inlineObject.name] !== undefined\n ? ({\n ...inlineObject,\n name: inlineObjectNames[inlineObject.name],\n } as ObjectSchemaType)\n : inlineObject,\n ),\n } satisfies PortableTextMemberSchemaTypes\n}\n"],"names":["SanitySchema","nameMapping"],"mappings":";;;;AAiCO,SAAS,oCACd,kBAC+B;AAC/B,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,iDAAiD;AAEnE,QAAM,YAAY,iBAAiB,IAAI,KAAK,aAAa;AAGzD,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,qDAAqD;AAEvE,QAAM,gBAAgB,UAAU,QAAQ;AAAA,IACtC,CAAC,UAAU,MAAM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a,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.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Convert a Sanity Schema to a Portable Text Schema",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -42,14 +42,14 @@
|
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@sanity/pkg-utils": "^7.11.1",
|
|
45
|
-
"@sanity/schema": "^4.
|
|
46
|
-
"@sanity/types": "^4.
|
|
45
|
+
"@sanity/schema": "^4.4.1",
|
|
46
|
+
"@sanity/types": "^4.4.1",
|
|
47
47
|
"@types/lodash.startcase": "^4.4.9",
|
|
48
48
|
"typescript": "^5.9.2"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"@sanity/schema": "^4.
|
|
52
|
-
"@sanity/types": "^4.
|
|
51
|
+
"@sanity/schema": "^4.4.1",
|
|
52
|
+
"@sanity/types": "^4.4.1"
|
|
53
53
|
},
|
|
54
54
|
"engines": {
|
|
55
55
|
"node": ">=20.19"
|
package/src/index.ts
CHANGED
|
@@ -3,4 +3,5 @@ export {
|
|
|
3
3
|
type PortableTextMemberSchemaTypes,
|
|
4
4
|
} from './portable-text-member-schema-types'
|
|
5
5
|
export {portableTextMemberSchemaTypesToSchema} from './portable-text-member-schema-types-to-schema'
|
|
6
|
+
export {sanitySchemaToPortableTextSchema} from './sanity-schema-to-portable-text-schema'
|
|
6
7
|
export {compileSchemaDefinitionToPortableTextMemberSchemaTypes} from './schema-definition-to-portable-text-member-schema-types'
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type {Schema} from '@portabletext/schema'
|
|
2
|
+
import {Schema as SanitySchema} from '@sanity/schema'
|
|
3
|
+
import type {ArrayDefinition, ArraySchemaType} from '@sanity/types'
|
|
4
|
+
import {createPortableTextMemberSchemaTypes} from './portable-text-member-schema-types'
|
|
5
|
+
import {portableTextMemberSchemaTypesToSchema} from './portable-text-member-schema-types-to-schema'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
* Compile a Sanity schema to a Portable Text `Schema`.
|
|
10
|
+
*
|
|
11
|
+
* A Portable Text `Schema` is compatible with a Portable Text
|
|
12
|
+
* `SchemaDefinition` and can be used as configuration for the Portable Text
|
|
13
|
+
* Editor.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* const schema = sanitySchemaToPortableTextSchema(sanitySchema)
|
|
18
|
+
*
|
|
19
|
+
* return (
|
|
20
|
+
* <EditorProvider
|
|
21
|
+
* initialConfig={{
|
|
22
|
+
* // ...
|
|
23
|
+
* schemaDefinition: schema,
|
|
24
|
+
* }}
|
|
25
|
+
* >
|
|
26
|
+
* // ...
|
|
27
|
+
* </EditorProvider>
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function sanitySchemaToPortableTextSchema(
|
|
31
|
+
sanitySchema: ArraySchemaType<unknown> | ArrayDefinition,
|
|
32
|
+
): Schema {
|
|
33
|
+
const portableTextMemberSchemaTypes = createPortableTextMemberSchemaTypes(
|
|
34
|
+
sanitySchema.hasOwnProperty('jsonType')
|
|
35
|
+
? sanitySchema
|
|
36
|
+
: compileType(sanitySchema),
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
return portableTextMemberSchemaTypesToSchema(portableTextMemberSchemaTypes)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function compileType(rawType: any) {
|
|
43
|
+
return SanitySchema.compile({
|
|
44
|
+
name: 'blockTypeSchema',
|
|
45
|
+
types: [rawType],
|
|
46
|
+
}).get(rawType.name)
|
|
47
|
+
}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import type {SchemaDefinition} from '@portabletext/schema'
|
|
2
2
|
import {Schema as SanitySchema} from '@sanity/schema'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
defineField,
|
|
5
|
+
defineType,
|
|
6
|
+
isArraySchemaType,
|
|
7
|
+
isObjectSchemaType,
|
|
8
|
+
type ObjectField,
|
|
9
|
+
type ObjectSchemaType,
|
|
10
|
+
} from '@sanity/types'
|
|
4
11
|
import startCase from 'lodash.startcase'
|
|
5
12
|
import {keyGenerator} from './key-generator'
|
|
6
13
|
import {
|
|
@@ -8,17 +15,29 @@ import {
|
|
|
8
15
|
type PortableTextMemberSchemaTypes,
|
|
9
16
|
} from './portable-text-member-schema-types'
|
|
10
17
|
|
|
11
|
-
const
|
|
12
|
-
const
|
|
18
|
+
const temporaryImageBlockObjectName = `tmp-${keyGenerator()}-image`
|
|
19
|
+
const temporaryUrlBlockObjectName = `tmp-${keyGenerator()}-url`
|
|
20
|
+
const temporaryImageInlineObjectName = `tmp-${keyGenerator()}-image`
|
|
21
|
+
const temporaryUrlInlineObjectName = `tmp-${keyGenerator()}-url`
|
|
22
|
+
|
|
23
|
+
const temporaryBlockObjectNames: Record<string, string> = {
|
|
24
|
+
image: temporaryImageBlockObjectName,
|
|
25
|
+
url: temporaryUrlBlockObjectName,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const temporaryInlineObjectNames: Record<string, string> = {
|
|
29
|
+
image: temporaryImageInlineObjectName,
|
|
30
|
+
url: temporaryUrlInlineObjectName,
|
|
31
|
+
}
|
|
13
32
|
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
33
|
+
const blockObjectNames: Record<string, string> = {
|
|
34
|
+
[temporaryImageBlockObjectName]: 'image',
|
|
35
|
+
[temporaryUrlBlockObjectName]: 'url',
|
|
17
36
|
}
|
|
18
37
|
|
|
19
|
-
const
|
|
20
|
-
[
|
|
21
|
-
[
|
|
38
|
+
const inlineObjectNames: Record<string, string> = {
|
|
39
|
+
[temporaryImageInlineObjectName]: 'image',
|
|
40
|
+
[temporaryUrlInlineObjectName]: 'url',
|
|
22
41
|
}
|
|
23
42
|
|
|
24
43
|
const defaultObjectTitles: Record<string, string> = {
|
|
@@ -40,7 +59,7 @@ export function compileSchemaDefinitionToPortableTextMemberSchemaTypes(
|
|
|
40
59
|
type: 'object',
|
|
41
60
|
// Very naive way to work around `SanitySchema.compile` adding default
|
|
42
61
|
// fields to objects with certain names.
|
|
43
|
-
name:
|
|
62
|
+
name: temporaryBlockObjectNames[blockObject.name] ?? blockObject.name,
|
|
44
63
|
title:
|
|
45
64
|
blockObject.title === undefined
|
|
46
65
|
? // This avoids the default title which is a title case of the object name
|
|
@@ -61,7 +80,8 @@ export function compileSchemaDefinitionToPortableTextMemberSchemaTypes(
|
|
|
61
80
|
type: 'object',
|
|
62
81
|
// Very naive way to work around `SanitySchema.compile` adding default
|
|
63
82
|
// fields to objects with certain names.
|
|
64
|
-
name:
|
|
83
|
+
name:
|
|
84
|
+
temporaryInlineObjectNames[inlineObject.name] ?? inlineObject.name,
|
|
65
85
|
|
|
66
86
|
title:
|
|
67
87
|
inlineObject.title === undefined
|
|
@@ -127,23 +147,61 @@ export function compileSchemaDefinitionToPortableTextMemberSchemaTypes(
|
|
|
127
147
|
|
|
128
148
|
return {
|
|
129
149
|
...pteSchema,
|
|
150
|
+
portableText: {
|
|
151
|
+
...pteSchema.portableText,
|
|
152
|
+
of: pteSchema.portableText.of.map((schemaType) => {
|
|
153
|
+
if (!isObjectSchemaType(schemaType)) {
|
|
154
|
+
return schemaType
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const nameMapping = blockObjectNames[schemaType.name]
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
...schemaType,
|
|
161
|
+
name: nameMapping ?? schemaType.name,
|
|
162
|
+
fields: schemaType.fields.map((field) => {
|
|
163
|
+
if (field.name !== 'children' || !isArraySchemaType(field.type)) {
|
|
164
|
+
return field
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
...field,
|
|
169
|
+
type: {
|
|
170
|
+
of: field.type.of.map((ofSchemaType) => {
|
|
171
|
+
const nameMapping = inlineObjectNames[ofSchemaType.name]
|
|
172
|
+
|
|
173
|
+
if (!nameMapping) {
|
|
174
|
+
return ofSchemaType
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
...ofSchemaType,
|
|
179
|
+
name: nameMapping,
|
|
180
|
+
}
|
|
181
|
+
}),
|
|
182
|
+
},
|
|
183
|
+
} as ObjectField
|
|
184
|
+
}),
|
|
185
|
+
}
|
|
186
|
+
}),
|
|
187
|
+
},
|
|
130
188
|
blockObjects: pteSchema.blockObjects.map((blockObject) =>
|
|
131
|
-
|
|
189
|
+
blockObjectNames[blockObject.name] !== undefined
|
|
132
190
|
? ({
|
|
133
191
|
...blockObject,
|
|
134
|
-
name:
|
|
192
|
+
name: blockObjectNames[blockObject.name],
|
|
135
193
|
type: {
|
|
136
194
|
...blockObject.type,
|
|
137
|
-
name:
|
|
195
|
+
name: blockObjectNames[blockObject.name],
|
|
138
196
|
},
|
|
139
197
|
} as ObjectSchemaType)
|
|
140
198
|
: blockObject,
|
|
141
199
|
),
|
|
142
200
|
inlineObjects: pteSchema.inlineObjects.map((inlineObject) =>
|
|
143
|
-
|
|
201
|
+
inlineObjectNames[inlineObject.name] !== undefined
|
|
144
202
|
? ({
|
|
145
203
|
...inlineObject,
|
|
146
|
-
name:
|
|
204
|
+
name: inlineObjectNames[inlineObject.name],
|
|
147
205
|
} as ObjectSchemaType)
|
|
148
206
|
: inlineObject,
|
|
149
207
|
),
|