@nixxie-cms/document-renderer 1.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nixxie International DMCC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ export * from "../src/index.js";
2
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibml4eGllLWNtcy1kb2N1bWVudC1yZW5kZXJlci5janMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9pbmRleC50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEifQ==
@@ -0,0 +1,16 @@
1
+ "use strict";
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
9
+
10
+ // this bit of code imports the require hook and registers it
11
+ let unregister = require("../../../node_modules/.pnpm/@preconstruct+hook@0.4.0/node_modules/@preconstruct/hook").___internalHook(typeof __dirname === 'undefined' ? undefined : __dirname, "../../..", "..");
12
+
13
+ // this re-exports the source file
14
+ module.exports = require("../src/index.tsx");
15
+
16
+ unregister();
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@nixxie-cms/document-renderer",
3
+ "description": "NixxieCMS Document Renderer",
4
+ "version": "1.0.0",
5
+ "license": "MIT",
6
+ "main": "dist/nixxie-cms-document-renderer.cjs.js",
7
+ "module": "dist/nixxie-cms-document-renderer.esm.js",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/nixxie-cms-document-renderer.cjs.js",
11
+ "module": "./dist/nixxie-cms-document-renderer.esm.js",
12
+ "default": "./dist/nixxie-cms-document-renderer.cjs.js"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "peerDependencies": {
17
+ "react": "^16.14.0 || 17 || 18 || 19"
18
+ },
19
+ "devDependencies": {
20
+ "react": "^19.2.4"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/nixxiecms/nixxie/tree/main/packages/document-renderer"
25
+ }
26
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,252 @@
1
+ import { Fragment, type JSX, type ReactElement, type ReactNode } from 'react'
2
+
3
+ export type Node = Element | Text
4
+
5
+ export type Element = {
6
+ children: Node[]
7
+ [key: string]: unknown
8
+ }
9
+
10
+ export type Text = {
11
+ text: string
12
+ [key: string]: unknown
13
+ }
14
+
15
+ type Mark =
16
+ | 'bold'
17
+ | 'italic'
18
+ | 'underline'
19
+ | 'strikethrough'
20
+ | 'code'
21
+ | 'superscript'
22
+ | 'subscript'
23
+ | 'keyboard'
24
+
25
+ type Component<Props> = (props: Props) => ReactNode
26
+
27
+ type OnlyChildrenComponent = Component<{ children: ReactNode }> | keyof JSX.IntrinsicElements
28
+
29
+ type MarkRenderers = { [Key in Mark]: OnlyChildrenComponent }
30
+
31
+ interface Renderers {
32
+ inline: {
33
+ link: Component<{ children: ReactNode; href: string }> | 'a'
34
+ relationship: Component<{
35
+ relationship: string
36
+ data: { id: string; label: string | undefined; data: Record<string, any> | undefined } | null
37
+ }>
38
+ } & MarkRenderers
39
+ block: {
40
+ block: OnlyChildrenComponent
41
+ paragraph: Component<{ children: ReactNode; textAlign: 'center' | 'end' | undefined }>
42
+ blockquote: OnlyChildrenComponent
43
+ code: Component<{ children: string }> | keyof JSX.IntrinsicElements
44
+ layout: Component<{ layout: [number, ...number[]]; children: ReactNode[] }>
45
+ divider: Component<unknown> | keyof JSX.IntrinsicElements
46
+ heading: Component<{
47
+ level: 1 | 2 | 3 | 4 | 5 | 6
48
+ children: ReactNode
49
+ textAlign: 'center' | 'end' | undefined
50
+ }>
51
+ list: Component<{ type: 'ordered' | 'unordered'; children: ReactNode[] }>
52
+ }
53
+ }
54
+
55
+ export const defaultRenderers: Renderers = {
56
+ inline: {
57
+ bold: 'strong',
58
+ code: 'code',
59
+ keyboard: 'kbd',
60
+ strikethrough: 's',
61
+ italic: 'em',
62
+ link: 'a',
63
+ subscript: 'sub',
64
+ superscript: 'sup',
65
+ underline: 'u',
66
+ relationship: ({ data }) => {
67
+ return <span>{data?.label || data?.id}</span>
68
+ },
69
+ },
70
+ block: {
71
+ block: 'div',
72
+ blockquote: 'blockquote',
73
+ paragraph: ({ children, textAlign }) => {
74
+ return <p style={{ textAlign }}>{children}</p>
75
+ },
76
+ divider: 'hr',
77
+ heading: ({ level, children, textAlign }) => {
78
+ const Heading = `h${level}` as 'h1'
79
+ return <Heading style={{ textAlign }} children={children} />
80
+ },
81
+ code: 'pre',
82
+ list: ({ children, type }) => {
83
+ const List = type === 'ordered' ? 'ol' : 'ul'
84
+ return (
85
+ <List>
86
+ {children.map((x, i) => (
87
+ <li key={i}>{x}</li>
88
+ ))}
89
+ </List>
90
+ )
91
+ },
92
+ layout: ({ children, layout }) => {
93
+ return (
94
+ <div
95
+ style={{
96
+ display: 'grid',
97
+ gridTemplateColumns: layout.map(x => `${x}fr`).join(' '),
98
+ }}
99
+ >
100
+ {children.map((element, i) => (
101
+ <div key={i}>{element}</div>
102
+ ))}
103
+ </div>
104
+ )
105
+ },
106
+ },
107
+ }
108
+
109
+ function DocumentNode({
110
+ node: _node,
111
+ componentBlocks,
112
+ renderers,
113
+ }: {
114
+ node: Element | Text
115
+ renderers: Renderers
116
+ // TODO: allow inferring from the component blocks
117
+ componentBlocks: Record<string, Component<any>>
118
+ }): ReactElement {
119
+ if (typeof _node.text === 'string') {
120
+ let child = <Fragment>{_node.text}</Fragment>
121
+ ;(Object.keys(renderers.inline) as (keyof typeof renderers.inline)[]).forEach(markName => {
122
+ if (markName !== 'link' && markName !== 'relationship' && _node[markName]) {
123
+ const Mark = renderers.inline[markName]
124
+ child = <Mark>{child}</Mark>
125
+ }
126
+ })
127
+
128
+ return child
129
+ }
130
+ const node = _node as Element
131
+ const children = node.children.map((x, i) => (
132
+ <DocumentNode node={x} componentBlocks={componentBlocks} renderers={renderers} key={i} />
133
+ ))
134
+ switch (node.type as string) {
135
+ case 'blockquote': {
136
+ return <renderers.block.blockquote children={children} />
137
+ }
138
+ case 'paragraph': {
139
+ return <renderers.block.paragraph textAlign={node.textAlign as any} children={children} />
140
+ }
141
+ case 'code': {
142
+ if (
143
+ node.children.length === 1 &&
144
+ node.children[0] &&
145
+ typeof node.children[0].text === 'string'
146
+ ) {
147
+ return <renderers.block.code>{node.children[0].text}</renderers.block.code>
148
+ }
149
+ break
150
+ }
151
+ case 'layout': {
152
+ return <renderers.block.layout layout={node.layout as any} children={children} />
153
+ }
154
+ case 'divider': {
155
+ return <renderers.block.divider />
156
+ }
157
+ case 'heading': {
158
+ return (
159
+ <renderers.block.heading
160
+ textAlign={node.textAlign as any}
161
+ level={node.level as any}
162
+ children={children}
163
+ />
164
+ )
165
+ }
166
+ case 'component-block': {
167
+ const Comp = componentBlocks[node.component as string]
168
+ if (Comp) {
169
+ const props = createComponentBlockProps(node, children)
170
+ return (
171
+ <renderers.block.block>
172
+ <Comp {...props} />
173
+ </renderers.block.block>
174
+ )
175
+ }
176
+ if (process.env.NODE_ENV !== 'production') {
177
+ console.warn(
178
+ `[@nixxie-cms/document-renderer] No component block named "${node.component}" was provided; ` +
179
+ `rendering its children only. Pass it via the \`componentBlocks\` prop.`
180
+ )
181
+ }
182
+ break
183
+ }
184
+ case 'ordered-list':
185
+ case 'unordered-list': {
186
+ return (
187
+ <renderers.block.list
188
+ children={children}
189
+ type={node.type === 'ordered-list' ? 'ordered' : 'unordered'}
190
+ />
191
+ )
192
+ }
193
+ case 'relationship': {
194
+ const data = node.data as any
195
+ return (
196
+ <renderers.inline.relationship
197
+ relationship={node.relationship as string}
198
+ data={data ? { id: data.id, label: data.label, data: data.data } : null}
199
+ />
200
+ )
201
+ }
202
+ case 'link': {
203
+ return <renderers.inline.link href={node.href as string}>{children}</renderers.inline.link>
204
+ }
205
+ }
206
+ return <Fragment>{children}</Fragment>
207
+ }
208
+
209
+ function set(obj: Record<string, any>, propPath: (string | number)[], value: any) {
210
+ if (propPath.length === 1) {
211
+ obj[propPath[0]] = value
212
+ } else {
213
+ const firstElement = propPath.shift()!
214
+ set(obj[firstElement], propPath, value)
215
+ }
216
+ }
217
+
218
+ function createComponentBlockProps(node: Element, children: ReactElement[]) {
219
+ const formProps = JSON.parse(JSON.stringify(node.props))
220
+ node.children.forEach((child, i) => {
221
+ if (child.propPath) {
222
+ const propPath = [...(child.propPath as any)]
223
+ set(formProps, propPath, children[i])
224
+ }
225
+ })
226
+ return formProps
227
+ }
228
+
229
+ export type DocumentRendererProps<
230
+ ComponentBlocks extends Record<string, Component<any>> = Record<string, Component<any>>,
231
+ > = {
232
+ document: Element[]
233
+ renderers?: { inline?: Partial<Renderers['inline']>; block?: Partial<Renderers['block']> }
234
+ componentBlocks?: ComponentBlocks
235
+ }
236
+
237
+ export function DocumentRenderer<ComponentBlocks extends Record<string, Component<any>>>(
238
+ props: DocumentRendererProps<ComponentBlocks>
239
+ ) {
240
+ const renderers = {
241
+ inline: { ...defaultRenderers.inline, ...props.renderers?.inline },
242
+ block: { ...defaultRenderers.block, ...props.renderers?.block },
243
+ }
244
+ const componentBlocks = props.componentBlocks || {}
245
+ return (
246
+ <Fragment>
247
+ {props.document.map((x, i) => (
248
+ <DocumentNode node={x} componentBlocks={componentBlocks} renderers={renderers} key={i} />
249
+ ))}
250
+ </Fragment>
251
+ )
252
+ }