-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (159 loc) · 4.5 KB
/
index.js
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
import React from 'react'
const defaultBlockComponent = blockType =>
({
unstyled: 'p',
paragraph: 'p',
'header-one': 'h1',
'header-two': 'h2',
'header-three': 'h3',
'header-four': 'h4',
'header-five': 'h5',
'header-six': 'h6',
'unordered-list-item': ['li', 'ul'],
'ordered-list-item': ['li', 'ol'],
blockquote: 'blockquote',
'code-block': 'pre',
atomic: 'div'
}[blockType] || 'p')
const defaultStyleComponent = style =>
({
BOLD: 'b',
ITALIC: 'i',
UNDERLINE: 'u',
STRIKETHROUGH: 's',
CODE: 'code'
}[style] || 'span')
const defaultEntityComponent = entityType =>
({
IMAGE: ({ data }) => <img {...data} />,
LINK: ({ data, children }) => <a href={data.url}>{children}</a>
}[entityType])
const parseBlockComponent = component => {
if (typeof component === 'object' && component instanceof Array) {
return component
}
return [component, null]
}
const Renderer = ({
data: { blocks, entityMap },
getBlockComponent = defaultBlockComponent,
getStyleComponent = defaultStyleComponent,
getEntityComponent = defaultEntityComponent,
textComponent = ({ children }) => children
}) => {
let blockList = []
let blocksDepthStack = []
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i]
if (block.depth > 0) {
blocksDepthStack[block.depth - 1].children =
blocksDepthStack[block.depth - 1].children || []
blocksDepthStack[block.depth - 1].children.push(block)
} else {
blockList.push(block)
}
blocksDepthStack[block.depth] = block
blocksDepthStack.splice(block.depth + 1)
}
const renderArray = array => {
let newArray = []
let innerArray = []
for (let i = 0; i < array.length; i++) {
let block = array[i]
if (innerArray.length > 0 && innerArray[0].type === block.type) {
innerArray.push(block)
} else {
innerArray = [block]
newArray.push(innerArray)
}
}
return newArray.map(group =>
React.createElement(
parseBlockComponent(getBlockComponent(group[0].type))[1] ||
React.Fragment,
{ key: group[0].key },
group.map(block =>
React.createElement(
parseBlockComponent(getBlockComponent(block.type))[0],
{ key: block.key, data: block.data },
block.children
? [renderRichText(block), ...renderArray(block.children)]
: renderRichText(block)
)
)
)
)
}
const renderRichText = block => {
let indexes = [0]
block.entityRanges.forEach(entity => {
indexes.push(entity.offset)
indexes.push(entity.offset + entity.length)
})
block.inlineStyleRanges.forEach(style => {
indexes.push(style.offset)
indexes.push(style.offset + style.length)
})
indexes.sort((a, b) => a - b)
indexes = indexes.filter(
(v, i) => indexes.indexOf(v) === i && v != block.text.length
)
let ranges = indexes
.map((index, i) => ({
start: index,
end: indexes[i + 1] || block.text.length
}))
.map(({ start, end }) => ({
start,
end,
text: block.text.substring(start, end),
styles: block.inlineStyleRanges
.filter(
style => style.offset <= start && style.offset + style.length >= end
)
.map(style => style.style),
entities: block.entityRanges
.filter(
entity =>
entity.offset <= start && entity.offset + entity.length >= end
)
.map(entity => entityMap[entity.key])
}))
const renderStyledText = (text, styles) => {
let rendered = text
for (let i = 0; i < styles.length; i++) {
rendered = React.createElement(
getStyleComponent(styles[i]),
{},
rendered
)
}
return rendered
}
const renderEntities = (text, entities) => {
let rendered = text
for (let i = 0; i < entities.length; i++) {
rendered = React.createElement(
getEntityComponent(entities[i].type),
{ data: entities[i].data },
rendered
)
}
return rendered
}
return ranges.map((range, index) =>
React.cloneElement(
renderEntities(
renderStyledText(
React.createElement(textComponent, {}, range.text),
range.styles
),
range.entities
),
{ key: index }
)
)
}
return renderArray(blockList)
}
export default Renderer