-
Notifications
You must be signed in to change notification settings - Fork 391
/
html.ts
199 lines (196 loc) · 5.54 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
import randomKey from '../../util/randomKey'
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,
} from '../../constants'
import {tagName} from '../helpers'
export function resolveListItem(listNodeTagName) {
let listStyle
switch (listNodeTagName) {
case 'ul':
listStyle = 'bullet'
break
case 'ol':
listStyle = 'number'
break
default:
listStyle = 'bullet'
}
return listStyle
}
export default function createHTMLRules(blockContentType, options: any = {}) {
return [
// Text nodes
{
// eslint-disable-next-line complexity
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 = {...HTML_BLOCK_TAGS, ...HTML_HEADER_TAGS}
delete blocks.blockquote
const children = []
el.childNodes.forEach((node, index) => {
if (node.nodeType === 1 && Object.keys(blocks).includes(node.localName.toLowerCase())) {
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)
}
})
return {
_type: 'block',
style: 'blockquote',
markDefs: [],
children: next(children),
}
},
}, // Block elements
{
deserialize(el, next) {
const blocks = {...HTML_BLOCK_TAGS, ...HTML_HEADER_TAGS}
let block = blocks[tagName(el)]
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 span = HTML_SPAN_TAGS[tagName(el)]
if (!span) {
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 listContainer = HTML_LIST_CONTAINER_TAGS[tagName(el)]
if (!listContainer) {
return undefined
}
return next(el.childNodes)
},
}, // Deal with br's
{
deserialize(el, next) {
if (tagName(el) === 'br') {
return {
...DEFAULT_SPAN,
text: '\n',
}
}
return undefined
},
}, // Deal with list items
{
deserialize(el, next) {
const listItem = HTML_LIST_ITEM_TAGS[tagName(el)]
if (!listItem || !el.parentNode || !HTML_LIST_CONTAINER_TAGS[tagName(el.parentNode)]) {
return undefined
}
listItem.listItem = resolveListItem(tagName(el.parentNode))
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 = 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)
},
},
]
}