/
html.ts
222 lines (216 loc) · 6.15 KB
/
html.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import type {ArraySchemaType} from '@sanity/types'
import {randomKey} from '../../util/randomKey'
import {isElement, tagName} from '../helpers'
import {
DEFAULT_BLOCK,
DEFAULT_SPAN,
HTML_BLOCK_TAGS,
HTML_HEADER_TAGS,
HTML_SPAN_TAGS,
HTML_LIST_CONTAINER_TAGS,
HTML_LIST_ITEM_TAGS,
HTML_DECORATOR_TAGS,
PartialBlock,
} from '../../constants'
import {BlockEnabledFeatures, DeserializerRule} from '../../types'
export function resolveListItem(listNodeTagName: string): string {
let listStyle
switch (listNodeTagName) {
case 'ul':
listStyle = 'bullet'
break
case 'ol':
listStyle = 'number'
break
default:
listStyle = 'bullet'
}
return listStyle
}
export default function createHTMLRules(
blockContentType: ArraySchemaType,
options: BlockEnabledFeatures
): DeserializerRule[] {
return [
// Text nodes
{
deserialize(el) {
const isValidWhiteSpace =
el.nodeType === 3 &&
(el.textContent || '').replace(/[\r\n]/g, ' ').replace(/\s\s+/g, ' ') === ' ' &&
el.nextSibling &&
el.nextSibling.nodeType !== 3 &&
el.previousSibling &&
el.previousSibling.nodeType !== 3
const isValidText =
(isValidWhiteSpace || el.textContent !== ' ') && tagName(el.parentNode) !== 'body'
if (el.nodeName === '#text' && isValidText) {
return {
...DEFAULT_SPAN,
marks: [],
text: (el.textContent || '').replace(/\s\s+/g, ' '),
}
}
return undefined
},
}, // Blockquote element
{
deserialize(el, next) {
if (tagName(el) !== 'blockquote') {
return undefined
}
const blocks: Record<string, PartialBlock | undefined> = {
...HTML_BLOCK_TAGS,
...HTML_HEADER_TAGS,
}
delete blocks.blockquote
const children: HTMLElement[] = []
el.childNodes.forEach((node, index) => {
if (
node.nodeType === 1 &&
Object.keys(blocks).includes((node as Element).localName.toLowerCase())
) {
if (!el.ownerDocument) {
return
}
const span = el.ownerDocument.createElement('span')
span.appendChild(el.ownerDocument.createTextNode('\r'))
node.childNodes.forEach((cn) => {
span.appendChild(cn.cloneNode(true))
})
if (index !== el.childNodes.length) {
span.appendChild(el.ownerDocument.createTextNode('\r'))
}
children.push(span)
} else {
children.push(node as HTMLElement)
}
})
return {
_type: 'block',
style: 'blockquote',
markDefs: [],
children: next(children),
}
},
}, // Block elements
{
deserialize(el, next) {
const blocks: Record<string, PartialBlock | undefined> = {
...HTML_BLOCK_TAGS,
...HTML_HEADER_TAGS,
}
const tag = tagName(el)
let block = tag ? blocks[tag] : undefined
if (!block) {
return undefined
}
// Don't add blocks into list items
if (el.parentNode && tagName(el.parentNode) === 'li') {
return next(el.childNodes)
}
// If style is not supported, return a defaultBlockType
if (!options.enabledBlockStyles.includes(block.style)) {
block = DEFAULT_BLOCK
}
return {
...block,
children: next(el.childNodes),
}
},
}, // Ignore span tags
{
deserialize(el, next) {
const tag = tagName(el)
if (!tag || !(tag in HTML_SPAN_TAGS)) {
return undefined
}
return next(el.childNodes)
},
}, // Ignore div tags
{
deserialize(el, next) {
const div = tagName(el) === 'div'
if (!div) {
return undefined
}
return next(el.childNodes)
},
}, // Ignore list containers
{
deserialize(el, next) {
const tag = tagName(el)
if (!tag || !(tag in HTML_LIST_CONTAINER_TAGS)) {
return undefined
}
return next(el.childNodes)
},
}, // Deal with br's
{
deserialize(el) {
if (tagName(el) === 'br') {
return {
...DEFAULT_SPAN,
text: '\n',
}
}
return undefined
},
}, // Deal with list items
{
deserialize(el, next) {
const tag = tagName(el)
const listItem = tag ? HTML_LIST_ITEM_TAGS[tag] : undefined
const parentTag = tagName(el.parentNode) || ''
if (!listItem || !el.parentNode || !HTML_LIST_CONTAINER_TAGS[parentTag]) {
return undefined
}
listItem.listItem = resolveListItem(parentTag)
return {
...listItem,
children: next(el.childNodes),
}
},
}, // Deal with decorators
{
deserialize(el, next) {
const decorator = HTML_DECORATOR_TAGS[tagName(el) || '']
if (!decorator || !options.enabledSpanDecorators.includes(decorator)) {
return undefined
}
return {
_type: '__decorator',
name: decorator,
children: next(el.childNodes),
}
},
}, // Special case for hyperlinks, add annotation (if allowed by schema),
// If not supported just write out the link text and href in plain text.
{
deserialize(el, next) {
if (tagName(el) != 'a') {
return undefined
}
const linkEnabled = options.enabledBlockAnnotations.includes('link')
const href = isElement(el) && el.getAttribute('href')
if (!href) {
return next(el.childNodes)
}
let markDef
if (linkEnabled) {
markDef = {
_key: randomKey(12),
_type: 'link',
href: href,
}
return {
_type: '__annotation',
markDef: markDef,
children: next(el.childNodes),
}
}
return el.appendChild(el.ownerDocument.createTextNode(` (${href})`)) && next(el.childNodes)
},
},
]
}