@portabletext/block-tools 2.0.7 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -31
- package/lib/index.cjs +159 -162
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +24 -39
- package/lib/index.d.ts +24 -39
- package/lib/index.js +159 -162
- package/lib/index.js.map +1 -1
- package/package.json +3 -2
- package/src/HtmlDeserializer/helpers.ts +23 -46
- package/src/HtmlDeserializer/index.ts +15 -27
- package/src/HtmlDeserializer/rules/gdocs.ts +6 -7
- package/src/HtmlDeserializer/rules/html.ts +35 -24
- package/src/HtmlDeserializer/rules/index.ts +7 -7
- package/src/HtmlDeserializer/rules/notion.ts +1 -4
- package/src/index.ts +13 -28
- package/src/types.portable-text.ts +79 -0
- package/src/types.ts +11 -57
- package/src/util/normalizeBlock.ts +21 -7
- package/src/util/{blockContentTypeFeatures.ts → portable-text-schema.ts} +72 -39
|
@@ -3,23 +3,42 @@ import {
|
|
|
3
3
|
isBlockListObjectField,
|
|
4
4
|
isBlockSchemaType,
|
|
5
5
|
isBlockStyleObjectField,
|
|
6
|
-
isObjectSchemaType,
|
|
7
6
|
isTitledListValue,
|
|
8
7
|
type ArraySchemaType,
|
|
9
8
|
type BlockSchemaType,
|
|
10
9
|
type EnumListProps,
|
|
11
|
-
type I18nTitledListValue,
|
|
12
|
-
type ObjectSchemaType,
|
|
13
10
|
type SpanSchemaType,
|
|
14
|
-
type TitledListValue,
|
|
15
11
|
} from '@sanity/types'
|
|
16
|
-
import type {BlockContentFeatures, ResolvedAnnotationType} from '../types'
|
|
17
12
|
import {findBlockType} from './findBlockType'
|
|
18
13
|
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
export type PortableTextSchema = {
|
|
15
|
+
block: {
|
|
16
|
+
name: string
|
|
17
|
+
}
|
|
18
|
+
span: {
|
|
19
|
+
name: string
|
|
20
|
+
}
|
|
21
|
+
styles: ReadonlyArray<{
|
|
22
|
+
name: string
|
|
23
|
+
title?: string
|
|
24
|
+
}>
|
|
25
|
+
lists: ReadonlyArray<{
|
|
26
|
+
name: string
|
|
27
|
+
title?: string
|
|
28
|
+
}>
|
|
29
|
+
decorators: ReadonlyArray<{
|
|
30
|
+
name: string
|
|
31
|
+
title?: string
|
|
32
|
+
}>
|
|
33
|
+
annotations: ReadonlyArray<{
|
|
34
|
+
name: string
|
|
35
|
+
title?: string
|
|
36
|
+
}>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function getPortableTextSchema(
|
|
21
40
|
blockContentType: ArraySchemaType,
|
|
22
|
-
):
|
|
41
|
+
): PortableTextSchema {
|
|
23
42
|
if (!blockContentType) {
|
|
24
43
|
throw new Error("Parameter 'blockContentType' required")
|
|
25
44
|
}
|
|
@@ -43,33 +62,30 @@ export default function blockContentFeatures(
|
|
|
43
62
|
)
|
|
44
63
|
}
|
|
45
64
|
|
|
46
|
-
const
|
|
47
|
-
(inlineType): inlineType is ObjectSchemaType =>
|
|
48
|
-
inlineType.name !== 'span' && isObjectSchemaType(inlineType),
|
|
49
|
-
)
|
|
65
|
+
const blockName = blockContentType.of.find(findBlockType)?.name
|
|
50
66
|
|
|
51
|
-
|
|
52
|
-
(
|
|
53
|
-
|
|
54
|
-
)
|
|
67
|
+
if (!blockName) {
|
|
68
|
+
throw new Error('No `block` type found in schema type')
|
|
69
|
+
}
|
|
55
70
|
|
|
56
71
|
return {
|
|
57
72
|
styles: resolveEnabledStyles(blockType),
|
|
58
73
|
decorators: resolveEnabledDecorators(spanType),
|
|
59
74
|
annotations: resolveEnabledAnnotationTypes(spanType),
|
|
60
75
|
lists: resolveEnabledListItems(blockType),
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
76
|
+
block: {
|
|
77
|
+
name: blockName,
|
|
78
|
+
},
|
|
79
|
+
span: {
|
|
80
|
+
name: spanType.name,
|
|
66
81
|
},
|
|
67
82
|
}
|
|
68
83
|
}
|
|
69
84
|
|
|
70
|
-
function resolveEnabledStyles(
|
|
71
|
-
|
|
72
|
-
|
|
85
|
+
function resolveEnabledStyles(blockType: BlockSchemaType): ReadonlyArray<{
|
|
86
|
+
name: string
|
|
87
|
+
title?: string
|
|
88
|
+
}> {
|
|
73
89
|
const styleField = blockType.fields.find(isBlockStyleObjectField)
|
|
74
90
|
if (!styleField) {
|
|
75
91
|
throw new Error(
|
|
@@ -92,24 +108,30 @@ function resolveEnabledStyles(
|
|
|
92
108
|
|
|
93
109
|
function resolveEnabledAnnotationTypes(
|
|
94
110
|
spanType: SpanSchemaType,
|
|
95
|
-
):
|
|
111
|
+
): ReadonlyArray<{
|
|
112
|
+
name: string
|
|
113
|
+
title?: string
|
|
114
|
+
}> {
|
|
96
115
|
return spanType.annotations.map((annotation) => ({
|
|
116
|
+
name: annotation.name,
|
|
97
117
|
title: annotation.title,
|
|
98
|
-
type: annotation,
|
|
99
|
-
value: annotation.name,
|
|
100
|
-
icon: annotation.icon,
|
|
101
118
|
}))
|
|
102
119
|
}
|
|
103
120
|
|
|
104
|
-
function resolveEnabledDecorators(
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
121
|
+
function resolveEnabledDecorators(spanType: SpanSchemaType): ReadonlyArray<{
|
|
122
|
+
name: string
|
|
123
|
+
title?: string
|
|
124
|
+
}> {
|
|
125
|
+
return spanType.decorators.map((decorator) => ({
|
|
126
|
+
name: decorator.value,
|
|
127
|
+
title: decorator.title,
|
|
128
|
+
}))
|
|
108
129
|
}
|
|
109
130
|
|
|
110
|
-
function resolveEnabledListItems(
|
|
111
|
-
|
|
112
|
-
|
|
131
|
+
function resolveEnabledListItems(blockType: BlockSchemaType): ReadonlyArray<{
|
|
132
|
+
name: string
|
|
133
|
+
title?: string
|
|
134
|
+
}> {
|
|
113
135
|
const listField = blockType.fields.find(isBlockListObjectField)
|
|
114
136
|
if (!listField) {
|
|
115
137
|
throw new Error(
|
|
@@ -129,13 +151,24 @@ function resolveEnabledListItems(
|
|
|
129
151
|
|
|
130
152
|
function getTitledListValuesFromEnumListOptions(
|
|
131
153
|
options: EnumListProps<string> | undefined,
|
|
132
|
-
):
|
|
154
|
+
): ReadonlyArray<{
|
|
155
|
+
name: string
|
|
156
|
+
title?: string
|
|
157
|
+
}> {
|
|
133
158
|
const list = options ? options.list : undefined
|
|
134
159
|
if (!Array.isArray(list)) {
|
|
135
160
|
return []
|
|
136
161
|
}
|
|
137
162
|
|
|
138
|
-
return list.map((item) =>
|
|
139
|
-
isTitledListValue(item)
|
|
140
|
-
|
|
163
|
+
return list.map((item) => {
|
|
164
|
+
if (isTitledListValue(item)) {
|
|
165
|
+
return {
|
|
166
|
+
name: item.value ?? item.title,
|
|
167
|
+
title: item.title,
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
name: item,
|
|
172
|
+
}
|
|
173
|
+
})
|
|
141
174
|
}
|