This repository has been archived by the owner on Mar 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (98 loc) · 2.74 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
import {BLOCKS, helpers, INLINES, MARKS} from '@contentful/rich-text-types';
import React, {cloneElement, isValidElement} from 'react';
const defaultInline = (node) => <span key={node.data.target.sys.id}>
type: {node.nodeType} id: {node.data.target.sys.id}
</span>;
const defaultNodeRenderers = {
[BLOCKS.PARAGRAPH]: (node, children) => (
<p>{children}</p>
),
[BLOCKS.HEADING_1]: (node, children) => (
<h1>{children}</h1>
),
[BLOCKS.HEADING_2]: (node, children) => (
<h2>{children}</h2>
),
[BLOCKS.HEADING_3]: (node, children) => (
<h3>{children}</h3>
),
[BLOCKS.HEADING_4]: (node, children) => (
<h4>{children}</h4>
),
[BLOCKS.HEADING_5]: (node, children) => (
<h5>{children}</h5>
),
[BLOCKS.HEADING_6]: (node, children) => (
<h6>{children}</h6>
),
[BLOCKS.EMBEDDED_ENTRY]: (node, children) => (
<div>{children}</div>
),
[BLOCKS.UL_LIST]: (node, children) => (
<ul>{children}</ul>
),
[BLOCKS.OL_LIST]: (node, children) => (
<ol>{children}</ol>
),
[BLOCKS.LIST_ITEM]: (node, children) => (
<li>{children}</li>
),
[BLOCKS.QUOTE]: (node, children) => (
<blockquote>{children}</blockquote>
),
[BLOCKS.HR]: () => <hr/>,
[INLINES.ASSET_HYPERLINK]: defaultInline,
[INLINES.ENTRY_HYPERLINK]: defaultInline,
[INLINES.EMBEDDED_ENTRY]: defaultInline,
[INLINES.HYPERLINK]: (node, children) => (
<a href={node.data.uri}>
{children}
</a>
)
};
const defaultMarkRenderers = {
[MARKS.BOLD]: (text) => <b>{text}</b>,
[MARKS.ITALIC]: (text) => <i>{text}</i>,
[MARKS.UNDERLINE]: (text) => <u>{text}</u>,
[MARKS.CODE]: (text) => <code>{text}</code>
};
/**
* Serialize a Contentful Rich Text `document` to React tree
*/
export function documentToReactTree(richTextDocument, options = {}) {
const renderNode = {
...defaultNodeRenderers,
...options.renderNode
};
const renderMark = {
...defaultMarkRenderers,
...options.renderMark,
};
function convertNodeList(nodes) {
return nodes.map((node, index) => {
return appendKeyToValidElement(convertNode(node), index);
});
}
function convertNode(node) {
if (helpers.isText(node)) {
return node.marks.reduce((value, mark) => {
if (!renderMark[mark.type]) {
return value;
}
return renderMark[mark.type](value);
}, node.value);
} else {
if (!node.nodeType || !renderNode[node.nodeType]) {
return null;
}
return renderNode[node.nodeType](node, convertNodeList(node.content));
}
}
return convertNodeList(richTextDocument.content);
}
const appendKeyToValidElement = (element, key) => {
if (element && isValidElement(element) && element.key === null) {
return cloneElement(element, {key});
}
return element;
};