@portabletext/schema 1.2.0 → 2.0.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/dist/index.d.ts CHANGED
@@ -206,4 +206,5 @@ interface PortableTextObject {
206
206
  _key: string;
207
207
  [other: string]: unknown;
208
208
  }
209
- 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 PortableTextBlock, type PortableTextObject, type PortableTextSpan, type PortableTextTextBlock, type Schema, type SchemaDefinition, type StyleDefinition, type StyleSchemaType, type TypedObject, compileSchema, defineSchema, isSpan, isTextBlock, isTypedObject };
209
+ 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 PortableTextBlock, type PortableTextObject, type PortableTextSpan, type PortableTextTextBlock, type Schema, type SchemaDefinition, type StyleDefinition, type StyleSchemaType, type TypedObject, compileSchema, defineSchema, isSpan, isTextBlock, isTypedObject };
210
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/schema.ts","../src/define-schema.ts","../src/compile-schema.ts","../src/types.ts"],"sourcesContent":[],"mappings":"AAGA;;;AAQwB,KARZ,MAAA,GAQY;EAAd,KAAA,EAAA;IACa,IAAA,EAAA,MAAA;IAAd,MAAA,CAAA,EANI,aAMJ,CANkB,eAMlB,CAAA;EACmB,CAAA;EAAd,IAAA,EAAA;IACe,IAAA,EAAA,MAAA;EAAd,CAAA;EACe,MAAA,EAJpB,aAIoB,CAJN,eAIM,CAAA;EAAd,KAAA,EAHP,aAGO,CAHO,cAGP,CAAA;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d,KAAA,EAAA,MAAA;;;;;AAGqB,iBGFd,aAAA,CHEc,MAAA,EAAA,OAAA,CAAA,EAAA,MAAA,IGF4B,WHE5B;;;;AACA,KGQlB,iBAAA,GAAoB,qBHRF,GGQ0B,kBHR1B;AAM9B;AAWA;AAWA;AAWY,UG1BK,qBH0Be,CAAA,SGzBrB,gBHyBqB,GGzBF,kBHyBE,CAAA,CAAA;EAAG,KAAA,EAAA,MAAA;EACX,IAAA,EAAA,MAAA;EAAd,QAAA,EGtBE,MHsBF,EAAA;EAAa,QAAA,CAAA,EGrBV,kBHqBU,EAAA;EAMX,QAAA,CAAA,EAAA,MAAA;EAAwB,KAAA,CAAA,EAAA,MAAA;EACZ,KAAA,CAAA,EAAA,MAAA;;;AAMxB;;AACwB,iBG1BR,WAAA,CH0BQ,OAAA,EAAA;EAAd,MAAA,EGzBU,MHyBV;CAAa,EAAA,KAAA,EAAA,OAAA,CAAA,EAAA,KAAA,IGvBX,qBHuBW;AAMvB;AAOA;;AC9EY,UE6DK,gBAAA,CF7DW;EAGD,IAAA,EAAA,MAAA;EAAd,KAAA,EAAA,MAAA;EAEY,IAAA,EAAA,MAAA;EAAd,KAAA,CAAA,EAAA,MAAA,EAAA;;;;;AAGmB,iBE+Dd,MAAA,CF/Dc,OAAA,EAAA;EAAd,MAAA,EEgEI,MFhEJ;CACe,EAAA,KAAA,EAAA,OAAA,CAAA,EAAA,KAAA,IEiEnB,gBFjEmB;;;;AACA,UEmFd,kBAAA,CFnFc;EA0Bf,KAAA,EAAA,MAAA;EAA6C,IAAA,EAAA,MAAA;EAC/C,CAAA,KAAA,EAAA,MAAA,CAAA,EAAA,OAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/schema",
3
- "version": "1.2.0",
3
+ "version": "2.0.1",
4
4
  "description": "Portable Text Schema",
5
5
  "keywords": [
6
6
  "portabletext",
@@ -22,23 +22,23 @@
22
22
  "exports": {
23
23
  ".": {
24
24
  "source": "./src/index.ts",
25
- "import": "./dist/index.js",
26
- "require": "./dist/index.cjs",
27
25
  "default": "./dist/index.js"
28
26
  },
29
27
  "./package.json": "./package.json"
30
28
  },
31
- "main": "./dist/index.cjs",
32
- "module": "./dist/index.js",
29
+ "main": "./dist/index.js",
33
30
  "types": "./dist/index.d.ts",
34
31
  "files": [
35
- "dist",
36
- "src"
32
+ "dist"
37
33
  ],
38
34
  "devDependencies": {
39
- "@sanity/pkg-utils": "^8.0.5",
40
- "typescript": "^5.9.2",
41
- "vitest": "^3.2.4"
35
+ "@sanity/pkg-utils": "^10.1.1",
36
+ "@sanity/tsconfig": "^1.0.0",
37
+ "typescript": "5.9.3",
38
+ "vitest": "^4.0.14"
39
+ },
40
+ "engines": {
41
+ "node": ">=20.19 <22 || >=22.12"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "pkg-utils build --strict --check --clean",
package/dist/index.cjs DELETED
@@ -1,69 +0,0 @@
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
- })), blockFields = [];
8
- if (definition.block?.fields)
9
- for (const field of definition.block.fields) {
10
- if (field.name === "_type" || field.name === "_key" || field.name === "children" || field.name === "markDefs" || field.name === "style" || field.name === "listItem" || field.name === "level") {
11
- console.warn(
12
- `"${field.name}" is a reserved field name on Portable Text blocks`
13
- );
14
- continue;
15
- }
16
- blockFields.push(field);
17
- }
18
- return {
19
- block: {
20
- name: definition.block?.name ?? "block",
21
- ...blockFields.length > 0 ? { fields: blockFields } : {}
22
- },
23
- span: {
24
- name: "span"
25
- },
26
- styles: styles.some((style) => style.value === "normal") ? styles : [{ value: "normal", name: "normal", title: "Normal" }, ...styles],
27
- lists: (definition.lists ?? []).map((list) => ({
28
- ...list,
29
- value: list.name
30
- })),
31
- decorators: (definition.decorators ?? []).map((decorator) => ({
32
- ...decorator,
33
- value: decorator.name
34
- })),
35
- annotations: (definition.annotations ?? []).map((annotation) => ({
36
- ...annotation,
37
- fields: annotation.fields ?? []
38
- })),
39
- blockObjects: (definition.blockObjects ?? []).map((blockObject) => ({
40
- ...blockObject,
41
- fields: blockObject.fields ?? []
42
- })),
43
- inlineObjects: (definition.inlineObjects ?? []).map((inlineObject) => ({
44
- ...inlineObject,
45
- fields: inlineObject.fields ?? []
46
- }))
47
- };
48
- }
49
- function defineSchema(definition) {
50
- return definition;
51
- }
52
- function isTypedObject(object) {
53
- return isRecord(object) && typeof object._type == "string";
54
- }
55
- function isRecord(value) {
56
- return !!value && (typeof value == "object" || typeof value == "function");
57
- }
58
- function isTextBlock(context, block) {
59
- return !(!isTypedObject(block) || block._type !== context.schema.block.name || !Array.isArray(block.children));
60
- }
61
- function isSpan(context, child) {
62
- return !(!isTypedObject(child) || child._type !== context.schema.span.name || typeof child.text != "string");
63
- }
64
- exports.compileSchema = compileSchema;
65
- exports.defineSchema = defineSchema;
66
- exports.isSpan = isSpan;
67
- exports.isTextBlock = isTextBlock;
68
- exports.isTypedObject = isTypedObject;
69
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/compile-schema.ts","../src/define-schema.ts","../src/types.ts"],"sourcesContent":["import type {SchemaDefinition} from './define-schema'\nimport type {FieldDefinition, 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 const blockFields: Array<FieldDefinition> = []\n\n if (definition.block?.fields) {\n for (const field of definition.block.fields) {\n if (\n field.name === '_type' ||\n field.name === '_key' ||\n field.name === 'children' ||\n field.name === 'markDefs' ||\n field.name === 'style' ||\n field.name === 'listItem' ||\n field.name === 'level'\n ) {\n console.warn(\n `\"${field.name}\" is a reserved field name on Portable Text blocks`,\n )\n continue\n }\n\n blockFields.push(field)\n }\n }\n\n return {\n block: {\n name: definition.block?.name ?? 'block',\n ...(blockFields.length > 0 ? {fields: blockFields} : {}),\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 fields?: ReadonlyArray<FieldDefinition>\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","import type {Schema} from './schema'\n\n/**\n * @public\n */\nexport interface TypedObject {\n [key: string]: unknown\n _type: string\n}\n\n/**\n * @public\n */\nexport function isTypedObject(object: unknown): object is TypedObject {\n return isRecord(object) && typeof object._type === 'string'\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return !!value && (typeof value === 'object' || typeof value === 'function')\n}\n\n/**\n * @public\n */\nexport type PortableTextBlock = PortableTextTextBlock | PortableTextObject\n\n/**\n * @public\n */\nexport interface PortableTextTextBlock<\n TChild = PortableTextSpan | PortableTextObject,\n> {\n _type: string\n _key: string\n children: TChild[]\n markDefs?: PortableTextObject[]\n listItem?: string\n style?: string\n level?: number\n}\n\n/**\n * @public\n */\nexport function isTextBlock(\n context: {schema: Schema},\n block: unknown,\n): block is PortableTextTextBlock {\n if (!isTypedObject(block)) {\n return false\n }\n\n if (block._type !== context.schema.block.name) {\n return false\n }\n\n if (!Array.isArray(block.children)) {\n return false\n }\n\n return true\n}\n\n/**\n * @public\n */\nexport interface PortableTextSpan {\n _key: string\n _type: 'span'\n text: string\n marks?: string[]\n}\n\n/**\n * @public\n */\nexport function isSpan(\n context: {schema: Schema},\n child: unknown,\n): child is PortableTextSpan {\n if (!isTypedObject(child)) {\n return false\n }\n\n if (child._type !== context.schema.span.name) {\n return false\n }\n\n if (typeof child.text !== 'string') {\n return false\n }\n\n return true\n}\n\n/**\n * @public\n */\nexport interface PortableTextObject {\n _type: string\n _key: string\n [other: string]: unknown\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,GAEI,cAAsC,CAAA;AAE5C,MAAI,WAAW,OAAO;AACpB,eAAW,SAAS,WAAW,MAAM,QAAQ;AAC3C,UACE,MAAM,SAAS,WACf,MAAM,SAAS,UACf,MAAM,SAAS,cACf,MAAM,SAAS,cACf,MAAM,SAAS,WACf,MAAM,SAAS,cACf,MAAM,SAAS,SACf;AACA,gBAAQ;AAAA,UACN,IAAI,MAAM,IAAI;AAAA,QAAA;AAEhB;AAAA,MACF;AAEA,kBAAY,KAAK,KAAK;AAAA,IACxB;AAGF,SAAO;AAAA,IACL,OAAO;AAAA,MACL,MAAM,WAAW,OAAO,QAAQ;AAAA,MAChC,GAAI,YAAY,SAAS,IAAI,EAAC,QAAQ,YAAA,IAAe,CAAA;AAAA,IAAC;AAAA,IAExD,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;AC1BO,SAAS,aACd,YACmB;AACnB,SAAO;AACT;AChCO,SAAS,cAAc,QAAwC;AACpE,SAAO,SAAS,MAAM,KAAK,OAAO,OAAO,SAAU;AACrD;AAEA,SAAS,SAAS,OAAkD;AAClE,SAAO,CAAC,CAAC,UAAU,OAAO,SAAU,YAAY,OAAO,SAAU;AACnE;AAyBO,SAAS,YACd,SACA,OACgC;AAShC,SARI,EAAA,CAAC,cAAc,KAAK,KAIpB,MAAM,UAAU,QAAQ,OAAO,MAAM,QAIrC,CAAC,MAAM,QAAQ,MAAM,QAAQ;AAKnC;AAeO,SAAS,OACd,SACA,OAC2B;AAS3B,SARI,EAAA,CAAC,cAAc,KAAK,KAIpB,MAAM,UAAU,QAAQ,OAAO,KAAK,QAIpC,OAAO,MAAM,QAAS;AAK5B;;;;;;"}
package/dist/index.d.cts DELETED
@@ -1,209 +0,0 @@
1
- /**
2
- * @public
3
- */
4
- type Schema = {
5
- block: {
6
- name: string;
7
- fields?: ReadonlyArray<FieldDefinition>;
8
- };
9
- span: {
10
- name: string;
11
- };
12
- styles: ReadonlyArray<StyleSchemaType>;
13
- lists: ReadonlyArray<ListSchemaType>;
14
- decorators: ReadonlyArray<DecoratorSchemaType>;
15
- annotations: ReadonlyArray<AnnotationSchemaType>;
16
- blockObjects: ReadonlyArray<BlockObjectSchemaType>;
17
- inlineObjects: ReadonlyArray<InlineObjectSchemaType>;
18
- };
19
- /**
20
- * @public
21
- */
22
- type StyleSchemaType = BaseDefinition & {
23
- /**
24
- * @deprecated
25
- * Use `name` instead
26
- */
27
- value: string;
28
- };
29
- /**
30
- * @public
31
- */
32
- type ListSchemaType = BaseDefinition & {
33
- /**
34
- * @deprecated
35
- * Use `name` instead
36
- */
37
- value: string;
38
- };
39
- /**
40
- * @public
41
- */
42
- type DecoratorSchemaType = BaseDefinition & {
43
- /**
44
- * @deprecated
45
- * Use `name` instead
46
- */
47
- value: string;
48
- };
49
- /**
50
- * @public
51
- */
52
- type AnnotationSchemaType = BaseDefinition & {
53
- fields: ReadonlyArray<FieldDefinition>;
54
- };
55
- /**
56
- * @public
57
- */
58
- type BlockObjectSchemaType = BaseDefinition & {
59
- fields: ReadonlyArray<FieldDefinition>;
60
- };
61
- /**
62
- * @public
63
- */
64
- type InlineObjectSchemaType = BaseDefinition & {
65
- fields: ReadonlyArray<FieldDefinition>;
66
- };
67
- /**
68
- * @public
69
- */
70
- type FieldDefinition = BaseDefinition & {
71
- type: 'string' | 'number' | 'boolean' | 'array' | 'object';
72
- };
73
- /**
74
- * @public
75
- */
76
- type BaseDefinition = {
77
- name: string;
78
- title?: string;
79
- };
80
- /**
81
- * @public
82
- */
83
- type SchemaDefinition = {
84
- block?: {
85
- name?: string;
86
- fields?: ReadonlyArray<FieldDefinition>;
87
- };
88
- styles?: ReadonlyArray<StyleDefinition>;
89
- lists?: ReadonlyArray<ListDefinition>;
90
- decorators?: ReadonlyArray<DecoratorDefinition>;
91
- annotations?: ReadonlyArray<AnnotationDefinition>;
92
- blockObjects?: ReadonlyArray<BlockObjectDefinition>;
93
- inlineObjects?: ReadonlyArray<InlineObjectDefinition>;
94
- };
95
- /**
96
- * @public
97
- * A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.
98
- * @example
99
- * ```ts
100
- * import { defineSchema } from '@portabletext/editor'
101
- *
102
- * const schemaDefinition = defineSchema({
103
- * decorators: [{name: 'strong'}, {name: 'em'}, {name: 'underline'}],
104
- * annotations: [{name: 'link'}],
105
- * styles: [
106
- * {name: 'normal'},
107
- * {name: 'h1'},
108
- * {name: 'h2'},
109
- * {name: 'h3'},
110
- * {name: 'blockquote'},
111
- * ],
112
- * lists: [],
113
- * inlineObjects: [],
114
- * blockObjects: [],
115
- * }
116
- * ```
117
- */
118
- declare function defineSchema<const TSchemaDefinition extends SchemaDefinition>(definition: TSchemaDefinition): TSchemaDefinition;
119
- /**
120
- * @public
121
- */
122
- type StyleDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition;
123
- /**
124
- * @public
125
- */
126
- type ListDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition;
127
- /**
128
- * @public
129
- */
130
- type DecoratorDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition;
131
- /**
132
- * @public
133
- */
134
- type AnnotationDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition & {
135
- fields?: ReadonlyArray<FieldDefinition>;
136
- };
137
- /**
138
- * @public
139
- */
140
- type BlockObjectDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition & {
141
- fields?: ReadonlyArray<FieldDefinition>;
142
- };
143
- /**
144
- * @public
145
- */
146
- type InlineObjectDefinition<TBaseDefinition extends BaseDefinition = BaseDefinition> = TBaseDefinition & {
147
- fields?: ReadonlyArray<FieldDefinition>;
148
- };
149
- /**
150
- * @public
151
- */
152
- declare function compileSchema(definition: SchemaDefinition): Schema;
153
- /**
154
- * @public
155
- */
156
- interface TypedObject {
157
- [key: string]: unknown;
158
- _type: string;
159
- }
160
- /**
161
- * @public
162
- */
163
- declare function isTypedObject(object: unknown): object is TypedObject;
164
- /**
165
- * @public
166
- */
167
- type PortableTextBlock = PortableTextTextBlock | PortableTextObject;
168
- /**
169
- * @public
170
- */
171
- interface PortableTextTextBlock<TChild = PortableTextSpan | PortableTextObject> {
172
- _type: string;
173
- _key: string;
174
- children: TChild[];
175
- markDefs?: PortableTextObject[];
176
- listItem?: string;
177
- style?: string;
178
- level?: number;
179
- }
180
- /**
181
- * @public
182
- */
183
- declare function isTextBlock(context: {
184
- schema: Schema;
185
- }, block: unknown): block is PortableTextTextBlock;
186
- /**
187
- * @public
188
- */
189
- interface PortableTextSpan {
190
- _key: string;
191
- _type: 'span';
192
- text: string;
193
- marks?: string[];
194
- }
195
- /**
196
- * @public
197
- */
198
- declare function isSpan(context: {
199
- schema: Schema;
200
- }, child: unknown): child is PortableTextSpan;
201
- /**
202
- * @public
203
- */
204
- interface PortableTextObject {
205
- _type: string;
206
- _key: string;
207
- [other: string]: unknown;
208
- }
209
- 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 PortableTextBlock, type PortableTextObject, type PortableTextSpan, type PortableTextTextBlock, type Schema, type SchemaDefinition, type StyleDefinition, type StyleSchemaType, type TypedObject, compileSchema, defineSchema, isSpan, isTextBlock, isTypedObject };
@@ -1,46 +0,0 @@
1
- import {describe, expect, test, vi} from 'vitest'
2
- import {compileSchema} from './compile-schema'
3
-
4
- describe(compileSchema.name, () => {
5
- describe('block fields', () => {
6
- test('reserved fields are ignored and warned about', () => {
7
- const consoleWarnSpy = vi
8
- .spyOn(console, 'warn')
9
- .mockImplementation(() => {})
10
-
11
- expect(
12
- compileSchema({block: {fields: [{name: '_type', type: 'string'}]}}),
13
- ).toEqual({
14
- block: {name: 'block'},
15
- span: {name: 'span'},
16
- styles: [{value: 'normal', name: 'normal', title: 'Normal'}],
17
- lists: [],
18
- decorators: [],
19
- annotations: [],
20
- blockObjects: [],
21
- inlineObjects: [],
22
- })
23
-
24
- expect(consoleWarnSpy).toHaveBeenCalledWith(
25
- '"_type" is a reserved field name on Portable Text blocks',
26
- )
27
-
28
- consoleWarnSpy.mockRestore()
29
- })
30
-
31
- test('custom fields are included', () => {
32
- expect(
33
- compileSchema({block: {fields: [{name: 'foo', type: 'string'}]}}),
34
- ).toEqual({
35
- block: {name: 'block', fields: [{name: 'foo', type: 'string'}]},
36
- span: {name: 'span'},
37
- styles: [{value: 'normal', name: 'normal', title: 'Normal'}],
38
- lists: [],
39
- decorators: [],
40
- annotations: [],
41
- blockObjects: [],
42
- inlineObjects: [],
43
- })
44
- })
45
- })
46
- })
@@ -1,68 +0,0 @@
1
- import type {SchemaDefinition} from './define-schema'
2
- import type {FieldDefinition, 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
- const blockFields: Array<FieldDefinition> = []
14
-
15
- if (definition.block?.fields) {
16
- for (const field of definition.block.fields) {
17
- if (
18
- field.name === '_type' ||
19
- field.name === '_key' ||
20
- field.name === 'children' ||
21
- field.name === 'markDefs' ||
22
- field.name === 'style' ||
23
- field.name === 'listItem' ||
24
- field.name === 'level'
25
- ) {
26
- console.warn(
27
- `"${field.name}" is a reserved field name on Portable Text blocks`,
28
- )
29
- continue
30
- }
31
-
32
- blockFields.push(field)
33
- }
34
- }
35
-
36
- return {
37
- block: {
38
- name: definition.block?.name ?? 'block',
39
- ...(blockFields.length > 0 ? {fields: blockFields} : {}),
40
- },
41
- span: {
42
- name: 'span',
43
- },
44
- styles: !styles.some((style) => style.value === 'normal')
45
- ? [{value: 'normal', name: 'normal', title: 'Normal'}, ...styles]
46
- : styles,
47
- lists: (definition.lists ?? []).map((list) => ({
48
- ...list,
49
- value: list.name,
50
- })),
51
- decorators: (definition.decorators ?? []).map((decorator) => ({
52
- ...decorator,
53
- value: decorator.name,
54
- })),
55
- annotations: (definition.annotations ?? []).map((annotation) => ({
56
- ...annotation,
57
- fields: annotation.fields ?? [],
58
- })),
59
- blockObjects: (definition.blockObjects ?? []).map((blockObject) => ({
60
- ...blockObject,
61
- fields: blockObject.fields ?? [],
62
- })),
63
- inlineObjects: (definition.inlineObjects ?? []).map((inlineObject) => ({
64
- ...inlineObject,
65
- fields: inlineObject.fields ?? [],
66
- })),
67
- }
68
- }
@@ -1,94 +0,0 @@
1
- import type {BaseDefinition, FieldDefinition} from './schema'
2
-
3
- /**
4
- * @public
5
- */
6
- export type SchemaDefinition = {
7
- block?: {
8
- name?: string
9
- fields?: ReadonlyArray<FieldDefinition>
10
- }
11
- styles?: ReadonlyArray<StyleDefinition>
12
- lists?: ReadonlyArray<ListDefinition>
13
- decorators?: ReadonlyArray<DecoratorDefinition>
14
- annotations?: ReadonlyArray<AnnotationDefinition>
15
- blockObjects?: ReadonlyArray<BlockObjectDefinition>
16
- inlineObjects?: ReadonlyArray<InlineObjectDefinition>
17
- }
18
-
19
- /**
20
- * @public
21
- * A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.
22
- * @example
23
- * ```ts
24
- * import { defineSchema } from '@portabletext/editor'
25
- *
26
- * const schemaDefinition = defineSchema({
27
- * decorators: [{name: 'strong'}, {name: 'em'}, {name: 'underline'}],
28
- * annotations: [{name: 'link'}],
29
- * styles: [
30
- * {name: 'normal'},
31
- * {name: 'h1'},
32
- * {name: 'h2'},
33
- * {name: 'h3'},
34
- * {name: 'blockquote'},
35
- * ],
36
- * lists: [],
37
- * inlineObjects: [],
38
- * blockObjects: [],
39
- * }
40
- * ```
41
- */
42
- export function defineSchema<const TSchemaDefinition extends SchemaDefinition>(
43
- definition: TSchemaDefinition,
44
- ): TSchemaDefinition {
45
- return definition
46
- }
47
-
48
- /**
49
- * @public
50
- */
51
- export type StyleDefinition<
52
- TBaseDefinition extends BaseDefinition = BaseDefinition,
53
- > = TBaseDefinition
54
-
55
- /**
56
- * @public
57
- */
58
- export type ListDefinition<
59
- TBaseDefinition extends BaseDefinition = BaseDefinition,
60
- > = TBaseDefinition
61
-
62
- /**
63
- * @public
64
- */
65
- export type DecoratorDefinition<
66
- TBaseDefinition extends BaseDefinition = BaseDefinition,
67
- > = TBaseDefinition
68
-
69
- /**
70
- * @public
71
- */
72
- export type AnnotationDefinition<
73
- TBaseDefinition extends BaseDefinition = BaseDefinition,
74
- > = TBaseDefinition & {
75
- fields?: ReadonlyArray<FieldDefinition>
76
- }
77
-
78
- /**
79
- * @public
80
- */
81
- export type BlockObjectDefinition<
82
- TBaseDefinition extends BaseDefinition = BaseDefinition,
83
- > = TBaseDefinition & {
84
- fields?: ReadonlyArray<FieldDefinition>
85
- }
86
-
87
- /**
88
- * @public
89
- */
90
- export type InlineObjectDefinition<
91
- TBaseDefinition extends BaseDefinition = BaseDefinition,
92
- > = TBaseDefinition & {
93
- fields?: ReadonlyArray<FieldDefinition>
94
- }
package/src/index.ts DELETED
@@ -1,30 +0,0 @@
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'
23
- export {isSpan, isTextBlock, isTypedObject} from './types'
24
- export type {
25
- PortableTextBlock,
26
- PortableTextObject,
27
- PortableTextSpan,
28
- PortableTextTextBlock,
29
- TypedObject,
30
- } from './types'
package/src/schema.ts DELETED
@@ -1,87 +0,0 @@
1
- /**
2
- * @public
3
- */
4
- export type Schema = {
5
- block: {
6
- name: string
7
- fields?: ReadonlyArray<FieldDefinition>
8
- }
9
- span: {
10
- name: string
11
- }
12
- styles: ReadonlyArray<StyleSchemaType>
13
- lists: ReadonlyArray<ListSchemaType>
14
- decorators: ReadonlyArray<DecoratorSchemaType>
15
- annotations: ReadonlyArray<AnnotationSchemaType>
16
- blockObjects: ReadonlyArray<BlockObjectSchemaType>
17
- inlineObjects: ReadonlyArray<InlineObjectSchemaType>
18
- }
19
-
20
- /**
21
- * @public
22
- */
23
- export type StyleSchemaType = BaseDefinition & {
24
- /**
25
- * @deprecated
26
- * Use `name` instead
27
- */
28
- value: string
29
- }
30
-
31
- /**
32
- * @public
33
- */
34
- export type ListSchemaType = BaseDefinition & {
35
- /**
36
- * @deprecated
37
- * Use `name` instead
38
- */
39
- value: string
40
- }
41
-
42
- /**
43
- * @public
44
- */
45
- export type DecoratorSchemaType = BaseDefinition & {
46
- /**
47
- * @deprecated
48
- * Use `name` instead
49
- */
50
- value: string
51
- }
52
-
53
- /**
54
- * @public
55
- */
56
- export type AnnotationSchemaType = BaseDefinition & {
57
- fields: ReadonlyArray<FieldDefinition>
58
- }
59
-
60
- /**
61
- * @public
62
- */
63
- export type BlockObjectSchemaType = BaseDefinition & {
64
- fields: ReadonlyArray<FieldDefinition>
65
- }
66
-
67
- /**
68
- * @public
69
- */
70
- export type InlineObjectSchemaType = BaseDefinition & {
71
- fields: ReadonlyArray<FieldDefinition>
72
- }
73
-
74
- /**
75
- * @public
76
- */
77
- export type FieldDefinition = BaseDefinition & {
78
- type: 'string' | 'number' | 'boolean' | 'array' | 'object'
79
- }
80
-
81
- /**
82
- * @public
83
- */
84
- export type BaseDefinition = {
85
- name: string
86
- title?: string
87
- }
package/src/types.ts DELETED
@@ -1,103 +0,0 @@
1
- import type {Schema} from './schema'
2
-
3
- /**
4
- * @public
5
- */
6
- export interface TypedObject {
7
- [key: string]: unknown
8
- _type: string
9
- }
10
-
11
- /**
12
- * @public
13
- */
14
- export function isTypedObject(object: unknown): object is TypedObject {
15
- return isRecord(object) && typeof object._type === 'string'
16
- }
17
-
18
- function isRecord(value: unknown): value is Record<string, unknown> {
19
- return !!value && (typeof value === 'object' || typeof value === 'function')
20
- }
21
-
22
- /**
23
- * @public
24
- */
25
- export type PortableTextBlock = PortableTextTextBlock | PortableTextObject
26
-
27
- /**
28
- * @public
29
- */
30
- export interface PortableTextTextBlock<
31
- TChild = PortableTextSpan | PortableTextObject,
32
- > {
33
- _type: string
34
- _key: string
35
- children: TChild[]
36
- markDefs?: PortableTextObject[]
37
- listItem?: string
38
- style?: string
39
- level?: number
40
- }
41
-
42
- /**
43
- * @public
44
- */
45
- export function isTextBlock(
46
- context: {schema: Schema},
47
- block: unknown,
48
- ): block is PortableTextTextBlock {
49
- if (!isTypedObject(block)) {
50
- return false
51
- }
52
-
53
- if (block._type !== context.schema.block.name) {
54
- return false
55
- }
56
-
57
- if (!Array.isArray(block.children)) {
58
- return false
59
- }
60
-
61
- return true
62
- }
63
-
64
- /**
65
- * @public
66
- */
67
- export interface PortableTextSpan {
68
- _key: string
69
- _type: 'span'
70
- text: string
71
- marks?: string[]
72
- }
73
-
74
- /**
75
- * @public
76
- */
77
- export function isSpan(
78
- context: {schema: Schema},
79
- child: unknown,
80
- ): child is PortableTextSpan {
81
- if (!isTypedObject(child)) {
82
- return false
83
- }
84
-
85
- if (child._type !== context.schema.span.name) {
86
- return false
87
- }
88
-
89
- if (typeof child.text !== 'string') {
90
- return false
91
- }
92
-
93
- return true
94
- }
95
-
96
- /**
97
- * @public
98
- */
99
- export interface PortableTextObject {
100
- _type: string
101
- _key: string
102
- [other: string]: unknown
103
- }