-
Notifications
You must be signed in to change notification settings - Fork 6
/
rich-text-to-jsx.js
187 lines (144 loc) 路 4.51 KB
/
rich-text-to-jsx.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* eslint-disable no-use-before-define */
import React from 'react';
import { BLOCKS, INLINES, MARKS } from '@contentful/rich-text-types';
import cx from './lib/cx';
import get from './lib/get';
import isEmpty from './lib/is-empty';
import UnknownElement from './components/UnknownElement';
import BlockElement from './components/BlockElement';
import InlineElement from './components/InlineElement';
export const defaultOptions = {
overrides: {},
createElement: React.createElement
};
const customBlockNodes = {
[BLOCKS.EMBEDDED_ENTRY]: true,
[BLOCKS.EMBEDDED_ASSET]: true
};
const tagMap = {
[BLOCKS.HEADING_1]: 'h1',
[BLOCKS.HEADING_2]: 'h2',
[BLOCKS.HEADING_3]: 'h3',
[BLOCKS.HEADING_4]: 'h4',
[BLOCKS.HEADING_5]: 'h5',
[BLOCKS.HEADING_6]: 'h6',
[BLOCKS.PARAGRAPH]: 'p',
[BLOCKS.UL_LIST]: 'ul',
[BLOCKS.OL_LIST]: 'ol',
[BLOCKS.LIST_ITEM]: 'li',
[BLOCKS.QUOTE]: 'blockquote',
[BLOCKS.HR]: 'hr',
[INLINES.HYPERLINK]: 'a',
[MARKS.BOLD]: 'strong',
[MARKS.ITALIC]: 'em',
[MARKS.UNDERLINE]: 'u',
[MARKS.CODE]: 'code'
};
export default function richTextToJsx(richText, options = {}) {
if (!richText) {
return null;
}
return nodeListToJsx(richText.content, { ...defaultOptions, ...options });
}
export function nodeListToJsx(nodes, options) {
if (isEmpty(nodes)) {
return null;
}
return nodes.map((node, key) => nodeToJsx(node, options, key));
}
export function nodeToJsx(node = {}, options = {}, key) {
const { nodeType } = node;
if (!nodeType) {
return unknownNodeToJsx(node, options, key);
}
const isTextNode = nodeType === 'text';
if (isTextNode) {
return textNodeToJsx(node, options, key);
}
const isCustomNode = !tagMap[nodeType];
if (isCustomNode) {
return customNodeToJsx(node, options, key);
}
return parentNodeToJsx(node, options, key);
}
export function unknownNodeToJsx(node, options, key) {
const { data, content } = node;
const { createElement } = options;
const props = { ...data.target, key };
const children = isEmpty(content)
? undefined
: nodeListToJsx(content, options);
return createElement(UnknownElement, props, children);
}
export function textNodeToJsx(node, options, key) {
const { data = {}, value, marks } = node;
const { overrides, createElement } = options;
if (isEmpty(marks)) {
return value;
}
return marks.reduce((children, mark, markKey) => {
const element = getElement(mark.type, overrides);
if (!element) {
return children;
}
const props = getProps(mark.type, overrides, {
...data,
key: `${key}${markKey}`
});
return createElement(element, props, children);
}, value);
}
export function customNodeToJsx(node, options, key) {
const { data, content, nodeType } = node;
const { overrides, createElement } = options;
const contentType = get(data, 'target.contentType');
if (!contentType) {
return unknownNodeToJsx(node, options, key);
}
const elementOverrides = overrides[contentType];
const isBlockNode = customBlockNodes[nodeType];
const DefaultElement = isBlockNode ? BlockElement : InlineElement;
const element = getElement(nodeType, elementOverrides) || DefaultElement;
const props = getProps(nodeType, elementOverrides, {
...data.target,
key
});
const children = isEmpty(content)
? undefined
: nodeListToJsx(content, options);
return createElement(element, props, children);
}
export function parentNodeToJsx(node, options, key) {
const { data, content, nodeType } = node;
const { overrides, createElement } = options;
const element = getElement(nodeType, overrides) || BlockElement;
const { uri: href, ...rest } = data;
const props = getProps(nodeType, overrides, { ...rest, href, key });
const children = isEmpty(content)
? undefined
: nodeListToJsx(content, options);
return createElement(element, props, children);
}
export function getElement(type, overrides) {
const override = getOverride(type, overrides);
if (override) {
return typeof override === 'function' ? override : override.component;
}
return tagMap[type];
}
export function getProps(type, overrides, data = {}) {
const override = getOverride(type, overrides);
if (isEmpty(override) || typeof override === 'function') {
return data;
}
const overrideProps = override.props || {};
return {
...overrideProps,
...data,
className: cx(data.className, overrideProps.className)
};
}
export function getOverride(type, overrides = {}) {
const tag = tagMap[type];
return overrides[tag] || overrides[type];
}