@nixxie-cms/document-renderer 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type JSX, type ReactNode } from 'react';
|
|
2
|
+
export type Node = Element | Text;
|
|
3
|
+
export type Element = {
|
|
4
|
+
children: Node[];
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
};
|
|
7
|
+
export type Text = {
|
|
8
|
+
text: string;
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
};
|
|
11
|
+
type Mark = 'bold' | 'italic' | 'underline' | 'strikethrough' | 'code' | 'superscript' | 'subscript' | 'keyboard';
|
|
12
|
+
type Component<Props> = (props: Props) => ReactNode;
|
|
13
|
+
type OnlyChildrenComponent = Component<{
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
}> | keyof JSX.IntrinsicElements;
|
|
16
|
+
type MarkRenderers = {
|
|
17
|
+
[Key in Mark]: OnlyChildrenComponent;
|
|
18
|
+
};
|
|
19
|
+
interface Renderers {
|
|
20
|
+
inline: {
|
|
21
|
+
link: Component<{
|
|
22
|
+
children: ReactNode;
|
|
23
|
+
href: string;
|
|
24
|
+
}> | 'a';
|
|
25
|
+
relationship: Component<{
|
|
26
|
+
relationship: string;
|
|
27
|
+
data: {
|
|
28
|
+
id: string;
|
|
29
|
+
label: string | undefined;
|
|
30
|
+
data: Record<string, any> | undefined;
|
|
31
|
+
} | null;
|
|
32
|
+
}>;
|
|
33
|
+
} & MarkRenderers;
|
|
34
|
+
block: {
|
|
35
|
+
block: OnlyChildrenComponent;
|
|
36
|
+
paragraph: Component<{
|
|
37
|
+
children: ReactNode;
|
|
38
|
+
textAlign: 'center' | 'end' | undefined;
|
|
39
|
+
}>;
|
|
40
|
+
blockquote: OnlyChildrenComponent;
|
|
41
|
+
code: Component<{
|
|
42
|
+
children: string;
|
|
43
|
+
}> | keyof JSX.IntrinsicElements;
|
|
44
|
+
layout: Component<{
|
|
45
|
+
layout: [number, ...number[]];
|
|
46
|
+
children: ReactNode[];
|
|
47
|
+
}>;
|
|
48
|
+
divider: Component<unknown> | keyof JSX.IntrinsicElements;
|
|
49
|
+
heading: Component<{
|
|
50
|
+
level: 1 | 2 | 3 | 4 | 5 | 6;
|
|
51
|
+
children: ReactNode;
|
|
52
|
+
textAlign: 'center' | 'end' | undefined;
|
|
53
|
+
}>;
|
|
54
|
+
list: Component<{
|
|
55
|
+
type: 'ordered' | 'unordered';
|
|
56
|
+
children: ReactNode[];
|
|
57
|
+
}>;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export declare const defaultRenderers: Renderers;
|
|
61
|
+
export type DocumentRendererProps<ComponentBlocks extends Record<string, Component<any>> = Record<string, Component<any>>> = {
|
|
62
|
+
document: Element[];
|
|
63
|
+
renderers?: {
|
|
64
|
+
inline?: Partial<Renderers['inline']>;
|
|
65
|
+
block?: Partial<Renderers['block']>;
|
|
66
|
+
};
|
|
67
|
+
componentBlocks?: ComponentBlocks;
|
|
68
|
+
};
|
|
69
|
+
export declare function DocumentRenderer<ComponentBlocks extends Record<string, Component<any>>>(props: DocumentRendererProps<ComponentBlocks>): import("react/jsx-runtime").JSX.Element;
|
|
70
|
+
export {};
|
|
71
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,GAAG,EAAqB,KAAK,SAAS,EAAE,MAAM,OAAO,CAAA;AAE7E,MAAM,MAAM,IAAI,GAAG,OAAO,GAAG,IAAI,CAAA;AAEjC,MAAM,MAAM,OAAO,GAAG;IACpB,QAAQ,EAAE,IAAI,EAAE,CAAA;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACvB,CAAA;AAED,MAAM,MAAM,IAAI,GAAG;IACjB,IAAI,EAAE,MAA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C"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "
|
|
2
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,
|
|
1
|
+
export * from "./declarations/src/index.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibml4eGllLWNtcy1kb2N1bWVudC1yZW5kZXJlci5janMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4vZGVjbGFyYXRpb25zL3NyYy9pbmRleC5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
|
|
@@ -1,16 +1,239 @@
|
|
|
1
|
-
|
|
2
|
-
// this file might look strange and you might be wondering what it's for
|
|
3
|
-
// it's lets you import your source files by importing this entrypoint
|
|
4
|
-
// as you would import it if it was built with preconstruct build
|
|
5
|
-
// this file is slightly different to some others though
|
|
6
|
-
// it has a require hook which compiles your code with Babel
|
|
7
|
-
// this means that you don't have to set up @babel/register or anything like that
|
|
8
|
-
// but you can still require this module and it'll be compiled
|
|
1
|
+
'use strict';
|
|
9
2
|
|
|
10
|
-
|
|
11
|
-
let unregister = require("../../../node_modules/.pnpm/@preconstruct+hook@0.4.0/node_modules/@preconstruct/hook").___internalHook(typeof __dirname === 'undefined' ? undefined : __dirname, "../../..", "..");
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
12
4
|
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
15
7
|
|
|
16
|
-
|
|
8
|
+
const defaultRenderers = {
|
|
9
|
+
inline: {
|
|
10
|
+
bold: 'strong',
|
|
11
|
+
code: 'code',
|
|
12
|
+
keyboard: 'kbd',
|
|
13
|
+
strikethrough: 's',
|
|
14
|
+
italic: 'em',
|
|
15
|
+
link: 'a',
|
|
16
|
+
subscript: 'sub',
|
|
17
|
+
superscript: 'sup',
|
|
18
|
+
underline: 'u',
|
|
19
|
+
relationship: ({
|
|
20
|
+
data
|
|
21
|
+
}) => {
|
|
22
|
+
return /*#__PURE__*/jsxRuntime.jsx("span", {
|
|
23
|
+
children: (data === null || data === void 0 ? void 0 : data.label) || (data === null || data === void 0 ? void 0 : data.id)
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
block: {
|
|
28
|
+
block: 'div',
|
|
29
|
+
blockquote: 'blockquote',
|
|
30
|
+
paragraph: ({
|
|
31
|
+
children,
|
|
32
|
+
textAlign
|
|
33
|
+
}) => {
|
|
34
|
+
return /*#__PURE__*/jsxRuntime.jsx("p", {
|
|
35
|
+
style: {
|
|
36
|
+
textAlign
|
|
37
|
+
},
|
|
38
|
+
children: children
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
divider: 'hr',
|
|
42
|
+
heading: ({
|
|
43
|
+
level,
|
|
44
|
+
children,
|
|
45
|
+
textAlign
|
|
46
|
+
}) => {
|
|
47
|
+
const Heading = `h${level}`;
|
|
48
|
+
return /*#__PURE__*/jsxRuntime.jsx(Heading, {
|
|
49
|
+
style: {
|
|
50
|
+
textAlign
|
|
51
|
+
},
|
|
52
|
+
children: children
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
code: 'pre',
|
|
56
|
+
list: ({
|
|
57
|
+
children,
|
|
58
|
+
type
|
|
59
|
+
}) => {
|
|
60
|
+
const List = type === 'ordered' ? 'ol' : 'ul';
|
|
61
|
+
return /*#__PURE__*/jsxRuntime.jsx(List, {
|
|
62
|
+
children: children.map((x, i) => /*#__PURE__*/jsxRuntime.jsx("li", {
|
|
63
|
+
children: x
|
|
64
|
+
}, i))
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
layout: ({
|
|
68
|
+
children,
|
|
69
|
+
layout
|
|
70
|
+
}) => {
|
|
71
|
+
return /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
72
|
+
style: {
|
|
73
|
+
display: 'grid',
|
|
74
|
+
gridTemplateColumns: layout.map(x => `${x}fr`).join(' ')
|
|
75
|
+
},
|
|
76
|
+
children: children.map((element, i) => /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
77
|
+
children: element
|
|
78
|
+
}, i))
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
function DocumentNode({
|
|
84
|
+
node: _node,
|
|
85
|
+
componentBlocks,
|
|
86
|
+
renderers
|
|
87
|
+
}) {
|
|
88
|
+
if (typeof _node.text === 'string') {
|
|
89
|
+
let child = /*#__PURE__*/jsxRuntime.jsx(react.Fragment, {
|
|
90
|
+
children: _node.text
|
|
91
|
+
});
|
|
92
|
+
Object.keys(renderers.inline).forEach(markName => {
|
|
93
|
+
if (markName !== 'link' && markName !== 'relationship' && _node[markName]) {
|
|
94
|
+
const Mark = renderers.inline[markName];
|
|
95
|
+
child = /*#__PURE__*/jsxRuntime.jsx(Mark, {
|
|
96
|
+
children: child
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
return child;
|
|
101
|
+
}
|
|
102
|
+
const node = _node;
|
|
103
|
+
const children = node.children.map((x, i) => /*#__PURE__*/jsxRuntime.jsx(DocumentNode, {
|
|
104
|
+
node: x,
|
|
105
|
+
componentBlocks: componentBlocks,
|
|
106
|
+
renderers: renderers
|
|
107
|
+
}, i));
|
|
108
|
+
switch (node.type) {
|
|
109
|
+
case 'blockquote':
|
|
110
|
+
{
|
|
111
|
+
return /*#__PURE__*/jsxRuntime.jsx(renderers.block.blockquote, {
|
|
112
|
+
children: children
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
case 'paragraph':
|
|
116
|
+
{
|
|
117
|
+
return /*#__PURE__*/jsxRuntime.jsx(renderers.block.paragraph, {
|
|
118
|
+
textAlign: node.textAlign,
|
|
119
|
+
children: children
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
case 'code':
|
|
123
|
+
{
|
|
124
|
+
if (node.children.length === 1 && node.children[0] && typeof node.children[0].text === 'string') {
|
|
125
|
+
return /*#__PURE__*/jsxRuntime.jsx(renderers.block.code, {
|
|
126
|
+
children: node.children[0].text
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
case 'layout':
|
|
132
|
+
{
|
|
133
|
+
return /*#__PURE__*/jsxRuntime.jsx(renderers.block.layout, {
|
|
134
|
+
layout: node.layout,
|
|
135
|
+
children: children
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
case 'divider':
|
|
139
|
+
{
|
|
140
|
+
return /*#__PURE__*/jsxRuntime.jsx(renderers.block.divider, {});
|
|
141
|
+
}
|
|
142
|
+
case 'heading':
|
|
143
|
+
{
|
|
144
|
+
return /*#__PURE__*/jsxRuntime.jsx(renderers.block.heading, {
|
|
145
|
+
textAlign: node.textAlign,
|
|
146
|
+
level: node.level,
|
|
147
|
+
children: children
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
case 'component-block':
|
|
151
|
+
{
|
|
152
|
+
const Comp = componentBlocks[node.component];
|
|
153
|
+
if (Comp) {
|
|
154
|
+
const props = createComponentBlockProps(node, children);
|
|
155
|
+
return /*#__PURE__*/jsxRuntime.jsx(renderers.block.block, {
|
|
156
|
+
children: /*#__PURE__*/jsxRuntime.jsx(Comp, {
|
|
157
|
+
...props
|
|
158
|
+
})
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
162
|
+
console.warn(`[@nixxie-cms/document-renderer] No component block named "${node.component}" was provided; ` + `rendering its children only. Pass it via the \`componentBlocks\` prop.`);
|
|
163
|
+
}
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
case 'ordered-list':
|
|
167
|
+
case 'unordered-list':
|
|
168
|
+
{
|
|
169
|
+
return /*#__PURE__*/jsxRuntime.jsx(renderers.block.list, {
|
|
170
|
+
children: children,
|
|
171
|
+
type: node.type === 'ordered-list' ? 'ordered' : 'unordered'
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
case 'relationship':
|
|
175
|
+
{
|
|
176
|
+
const data = node.data;
|
|
177
|
+
return /*#__PURE__*/jsxRuntime.jsx(renderers.inline.relationship, {
|
|
178
|
+
relationship: node.relationship,
|
|
179
|
+
data: data ? {
|
|
180
|
+
id: data.id,
|
|
181
|
+
label: data.label,
|
|
182
|
+
data: data.data
|
|
183
|
+
} : null
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
case 'link':
|
|
187
|
+
{
|
|
188
|
+
return /*#__PURE__*/jsxRuntime.jsx(renderers.inline.link, {
|
|
189
|
+
href: node.href,
|
|
190
|
+
children: children
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return /*#__PURE__*/jsxRuntime.jsx(react.Fragment, {
|
|
195
|
+
children: children
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
function set(obj, propPath, value) {
|
|
199
|
+
if (propPath.length === 1) {
|
|
200
|
+
obj[propPath[0]] = value;
|
|
201
|
+
} else {
|
|
202
|
+
const firstElement = propPath.shift();
|
|
203
|
+
set(obj[firstElement], propPath, value);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function createComponentBlockProps(node, children) {
|
|
207
|
+
const formProps = JSON.parse(JSON.stringify(node.props));
|
|
208
|
+
node.children.forEach((child, i) => {
|
|
209
|
+
if (child.propPath) {
|
|
210
|
+
const propPath = [...child.propPath];
|
|
211
|
+
set(formProps, propPath, children[i]);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
return formProps;
|
|
215
|
+
}
|
|
216
|
+
function DocumentRenderer(props) {
|
|
217
|
+
var _props$renderers, _props$renderers2;
|
|
218
|
+
const renderers = {
|
|
219
|
+
inline: {
|
|
220
|
+
...defaultRenderers.inline,
|
|
221
|
+
...((_props$renderers = props.renderers) === null || _props$renderers === void 0 ? void 0 : _props$renderers.inline)
|
|
222
|
+
},
|
|
223
|
+
block: {
|
|
224
|
+
...defaultRenderers.block,
|
|
225
|
+
...((_props$renderers2 = props.renderers) === null || _props$renderers2 === void 0 ? void 0 : _props$renderers2.block)
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const componentBlocks = props.componentBlocks || {};
|
|
229
|
+
return /*#__PURE__*/jsxRuntime.jsx(react.Fragment, {
|
|
230
|
+
children: props.document.map((x, i) => /*#__PURE__*/jsxRuntime.jsx(DocumentNode, {
|
|
231
|
+
node: x,
|
|
232
|
+
componentBlocks: componentBlocks,
|
|
233
|
+
renderers: renderers
|
|
234
|
+
}, i))
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
exports.DocumentRenderer = DocumentRenderer;
|
|
239
|
+
exports.defaultRenderers = defaultRenderers;
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { Fragment } from 'react';
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
|
|
4
|
+
const defaultRenderers = {
|
|
5
|
+
inline: {
|
|
6
|
+
bold: 'strong',
|
|
7
|
+
code: 'code',
|
|
8
|
+
keyboard: 'kbd',
|
|
9
|
+
strikethrough: 's',
|
|
10
|
+
italic: 'em',
|
|
11
|
+
link: 'a',
|
|
12
|
+
subscript: 'sub',
|
|
13
|
+
superscript: 'sup',
|
|
14
|
+
underline: 'u',
|
|
15
|
+
relationship: ({
|
|
16
|
+
data
|
|
17
|
+
}) => {
|
|
18
|
+
return /*#__PURE__*/jsx("span", {
|
|
19
|
+
children: (data === null || data === void 0 ? void 0 : data.label) || (data === null || data === void 0 ? void 0 : data.id)
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
block: {
|
|
24
|
+
block: 'div',
|
|
25
|
+
blockquote: 'blockquote',
|
|
26
|
+
paragraph: ({
|
|
27
|
+
children,
|
|
28
|
+
textAlign
|
|
29
|
+
}) => {
|
|
30
|
+
return /*#__PURE__*/jsx("p", {
|
|
31
|
+
style: {
|
|
32
|
+
textAlign
|
|
33
|
+
},
|
|
34
|
+
children: children
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
divider: 'hr',
|
|
38
|
+
heading: ({
|
|
39
|
+
level,
|
|
40
|
+
children,
|
|
41
|
+
textAlign
|
|
42
|
+
}) => {
|
|
43
|
+
const Heading = `h${level}`;
|
|
44
|
+
return /*#__PURE__*/jsx(Heading, {
|
|
45
|
+
style: {
|
|
46
|
+
textAlign
|
|
47
|
+
},
|
|
48
|
+
children: children
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
code: 'pre',
|
|
52
|
+
list: ({
|
|
53
|
+
children,
|
|
54
|
+
type
|
|
55
|
+
}) => {
|
|
56
|
+
const List = type === 'ordered' ? 'ol' : 'ul';
|
|
57
|
+
return /*#__PURE__*/jsx(List, {
|
|
58
|
+
children: children.map((x, i) => /*#__PURE__*/jsx("li", {
|
|
59
|
+
children: x
|
|
60
|
+
}, i))
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
layout: ({
|
|
64
|
+
children,
|
|
65
|
+
layout
|
|
66
|
+
}) => {
|
|
67
|
+
return /*#__PURE__*/jsx("div", {
|
|
68
|
+
style: {
|
|
69
|
+
display: 'grid',
|
|
70
|
+
gridTemplateColumns: layout.map(x => `${x}fr`).join(' ')
|
|
71
|
+
},
|
|
72
|
+
children: children.map((element, i) => /*#__PURE__*/jsx("div", {
|
|
73
|
+
children: element
|
|
74
|
+
}, i))
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
function DocumentNode({
|
|
80
|
+
node: _node,
|
|
81
|
+
componentBlocks,
|
|
82
|
+
renderers
|
|
83
|
+
}) {
|
|
84
|
+
if (typeof _node.text === 'string') {
|
|
85
|
+
let child = /*#__PURE__*/jsx(Fragment, {
|
|
86
|
+
children: _node.text
|
|
87
|
+
});
|
|
88
|
+
Object.keys(renderers.inline).forEach(markName => {
|
|
89
|
+
if (markName !== 'link' && markName !== 'relationship' && _node[markName]) {
|
|
90
|
+
const Mark = renderers.inline[markName];
|
|
91
|
+
child = /*#__PURE__*/jsx(Mark, {
|
|
92
|
+
children: child
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return child;
|
|
97
|
+
}
|
|
98
|
+
const node = _node;
|
|
99
|
+
const children = node.children.map((x, i) => /*#__PURE__*/jsx(DocumentNode, {
|
|
100
|
+
node: x,
|
|
101
|
+
componentBlocks: componentBlocks,
|
|
102
|
+
renderers: renderers
|
|
103
|
+
}, i));
|
|
104
|
+
switch (node.type) {
|
|
105
|
+
case 'blockquote':
|
|
106
|
+
{
|
|
107
|
+
return /*#__PURE__*/jsx(renderers.block.blockquote, {
|
|
108
|
+
children: children
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
case 'paragraph':
|
|
112
|
+
{
|
|
113
|
+
return /*#__PURE__*/jsx(renderers.block.paragraph, {
|
|
114
|
+
textAlign: node.textAlign,
|
|
115
|
+
children: children
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
case 'code':
|
|
119
|
+
{
|
|
120
|
+
if (node.children.length === 1 && node.children[0] && typeof node.children[0].text === 'string') {
|
|
121
|
+
return /*#__PURE__*/jsx(renderers.block.code, {
|
|
122
|
+
children: node.children[0].text
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
case 'layout':
|
|
128
|
+
{
|
|
129
|
+
return /*#__PURE__*/jsx(renderers.block.layout, {
|
|
130
|
+
layout: node.layout,
|
|
131
|
+
children: children
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
case 'divider':
|
|
135
|
+
{
|
|
136
|
+
return /*#__PURE__*/jsx(renderers.block.divider, {});
|
|
137
|
+
}
|
|
138
|
+
case 'heading':
|
|
139
|
+
{
|
|
140
|
+
return /*#__PURE__*/jsx(renderers.block.heading, {
|
|
141
|
+
textAlign: node.textAlign,
|
|
142
|
+
level: node.level,
|
|
143
|
+
children: children
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
case 'component-block':
|
|
147
|
+
{
|
|
148
|
+
const Comp = componentBlocks[node.component];
|
|
149
|
+
if (Comp) {
|
|
150
|
+
const props = createComponentBlockProps(node, children);
|
|
151
|
+
return /*#__PURE__*/jsx(renderers.block.block, {
|
|
152
|
+
children: /*#__PURE__*/jsx(Comp, {
|
|
153
|
+
...props
|
|
154
|
+
})
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
158
|
+
console.warn(`[@nixxie-cms/document-renderer] No component block named "${node.component}" was provided; ` + `rendering its children only. Pass it via the \`componentBlocks\` prop.`);
|
|
159
|
+
}
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
case 'ordered-list':
|
|
163
|
+
case 'unordered-list':
|
|
164
|
+
{
|
|
165
|
+
return /*#__PURE__*/jsx(renderers.block.list, {
|
|
166
|
+
children: children,
|
|
167
|
+
type: node.type === 'ordered-list' ? 'ordered' : 'unordered'
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
case 'relationship':
|
|
171
|
+
{
|
|
172
|
+
const data = node.data;
|
|
173
|
+
return /*#__PURE__*/jsx(renderers.inline.relationship, {
|
|
174
|
+
relationship: node.relationship,
|
|
175
|
+
data: data ? {
|
|
176
|
+
id: data.id,
|
|
177
|
+
label: data.label,
|
|
178
|
+
data: data.data
|
|
179
|
+
} : null
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
case 'link':
|
|
183
|
+
{
|
|
184
|
+
return /*#__PURE__*/jsx(renderers.inline.link, {
|
|
185
|
+
href: node.href,
|
|
186
|
+
children: children
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return /*#__PURE__*/jsx(Fragment, {
|
|
191
|
+
children: children
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
function set(obj, propPath, value) {
|
|
195
|
+
if (propPath.length === 1) {
|
|
196
|
+
obj[propPath[0]] = value;
|
|
197
|
+
} else {
|
|
198
|
+
const firstElement = propPath.shift();
|
|
199
|
+
set(obj[firstElement], propPath, value);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function createComponentBlockProps(node, children) {
|
|
203
|
+
const formProps = JSON.parse(JSON.stringify(node.props));
|
|
204
|
+
node.children.forEach((child, i) => {
|
|
205
|
+
if (child.propPath) {
|
|
206
|
+
const propPath = [...child.propPath];
|
|
207
|
+
set(formProps, propPath, children[i]);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
return formProps;
|
|
211
|
+
}
|
|
212
|
+
function DocumentRenderer(props) {
|
|
213
|
+
var _props$renderers, _props$renderers2;
|
|
214
|
+
const renderers = {
|
|
215
|
+
inline: {
|
|
216
|
+
...defaultRenderers.inline,
|
|
217
|
+
...((_props$renderers = props.renderers) === null || _props$renderers === void 0 ? void 0 : _props$renderers.inline)
|
|
218
|
+
},
|
|
219
|
+
block: {
|
|
220
|
+
...defaultRenderers.block,
|
|
221
|
+
...((_props$renderers2 = props.renderers) === null || _props$renderers2 === void 0 ? void 0 : _props$renderers2.block)
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
const componentBlocks = props.componentBlocks || {};
|
|
225
|
+
return /*#__PURE__*/jsx(Fragment, {
|
|
226
|
+
children: props.document.map((x, i) => /*#__PURE__*/jsx(DocumentNode, {
|
|
227
|
+
node: x,
|
|
228
|
+
componentBlocks: componentBlocks,
|
|
229
|
+
renderers: renderers
|
|
230
|
+
}, i))
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export { DocumentRenderer, defaultRenderers };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nixxie-cms/document-renderer",
|
|
3
3
|
"description": "NixxieCMS Document Renderer",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/nixxie-cms-document-renderer.cjs.js",
|
|
7
7
|
"module": "dist/nixxie-cms-document-renderer.esm.js",
|