@portabletext/block-tools 2.0.8 → 3.1.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 +78 -162
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +27 -41
- package/lib/index.d.ts +27 -41
- package/lib/index.js +78 -162
- package/lib/index.js.map +1 -1
- package/package.json +9 -6
- package/src/HtmlDeserializer/helpers.ts +23 -46
- package/src/HtmlDeserializer/index.ts +15 -30
- package/src/HtmlDeserializer/rules/gdocs.ts +6 -9
- 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 +25 -30
- package/src/types.portable-text.ts +79 -0
- package/src/types.ts +11 -57
- package/src/util/normalizeBlock.ts +23 -7
- package/src/util/blockContentTypeFeatures.ts +0 -141
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
isBlockChildrenObjectField,
|
|
3
|
-
isBlockListObjectField,
|
|
4
|
-
isBlockSchemaType,
|
|
5
|
-
isBlockStyleObjectField,
|
|
6
|
-
isObjectSchemaType,
|
|
7
|
-
isTitledListValue,
|
|
8
|
-
type ArraySchemaType,
|
|
9
|
-
type BlockSchemaType,
|
|
10
|
-
type EnumListProps,
|
|
11
|
-
type I18nTitledListValue,
|
|
12
|
-
type ObjectSchemaType,
|
|
13
|
-
type SpanSchemaType,
|
|
14
|
-
type TitledListValue,
|
|
15
|
-
} from '@sanity/types'
|
|
16
|
-
import type {BlockContentFeatures, ResolvedAnnotationType} from '../types'
|
|
17
|
-
import {findBlockType} from './findBlockType'
|
|
18
|
-
|
|
19
|
-
// Helper method for describing a blockContentType's feature set
|
|
20
|
-
export default function blockContentFeatures(
|
|
21
|
-
blockContentType: ArraySchemaType,
|
|
22
|
-
): BlockContentFeatures {
|
|
23
|
-
if (!blockContentType) {
|
|
24
|
-
throw new Error("Parameter 'blockContentType' required")
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const blockType = blockContentType.of.find(findBlockType)
|
|
28
|
-
if (!isBlockSchemaType(blockType)) {
|
|
29
|
-
throw new Error("'block' type is not defined in this schema (required).")
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const ofType = blockType.fields.find(isBlockChildrenObjectField)?.type?.of
|
|
33
|
-
if (!ofType) {
|
|
34
|
-
throw new Error('No `of` declaration found for blocks `children` field')
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const spanType = ofType.find(
|
|
38
|
-
(member): member is SpanSchemaType => member.name === 'span',
|
|
39
|
-
)
|
|
40
|
-
if (!spanType) {
|
|
41
|
-
throw new Error(
|
|
42
|
-
'No `span` type found in `block` schema type `children` definition',
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const inlineObjectTypes = ofType.filter(
|
|
47
|
-
(inlineType): inlineType is ObjectSchemaType =>
|
|
48
|
-
inlineType.name !== 'span' && isObjectSchemaType(inlineType),
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
const blockObjectTypes = blockContentType.of.filter(
|
|
52
|
-
(memberType): memberType is ObjectSchemaType =>
|
|
53
|
-
memberType.name !== blockType.name && isObjectSchemaType(memberType),
|
|
54
|
-
)
|
|
55
|
-
|
|
56
|
-
return {
|
|
57
|
-
styles: resolveEnabledStyles(blockType),
|
|
58
|
-
decorators: resolveEnabledDecorators(spanType),
|
|
59
|
-
annotations: resolveEnabledAnnotationTypes(spanType),
|
|
60
|
-
lists: resolveEnabledListItems(blockType),
|
|
61
|
-
types: {
|
|
62
|
-
block: blockContentType,
|
|
63
|
-
span: spanType,
|
|
64
|
-
inlineObjects: inlineObjectTypes,
|
|
65
|
-
blockObjects: blockObjectTypes,
|
|
66
|
-
},
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function resolveEnabledStyles(
|
|
71
|
-
blockType: BlockSchemaType,
|
|
72
|
-
): TitledListValue<string>[] {
|
|
73
|
-
const styleField = blockType.fields.find(isBlockStyleObjectField)
|
|
74
|
-
if (!styleField) {
|
|
75
|
-
throw new Error(
|
|
76
|
-
"A field with name 'style' is not defined in the block type (required).",
|
|
77
|
-
)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const textStyles = getTitledListValuesFromEnumListOptions(
|
|
81
|
-
styleField.type.options,
|
|
82
|
-
)
|
|
83
|
-
if (textStyles.length === 0) {
|
|
84
|
-
throw new Error(
|
|
85
|
-
'The style fields need at least one style ' +
|
|
86
|
-
"defined. I.e: {title: 'Normal', value: 'normal'}.",
|
|
87
|
-
)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return textStyles
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function resolveEnabledAnnotationTypes(
|
|
94
|
-
spanType: SpanSchemaType,
|
|
95
|
-
): ResolvedAnnotationType[] {
|
|
96
|
-
return spanType.annotations.map((annotation) => ({
|
|
97
|
-
title: annotation.title,
|
|
98
|
-
type: annotation,
|
|
99
|
-
value: annotation.name,
|
|
100
|
-
icon: annotation.icon,
|
|
101
|
-
}))
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function resolveEnabledDecorators(
|
|
105
|
-
spanType: SpanSchemaType,
|
|
106
|
-
): TitledListValue<string>[] {
|
|
107
|
-
return spanType.decorators
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function resolveEnabledListItems(
|
|
111
|
-
blockType: BlockSchemaType,
|
|
112
|
-
): I18nTitledListValue<string>[] {
|
|
113
|
-
const listField = blockType.fields.find(isBlockListObjectField)
|
|
114
|
-
if (!listField) {
|
|
115
|
-
throw new Error(
|
|
116
|
-
"A field with name 'list' is not defined in the block type (required).",
|
|
117
|
-
)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const listItems = getTitledListValuesFromEnumListOptions(
|
|
121
|
-
listField.type.options,
|
|
122
|
-
)
|
|
123
|
-
if (!listItems) {
|
|
124
|
-
throw new Error('The list field need at least to be an empty array')
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return listItems
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function getTitledListValuesFromEnumListOptions(
|
|
131
|
-
options: EnumListProps<string> | undefined,
|
|
132
|
-
): I18nTitledListValue<string>[] {
|
|
133
|
-
const list = options ? options.list : undefined
|
|
134
|
-
if (!Array.isArray(list)) {
|
|
135
|
-
return []
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return list.map((item) =>
|
|
139
|
-
isTitledListValue(item) ? item : {title: item, value: item},
|
|
140
|
-
)
|
|
141
|
-
}
|