@portabletext/schema 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 - 2024 Sanity.io
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # `@portabletext/schema`
2
+
3
+ A TypeScript library for defining and compiling Portable Text schemas with full type safety and editor support.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @portabletext/schema
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Define a Schema
14
+
15
+ ```ts
16
+ import {defineSchema} from '@portabletext/schema'
17
+
18
+ const schemaDefinition = defineSchema({
19
+ styles: [{name: 'normal'}, {name: 'h1'}, {name: 'h2'}, {name: 'blockquote'}],
20
+ decorators: [{name: 'strong'}, {name: 'em'}],
21
+ annotations: [{name: 'link'}],
22
+ lists: [{name: 'bullet'}, {name: 'numbered'}],
23
+ })
24
+ ```
25
+
26
+ ### Compile Schema
27
+
28
+ ```ts
29
+ import {compileSchema} from '@portabletext/schema'
30
+
31
+ const schema = compileSchema(schemaDefinition)
32
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: !0 });
3
+ function compileSchema(definition) {
4
+ const styles = (definition.styles ?? []).map((style) => ({
5
+ ...style,
6
+ value: style.name
7
+ }));
8
+ return {
9
+ block: {
10
+ name: definition.block?.name ?? "block"
11
+ },
12
+ span: {
13
+ name: "span"
14
+ },
15
+ styles: styles.some((style) => style.value === "normal") ? styles : [{ value: "normal", name: "normal", title: "Normal" }, ...styles],
16
+ lists: (definition.lists ?? []).map((list) => ({
17
+ ...list,
18
+ value: list.name
19
+ })),
20
+ decorators: (definition.decorators ?? []).map((decorator) => ({
21
+ ...decorator,
22
+ value: decorator.name
23
+ })),
24
+ annotations: (definition.annotations ?? []).map((annotation) => ({
25
+ ...annotation,
26
+ fields: annotation.fields ?? []
27
+ })),
28
+ blockObjects: (definition.blockObjects ?? []).map((blockObject) => ({
29
+ ...blockObject,
30
+ fields: blockObject.fields ?? []
31
+ })),
32
+ inlineObjects: (definition.inlineObjects ?? []).map((inlineObject) => ({
33
+ ...inlineObject,
34
+ fields: inlineObject.fields ?? []
35
+ }))
36
+ };
37
+ }
38
+ function defineSchema(definition) {
39
+ return definition;
40
+ }
41
+ exports.compileSchema = compileSchema;
42
+ exports.defineSchema = defineSchema;
43
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/compile-schema.ts","../src/define-schema.ts"],"sourcesContent":["import type {SchemaDefinition} from './define-schema'\nimport type {Schema} from './schema'\n\n/**\n * @public\n */\nexport function compileSchema(definition: SchemaDefinition): Schema {\n const styles = (definition.styles ?? []).map((style) => ({\n ...style,\n value: style.name,\n }))\n\n return {\n block: {\n name: definition.block?.name ?? 'block',\n },\n span: {\n name: 'span',\n },\n styles: !styles.some((style) => style.value === 'normal')\n ? [{value: 'normal', name: 'normal', title: 'Normal'}, ...styles]\n : styles,\n lists: (definition.lists ?? []).map((list) => ({\n ...list,\n value: list.name,\n })),\n decorators: (definition.decorators ?? []).map((decorator) => ({\n ...decorator,\n value: decorator.name,\n })),\n annotations: (definition.annotations ?? []).map((annotation) => ({\n ...annotation,\n fields: annotation.fields ?? [],\n })),\n blockObjects: (definition.blockObjects ?? []).map((blockObject) => ({\n ...blockObject,\n fields: blockObject.fields ?? [],\n })),\n inlineObjects: (definition.inlineObjects ?? []).map((inlineObject) => ({\n ...inlineObject,\n fields: inlineObject.fields ?? [],\n })),\n }\n}\n","import type {BaseDefinition, FieldDefinition} from './schema'\n\n/**\n * @public\n */\nexport type SchemaDefinition = {\n block?: {\n name: string\n }\n styles?: ReadonlyArray<StyleDefinition>\n lists?: ReadonlyArray<ListDefinition>\n decorators?: ReadonlyArray<DecoratorDefinition>\n annotations?: ReadonlyArray<AnnotationDefinition>\n blockObjects?: ReadonlyArray<BlockObjectDefinition>\n inlineObjects?: ReadonlyArray<InlineObjectDefinition>\n}\n\n/**\n * @public\n * A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.\n * @example\n * ```ts\n * import { defineSchema } from '@portabletext/editor'\n *\n * const schemaDefinition = defineSchema({\n * decorators: [{name: 'strong'}, {name: 'em'}, {name: 'underline'}],\n * annotations: [{name: 'link'}],\n * styles: [\n * {name: 'normal'},\n * {name: 'h1'},\n * {name: 'h2'},\n * {name: 'h3'},\n * {name: 'blockquote'},\n * ],\n * lists: [],\n * inlineObjects: [],\n * blockObjects: [],\n * }\n * ```\n */\nexport function defineSchema<const TSchemaDefinition extends SchemaDefinition>(\n definition: TSchemaDefinition,\n): TSchemaDefinition {\n return definition\n}\n\n/**\n * @public\n */\nexport type StyleDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition\n\n/**\n * @public\n */\nexport type ListDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition\n\n/**\n * @public\n */\nexport type DecoratorDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition\n\n/**\n * @public\n */\nexport type AnnotationDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition & {\n fields?: ReadonlyArray<FieldDefinition>\n}\n\n/**\n * @public\n */\nexport type BlockObjectDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition & {\n fields?: ReadonlyArray<FieldDefinition>\n}\n\n/**\n * @public\n */\nexport type InlineObjectDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition & {\n fields?: ReadonlyArray<FieldDefinition>\n}\n"],"names":[],"mappings":";;AAMO,SAAS,cAAc,YAAsC;AAClE,QAAM,UAAU,WAAW,UAAU,CAAA,GAAI,IAAI,CAAC,WAAW;AAAA,IACvD,GAAG;AAAA,IACH,OAAO,MAAM;AAAA,EAAA,EACb;AAEF,SAAO;AAAA,IACL,OAAO;AAAA,MACL,MAAM,WAAW,OAAO,QAAQ;AAAA,IAAA;AAAA,IAElC,MAAM;AAAA,MACJ,MAAM;AAAA,IAAA;AAAA,IAER,QAAS,OAAO,KAAK,CAAC,UAAU,MAAM,UAAU,QAAQ,IAEpD,SADA,CAAC,EAAC,OAAO,UAAU,MAAM,UAAU,OAAO,SAAA,GAAW,GAAG,MAAM;AAAA,IAElE,QAAQ,WAAW,SAAS,CAAA,GAAI,IAAI,CAAC,UAAU;AAAA,MAC7C,GAAG;AAAA,MACH,OAAO,KAAK;AAAA,IAAA,EACZ;AAAA,IACF,aAAa,WAAW,cAAc,CAAA,GAAI,IAAI,CAAC,eAAe;AAAA,MAC5D,GAAG;AAAA,MACH,OAAO,UAAU;AAAA,IAAA,EACjB;AAAA,IACF,cAAc,WAAW,eAAe,CAAA,GAAI,IAAI,CAAC,gBAAgB;AAAA,MAC/D,GAAG;AAAA,MACH,QAAQ,WAAW,UAAU,CAAA;AAAA,IAAC,EAC9B;AAAA,IACF,eAAe,WAAW,gBAAgB,CAAA,GAAI,IAAI,CAAC,iBAAiB;AAAA,MAClE,GAAG;AAAA,MACH,QAAQ,YAAY,UAAU,CAAA;AAAA,IAAC,EAC/B;AAAA,IACF,gBAAgB,WAAW,iBAAiB,CAAA,GAAI,IAAI,CAAC,kBAAkB;AAAA,MACrE,GAAG;AAAA,MACH,QAAQ,aAAa,UAAU,CAAA;AAAA,IAAC,EAChC;AAAA,EAAA;AAEN;ACHO,SAAS,aACd,YACmB;AACnB,SAAO;AACT;;;"}
@@ -0,0 +1,151 @@
1
+ /**
2
+ * @public
3
+ */
4
+ type Schema = {
5
+ block: {
6
+ name: string;
7
+ };
8
+ span: {
9
+ name: string;
10
+ };
11
+ styles: ReadonlyArray<StyleSchemaType>;
12
+ lists: ReadonlyArray<ListSchemaType>;
13
+ decorators: ReadonlyArray<DecoratorSchemaType>;
14
+ annotations: ReadonlyArray<AnnotationSchemaType>;
15
+ blockObjects: ReadonlyArray<BlockObjectSchemaType>;
16
+ inlineObjects: ReadonlyArray<InlineObjectSchemaType>;
17
+ };
18
+ /**
19
+ * @public
20
+ */
21
+ type StyleSchemaType = BaseDefinition & {
22
+ /**
23
+ * @deprecated
24
+ * Use `name` instead
25
+ */
26
+ value: string;
27
+ };
28
+ /**
29
+ * @public
30
+ */
31
+ type ListSchemaType = BaseDefinition & {
32
+ /**
33
+ * @deprecated
34
+ * Use `name` instead
35
+ */
36
+ value: string;
37
+ };
38
+ /**
39
+ * @public
40
+ */
41
+ type DecoratorSchemaType = BaseDefinition & {
42
+ /**
43
+ * @deprecated
44
+ * Use `name` instead
45
+ */
46
+ value: string;
47
+ };
48
+ /**
49
+ * @public
50
+ */
51
+ type AnnotationSchemaType = BaseDefinition & {
52
+ fields: ReadonlyArray<FieldDefinition>;
53
+ };
54
+ /**
55
+ * @public
56
+ */
57
+ type BlockObjectSchemaType = BaseDefinition & {
58
+ fields: ReadonlyArray<FieldDefinition>;
59
+ };
60
+ /**
61
+ * @public
62
+ */
63
+ type InlineObjectSchemaType = BaseDefinition & {
64
+ fields: ReadonlyArray<FieldDefinition>;
65
+ };
66
+ /**
67
+ * @public
68
+ */
69
+ type FieldDefinition = BaseDefinition & {
70
+ type: 'string' | 'number' | 'boolean' | 'array' | 'object';
71
+ };
72
+ /**
73
+ * @public
74
+ */
75
+ type BaseDefinition = {
76
+ name: string;
77
+ title?: string;
78
+ };
79
+ /**
80
+ * @public
81
+ */
82
+ type SchemaDefinition = {
83
+ block?: {
84
+ name: string;
85
+ };
86
+ styles?: ReadonlyArray<StyleDefinition>;
87
+ lists?: ReadonlyArray<ListDefinition>;
88
+ decorators?: ReadonlyArray<DecoratorDefinition>;
89
+ annotations?: ReadonlyArray<AnnotationDefinition>;
90
+ blockObjects?: ReadonlyArray<BlockObjectDefinition>;
91
+ inlineObjects?: ReadonlyArray<InlineObjectDefinition>;
92
+ };
93
+ /**
94
+ * @public
95
+ * A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.
96
+ * @example
97
+ * ```ts
98
+ * import { defineSchema } from '@portabletext/editor'
99
+ *
100
+ * const schemaDefinition = defineSchema({
101
+ * decorators: [{name: 'strong'}, {name: 'em'}, {name: 'underline'}],
102
+ * annotations: [{name: 'link'}],
103
+ * styles: [
104
+ * {name: 'normal'},
105
+ * {name: 'h1'},
106
+ * {name: 'h2'},
107
+ * {name: 'h3'},
108
+ * {name: 'blockquote'},
109
+ * ],
110
+ * lists: [],
111
+ * inlineObjects: [],
112
+ * blockObjects: [],
113
+ * }
114
+ * ```
115
+ */
116
+ declare function defineSchema<const TSchemaDefinition extends SchemaDefinition>(definition: TSchemaDefinition): TSchemaDefinition;
117
+ /**
118
+ * @public
119
+ */
120
+ type StyleDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition;
121
+ /**
122
+ * @public
123
+ */
124
+ type ListDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition;
125
+ /**
126
+ * @public
127
+ */
128
+ type DecoratorDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition;
129
+ /**
130
+ * @public
131
+ */
132
+ type AnnotationDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition & {
133
+ fields?: ReadonlyArray<FieldDefinition>;
134
+ };
135
+ /**
136
+ * @public
137
+ */
138
+ type BlockObjectDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition & {
139
+ fields?: ReadonlyArray<FieldDefinition>;
140
+ };
141
+ /**
142
+ * @public
143
+ */
144
+ type InlineObjectDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition & {
145
+ fields?: ReadonlyArray<FieldDefinition>;
146
+ };
147
+ /**
148
+ * @public
149
+ */
150
+ declare function compileSchema(definition: SchemaDefinition): Schema;
151
+ export { type AnnotationDefinition, type AnnotationSchemaType, type BaseDefinition, type BlockObjectDefinition, type BlockObjectSchemaType, type DecoratorDefinition, type DecoratorSchemaType, type FieldDefinition, type InlineObjectDefinition, type InlineObjectSchemaType, type ListDefinition, type ListSchemaType, type Schema, type SchemaDefinition, type StyleDefinition, type StyleSchemaType, compileSchema, defineSchema };
@@ -0,0 +1,151 @@
1
+ /**
2
+ * @public
3
+ */
4
+ type Schema = {
5
+ block: {
6
+ name: string;
7
+ };
8
+ span: {
9
+ name: string;
10
+ };
11
+ styles: ReadonlyArray<StyleSchemaType>;
12
+ lists: ReadonlyArray<ListSchemaType>;
13
+ decorators: ReadonlyArray<DecoratorSchemaType>;
14
+ annotations: ReadonlyArray<AnnotationSchemaType>;
15
+ blockObjects: ReadonlyArray<BlockObjectSchemaType>;
16
+ inlineObjects: ReadonlyArray<InlineObjectSchemaType>;
17
+ };
18
+ /**
19
+ * @public
20
+ */
21
+ type StyleSchemaType = BaseDefinition & {
22
+ /**
23
+ * @deprecated
24
+ * Use `name` instead
25
+ */
26
+ value: string;
27
+ };
28
+ /**
29
+ * @public
30
+ */
31
+ type ListSchemaType = BaseDefinition & {
32
+ /**
33
+ * @deprecated
34
+ * Use `name` instead
35
+ */
36
+ value: string;
37
+ };
38
+ /**
39
+ * @public
40
+ */
41
+ type DecoratorSchemaType = BaseDefinition & {
42
+ /**
43
+ * @deprecated
44
+ * Use `name` instead
45
+ */
46
+ value: string;
47
+ };
48
+ /**
49
+ * @public
50
+ */
51
+ type AnnotationSchemaType = BaseDefinition & {
52
+ fields: ReadonlyArray<FieldDefinition>;
53
+ };
54
+ /**
55
+ * @public
56
+ */
57
+ type BlockObjectSchemaType = BaseDefinition & {
58
+ fields: ReadonlyArray<FieldDefinition>;
59
+ };
60
+ /**
61
+ * @public
62
+ */
63
+ type InlineObjectSchemaType = BaseDefinition & {
64
+ fields: ReadonlyArray<FieldDefinition>;
65
+ };
66
+ /**
67
+ * @public
68
+ */
69
+ type FieldDefinition = BaseDefinition & {
70
+ type: 'string' | 'number' | 'boolean' | 'array' | 'object';
71
+ };
72
+ /**
73
+ * @public
74
+ */
75
+ type BaseDefinition = {
76
+ name: string;
77
+ title?: string;
78
+ };
79
+ /**
80
+ * @public
81
+ */
82
+ type SchemaDefinition = {
83
+ block?: {
84
+ name: string;
85
+ };
86
+ styles?: ReadonlyArray<StyleDefinition>;
87
+ lists?: ReadonlyArray<ListDefinition>;
88
+ decorators?: ReadonlyArray<DecoratorDefinition>;
89
+ annotations?: ReadonlyArray<AnnotationDefinition>;
90
+ blockObjects?: ReadonlyArray<BlockObjectDefinition>;
91
+ inlineObjects?: ReadonlyArray<InlineObjectDefinition>;
92
+ };
93
+ /**
94
+ * @public
95
+ * A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.
96
+ * @example
97
+ * ```ts
98
+ * import { defineSchema } from '@portabletext/editor'
99
+ *
100
+ * const schemaDefinition = defineSchema({
101
+ * decorators: [{name: 'strong'}, {name: 'em'}, {name: 'underline'}],
102
+ * annotations: [{name: 'link'}],
103
+ * styles: [
104
+ * {name: 'normal'},
105
+ * {name: 'h1'},
106
+ * {name: 'h2'},
107
+ * {name: 'h3'},
108
+ * {name: 'blockquote'},
109
+ * ],
110
+ * lists: [],
111
+ * inlineObjects: [],
112
+ * blockObjects: [],
113
+ * }
114
+ * ```
115
+ */
116
+ declare function defineSchema<const TSchemaDefinition extends SchemaDefinition>(definition: TSchemaDefinition): TSchemaDefinition;
117
+ /**
118
+ * @public
119
+ */
120
+ type StyleDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition;
121
+ /**
122
+ * @public
123
+ */
124
+ type ListDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition;
125
+ /**
126
+ * @public
127
+ */
128
+ type DecoratorDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition;
129
+ /**
130
+ * @public
131
+ */
132
+ type AnnotationDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition & {
133
+ fields?: ReadonlyArray<FieldDefinition>;
134
+ };
135
+ /**
136
+ * @public
137
+ */
138
+ type BlockObjectDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition & {
139
+ fields?: ReadonlyArray<FieldDefinition>;
140
+ };
141
+ /**
142
+ * @public
143
+ */
144
+ type InlineObjectDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition & {
145
+ fields?: ReadonlyArray<FieldDefinition>;
146
+ };
147
+ /**
148
+ * @public
149
+ */
150
+ declare function compileSchema(definition: SchemaDefinition): Schema;
151
+ export { type AnnotationDefinition, type AnnotationSchemaType, type BaseDefinition, type BlockObjectDefinition, type BlockObjectSchemaType, type DecoratorDefinition, type DecoratorSchemaType, type FieldDefinition, type InlineObjectDefinition, type InlineObjectSchemaType, type ListDefinition, type ListSchemaType, type Schema, type SchemaDefinition, type StyleDefinition, type StyleSchemaType, compileSchema, defineSchema };
package/dist/index.js ADDED
@@ -0,0 +1,43 @@
1
+ function compileSchema(definition) {
2
+ const styles = (definition.styles ?? []).map((style) => ({
3
+ ...style,
4
+ value: style.name
5
+ }));
6
+ return {
7
+ block: {
8
+ name: definition.block?.name ?? "block"
9
+ },
10
+ span: {
11
+ name: "span"
12
+ },
13
+ styles: styles.some((style) => style.value === "normal") ? styles : [{ value: "normal", name: "normal", title: "Normal" }, ...styles],
14
+ lists: (definition.lists ?? []).map((list) => ({
15
+ ...list,
16
+ value: list.name
17
+ })),
18
+ decorators: (definition.decorators ?? []).map((decorator) => ({
19
+ ...decorator,
20
+ value: decorator.name
21
+ })),
22
+ annotations: (definition.annotations ?? []).map((annotation) => ({
23
+ ...annotation,
24
+ fields: annotation.fields ?? []
25
+ })),
26
+ blockObjects: (definition.blockObjects ?? []).map((blockObject) => ({
27
+ ...blockObject,
28
+ fields: blockObject.fields ?? []
29
+ })),
30
+ inlineObjects: (definition.inlineObjects ?? []).map((inlineObject) => ({
31
+ ...inlineObject,
32
+ fields: inlineObject.fields ?? []
33
+ }))
34
+ };
35
+ }
36
+ function defineSchema(definition) {
37
+ return definition;
38
+ }
39
+ export {
40
+ compileSchema,
41
+ defineSchema
42
+ };
43
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/compile-schema.ts","../src/define-schema.ts"],"sourcesContent":["import type {SchemaDefinition} from './define-schema'\nimport type {Schema} from './schema'\n\n/**\n * @public\n */\nexport function compileSchema(definition: SchemaDefinition): Schema {\n const styles = (definition.styles ?? []).map((style) => ({\n ...style,\n value: style.name,\n }))\n\n return {\n block: {\n name: definition.block?.name ?? 'block',\n },\n span: {\n name: 'span',\n },\n styles: !styles.some((style) => style.value === 'normal')\n ? [{value: 'normal', name: 'normal', title: 'Normal'}, ...styles]\n : styles,\n lists: (definition.lists ?? []).map((list) => ({\n ...list,\n value: list.name,\n })),\n decorators: (definition.decorators ?? []).map((decorator) => ({\n ...decorator,\n value: decorator.name,\n })),\n annotations: (definition.annotations ?? []).map((annotation) => ({\n ...annotation,\n fields: annotation.fields ?? [],\n })),\n blockObjects: (definition.blockObjects ?? []).map((blockObject) => ({\n ...blockObject,\n fields: blockObject.fields ?? [],\n })),\n inlineObjects: (definition.inlineObjects ?? []).map((inlineObject) => ({\n ...inlineObject,\n fields: inlineObject.fields ?? [],\n })),\n }\n}\n","import type {BaseDefinition, FieldDefinition} from './schema'\n\n/**\n * @public\n */\nexport type SchemaDefinition = {\n block?: {\n name: string\n }\n styles?: ReadonlyArray<StyleDefinition>\n lists?: ReadonlyArray<ListDefinition>\n decorators?: ReadonlyArray<DecoratorDefinition>\n annotations?: ReadonlyArray<AnnotationDefinition>\n blockObjects?: ReadonlyArray<BlockObjectDefinition>\n inlineObjects?: ReadonlyArray<InlineObjectDefinition>\n}\n\n/**\n * @public\n * A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.\n * @example\n * ```ts\n * import { defineSchema } from '@portabletext/editor'\n *\n * const schemaDefinition = defineSchema({\n * decorators: [{name: 'strong'}, {name: 'em'}, {name: 'underline'}],\n * annotations: [{name: 'link'}],\n * styles: [\n * {name: 'normal'},\n * {name: 'h1'},\n * {name: 'h2'},\n * {name: 'h3'},\n * {name: 'blockquote'},\n * ],\n * lists: [],\n * inlineObjects: [],\n * blockObjects: [],\n * }\n * ```\n */\nexport function defineSchema<const TSchemaDefinition extends SchemaDefinition>(\n definition: TSchemaDefinition,\n): TSchemaDefinition {\n return definition\n}\n\n/**\n * @public\n */\nexport type StyleDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition\n\n/**\n * @public\n */\nexport type ListDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition\n\n/**\n * @public\n */\nexport type DecoratorDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition\n\n/**\n * @public\n */\nexport type AnnotationDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition & {\n fields?: ReadonlyArray<FieldDefinition>\n}\n\n/**\n * @public\n */\nexport type BlockObjectDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition & {\n fields?: ReadonlyArray<FieldDefinition>\n}\n\n/**\n * @public\n */\nexport type InlineObjectDefinition<\n TBaseDefinition extends BaseDefinition = BaseDefinition,\n> = TBaseDefinition & {\n fields?: ReadonlyArray<FieldDefinition>\n}\n"],"names":[],"mappings":"AAMO,SAAS,cAAc,YAAsC;AAClE,QAAM,UAAU,WAAW,UAAU,CAAA,GAAI,IAAI,CAAC,WAAW;AAAA,IACvD,GAAG;AAAA,IACH,OAAO,MAAM;AAAA,EAAA,EACb;AAEF,SAAO;AAAA,IACL,OAAO;AAAA,MACL,MAAM,WAAW,OAAO,QAAQ;AAAA,IAAA;AAAA,IAElC,MAAM;AAAA,MACJ,MAAM;AAAA,IAAA;AAAA,IAER,QAAS,OAAO,KAAK,CAAC,UAAU,MAAM,UAAU,QAAQ,IAEpD,SADA,CAAC,EAAC,OAAO,UAAU,MAAM,UAAU,OAAO,SAAA,GAAW,GAAG,MAAM;AAAA,IAElE,QAAQ,WAAW,SAAS,CAAA,GAAI,IAAI,CAAC,UAAU;AAAA,MAC7C,GAAG;AAAA,MACH,OAAO,KAAK;AAAA,IAAA,EACZ;AAAA,IACF,aAAa,WAAW,cAAc,CAAA,GAAI,IAAI,CAAC,eAAe;AAAA,MAC5D,GAAG;AAAA,MACH,OAAO,UAAU;AAAA,IAAA,EACjB;AAAA,IACF,cAAc,WAAW,eAAe,CAAA,GAAI,IAAI,CAAC,gBAAgB;AAAA,MAC/D,GAAG;AAAA,MACH,QAAQ,WAAW,UAAU,CAAA;AAAA,IAAC,EAC9B;AAAA,IACF,eAAe,WAAW,gBAAgB,CAAA,GAAI,IAAI,CAAC,iBAAiB;AAAA,MAClE,GAAG;AAAA,MACH,QAAQ,YAAY,UAAU,CAAA;AAAA,IAAC,EAC/B;AAAA,IACF,gBAAgB,WAAW,iBAAiB,CAAA,GAAI,IAAI,CAAC,kBAAkB;AAAA,MACrE,GAAG;AAAA,MACH,QAAQ,aAAa,UAAU,CAAA;AAAA,IAAC,EAChC;AAAA,EAAA;AAEN;ACHO,SAAS,aACd,YACmB;AACnB,SAAO;AACT;"}
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@portabletext/schema",
3
+ "version": "0.0.0",
4
+ "description": "Portable Text Schema",
5
+ "keywords": [
6
+ "portabletext",
7
+ "schema"
8
+ ],
9
+ "homepage": "https://www.portabletext.org/",
10
+ "bugs": {
11
+ "url": "https://github.com/portabletext/editor/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/portabletext/editor.git",
16
+ "directory": "packages/schema"
17
+ },
18
+ "license": "MIT",
19
+ "author": "Sanity.io <hello@sanity.io>",
20
+ "sideEffects": false,
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "source": "./src/index.ts",
25
+ "import": "./dist/index.js",
26
+ "require": "./dist/index.cjs",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "./package.json": "./package.json"
30
+ },
31
+ "main": "./dist/index.cjs",
32
+ "module": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "files": [
35
+ "dist",
36
+ "src"
37
+ ],
38
+ "devDependencies": {
39
+ "@sanity/pkg-utils": "^7.11.1",
40
+ "typescript": "^5.9.2"
41
+ },
42
+ "scripts": {
43
+ "build": "pkg-utils build --strict --check --clean",
44
+ "check:lint": "biome lint .",
45
+ "check:types": "tsc",
46
+ "check:types:watch": "tsc --watch",
47
+ "clean": "del .turbo && del lib && del node_modules",
48
+ "dev": "pkg-utils watch",
49
+ "lint:fix": "biome lint --write ."
50
+ }
51
+ }
@@ -0,0 +1,44 @@
1
+ import type {SchemaDefinition} from './define-schema'
2
+ import type {Schema} from './schema'
3
+
4
+ /**
5
+ * @public
6
+ */
7
+ export function compileSchema(definition: SchemaDefinition): Schema {
8
+ const styles = (definition.styles ?? []).map((style) => ({
9
+ ...style,
10
+ value: style.name,
11
+ }))
12
+
13
+ return {
14
+ block: {
15
+ name: definition.block?.name ?? 'block',
16
+ },
17
+ span: {
18
+ name: 'span',
19
+ },
20
+ styles: !styles.some((style) => style.value === 'normal')
21
+ ? [{value: 'normal', name: 'normal', title: 'Normal'}, ...styles]
22
+ : styles,
23
+ lists: (definition.lists ?? []).map((list) => ({
24
+ ...list,
25
+ value: list.name,
26
+ })),
27
+ decorators: (definition.decorators ?? []).map((decorator) => ({
28
+ ...decorator,
29
+ value: decorator.name,
30
+ })),
31
+ annotations: (definition.annotations ?? []).map((annotation) => ({
32
+ ...annotation,
33
+ fields: annotation.fields ?? [],
34
+ })),
35
+ blockObjects: (definition.blockObjects ?? []).map((blockObject) => ({
36
+ ...blockObject,
37
+ fields: blockObject.fields ?? [],
38
+ })),
39
+ inlineObjects: (definition.inlineObjects ?? []).map((inlineObject) => ({
40
+ ...inlineObject,
41
+ fields: inlineObject.fields ?? [],
42
+ })),
43
+ }
44
+ }
@@ -0,0 +1,93 @@
1
+ import type {BaseDefinition, FieldDefinition} from './schema'
2
+
3
+ /**
4
+ * @public
5
+ */
6
+ export type SchemaDefinition = {
7
+ block?: {
8
+ name: string
9
+ }
10
+ styles?: ReadonlyArray<StyleDefinition>
11
+ lists?: ReadonlyArray<ListDefinition>
12
+ decorators?: ReadonlyArray<DecoratorDefinition>
13
+ annotations?: ReadonlyArray<AnnotationDefinition>
14
+ blockObjects?: ReadonlyArray<BlockObjectDefinition>
15
+ inlineObjects?: ReadonlyArray<InlineObjectDefinition>
16
+ }
17
+
18
+ /**
19
+ * @public
20
+ * A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.
21
+ * @example
22
+ * ```ts
23
+ * import { defineSchema } from '@portabletext/editor'
24
+ *
25
+ * const schemaDefinition = defineSchema({
26
+ * decorators: [{name: 'strong'}, {name: 'em'}, {name: 'underline'}],
27
+ * annotations: [{name: 'link'}],
28
+ * styles: [
29
+ * {name: 'normal'},
30
+ * {name: 'h1'},
31
+ * {name: 'h2'},
32
+ * {name: 'h3'},
33
+ * {name: 'blockquote'},
34
+ * ],
35
+ * lists: [],
36
+ * inlineObjects: [],
37
+ * blockObjects: [],
38
+ * }
39
+ * ```
40
+ */
41
+ export function defineSchema<const TSchemaDefinition extends SchemaDefinition>(
42
+ definition: TSchemaDefinition,
43
+ ): TSchemaDefinition {
44
+ return definition
45
+ }
46
+
47
+ /**
48
+ * @public
49
+ */
50
+ export type StyleDefinition<
51
+ TBaseDefinition extends BaseDefinition = BaseDefinition,
52
+ > = TBaseDefinition
53
+
54
+ /**
55
+ * @public
56
+ */
57
+ export type ListDefinition<
58
+ TBaseDefinition extends BaseDefinition = BaseDefinition,
59
+ > = TBaseDefinition
60
+
61
+ /**
62
+ * @public
63
+ */
64
+ export type DecoratorDefinition<
65
+ TBaseDefinition extends BaseDefinition = BaseDefinition,
66
+ > = TBaseDefinition
67
+
68
+ /**
69
+ * @public
70
+ */
71
+ export type AnnotationDefinition<
72
+ TBaseDefinition extends BaseDefinition = BaseDefinition,
73
+ > = TBaseDefinition & {
74
+ fields?: ReadonlyArray<FieldDefinition>
75
+ }
76
+
77
+ /**
78
+ * @public
79
+ */
80
+ export type BlockObjectDefinition<
81
+ TBaseDefinition extends BaseDefinition = BaseDefinition,
82
+ > = TBaseDefinition & {
83
+ fields?: ReadonlyArray<FieldDefinition>
84
+ }
85
+
86
+ /**
87
+ * @public
88
+ */
89
+ export type InlineObjectDefinition<
90
+ TBaseDefinition extends BaseDefinition = BaseDefinition,
91
+ > = TBaseDefinition & {
92
+ fields?: ReadonlyArray<FieldDefinition>
93
+ }
package/src/index.ts ADDED
@@ -0,0 +1,22 @@
1
+ export {compileSchema} from './compile-schema'
2
+ export {
3
+ defineSchema,
4
+ type AnnotationDefinition,
5
+ type BlockObjectDefinition,
6
+ type DecoratorDefinition,
7
+ type InlineObjectDefinition,
8
+ type ListDefinition,
9
+ type SchemaDefinition,
10
+ type StyleDefinition,
11
+ } from './define-schema'
12
+ export type {
13
+ AnnotationSchemaType,
14
+ BaseDefinition,
15
+ BlockObjectSchemaType,
16
+ DecoratorSchemaType,
17
+ FieldDefinition,
18
+ InlineObjectSchemaType,
19
+ ListSchemaType,
20
+ Schema,
21
+ StyleSchemaType,
22
+ } from './schema'
package/src/schema.ts ADDED
@@ -0,0 +1,86 @@
1
+ /**
2
+ * @public
3
+ */
4
+ export type Schema = {
5
+ block: {
6
+ name: string
7
+ }
8
+ span: {
9
+ name: string
10
+ }
11
+ styles: ReadonlyArray<StyleSchemaType>
12
+ lists: ReadonlyArray<ListSchemaType>
13
+ decorators: ReadonlyArray<DecoratorSchemaType>
14
+ annotations: ReadonlyArray<AnnotationSchemaType>
15
+ blockObjects: ReadonlyArray<BlockObjectSchemaType>
16
+ inlineObjects: ReadonlyArray<InlineObjectSchemaType>
17
+ }
18
+
19
+ /**
20
+ * @public
21
+ */
22
+ export type StyleSchemaType = BaseDefinition & {
23
+ /**
24
+ * @deprecated
25
+ * Use `name` instead
26
+ */
27
+ value: string
28
+ }
29
+
30
+ /**
31
+ * @public
32
+ */
33
+ export type ListSchemaType = BaseDefinition & {
34
+ /**
35
+ * @deprecated
36
+ * Use `name` instead
37
+ */
38
+ value: string
39
+ }
40
+
41
+ /**
42
+ * @public
43
+ */
44
+ export type DecoratorSchemaType = BaseDefinition & {
45
+ /**
46
+ * @deprecated
47
+ * Use `name` instead
48
+ */
49
+ value: string
50
+ }
51
+
52
+ /**
53
+ * @public
54
+ */
55
+ export type AnnotationSchemaType = BaseDefinition & {
56
+ fields: ReadonlyArray<FieldDefinition>
57
+ }
58
+
59
+ /**
60
+ * @public
61
+ */
62
+ export type BlockObjectSchemaType = BaseDefinition & {
63
+ fields: ReadonlyArray<FieldDefinition>
64
+ }
65
+
66
+ /**
67
+ * @public
68
+ */
69
+ export type InlineObjectSchemaType = BaseDefinition & {
70
+ fields: ReadonlyArray<FieldDefinition>
71
+ }
72
+
73
+ /**
74
+ * @public
75
+ */
76
+ export type FieldDefinition = BaseDefinition & {
77
+ type: 'string' | 'number' | 'boolean' | 'array' | 'object'
78
+ }
79
+
80
+ /**
81
+ * @public
82
+ */
83
+ export type BaseDefinition = {
84
+ name: string
85
+ title?: string
86
+ }