@tiptap/static-renderer 3.0.0-next.1 → 3.0.0-next.4

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.
Files changed (60) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +1 -1
  3. package/dist/index.cjs +573 -6
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.cts +307 -32
  6. package/dist/index.d.ts +307 -32
  7. package/dist/index.js +552 -2
  8. package/dist/index.js.map +1 -1
  9. package/dist/json/html-string/index.cjs +16 -5
  10. package/dist/json/html-string/index.cjs.map +1 -1
  11. package/dist/json/html-string/index.d.cts +18 -22
  12. package/dist/json/html-string/index.d.ts +18 -22
  13. package/dist/json/html-string/index.js +10 -1
  14. package/dist/json/html-string/index.js.map +1 -1
  15. package/dist/json/react/index.cjs +12 -2201
  16. package/dist/json/react/index.cjs.map +1 -1
  17. package/dist/json/react/index.d.cts +5 -22
  18. package/dist/json/react/index.d.ts +5 -22
  19. package/dist/json/react/index.js +9 -2221
  20. package/dist/json/react/index.js.map +1 -1
  21. package/dist/json/renderer.cjs.map +1 -1
  22. package/dist/json/renderer.d.cts +5 -21
  23. package/dist/json/renderer.d.ts +5 -21
  24. package/dist/json/renderer.js.map +1 -1
  25. package/dist/pm/html-string/index.cjs +22 -37
  26. package/dist/pm/html-string/index.cjs.map +1 -1
  27. package/dist/pm/html-string/index.d.cts +7 -24
  28. package/dist/pm/html-string/index.d.ts +7 -24
  29. package/dist/pm/html-string/index.js +19 -34
  30. package/dist/pm/html-string/index.js.map +1 -1
  31. package/dist/pm/markdown/index.cjs +473 -0
  32. package/dist/pm/markdown/index.cjs.map +1 -0
  33. package/dist/pm/markdown/index.d.cts +153 -0
  34. package/dist/pm/markdown/index.d.ts +153 -0
  35. package/dist/pm/markdown/index.js +449 -0
  36. package/dist/pm/markdown/index.js.map +1 -0
  37. package/dist/pm/react/index.cjs +41 -2230
  38. package/dist/pm/react/index.cjs.map +1 -1
  39. package/dist/pm/react/index.d.cts +5 -23
  40. package/dist/pm/react/index.d.ts +5 -23
  41. package/dist/pm/react/index.js +47 -2259
  42. package/dist/pm/react/index.js.map +1 -1
  43. package/package.json +27 -8
  44. package/src/helpers.ts +5 -16
  45. package/src/index.ts +5 -1
  46. package/src/json/html-string/string.ts +39 -13
  47. package/src/json/react/react.tsx +12 -15
  48. package/src/json/renderer.ts +50 -51
  49. package/src/pm/extensionRenderer.ts +16 -34
  50. package/src/pm/html-string/html-string.ts +29 -45
  51. package/src/pm/markdown/index.ts +2 -0
  52. package/src/pm/markdown/markdown.ts +142 -0
  53. package/src/pm/react/react.tsx +49 -30
  54. package/src/helpers.example.ts +0 -35
  55. package/src/json/html-string/string.example.ts +0 -46
  56. package/src/json/react/react.example.ts +0 -45
  57. package/src/pm/html-string/html-string.example.ts +0 -225
  58. package/src/pm/markdown/markdown.example.ts +0 -296
  59. package/src/pm/react/react.example.tsx +0 -306
  60. package/src/types.ts +0 -57
package/dist/index.d.ts CHANGED
@@ -1,52 +1,327 @@
1
- import { ExtensionAttribute } from '@tiptap/core';
1
+ import { NodeType, MarkType, ExtensionAttribute, Node, Mark as Mark$1, JSONContent, Extensions } from '@tiptap/core';
2
+ import React from 'react';
3
+ import { DOMOutputSpec, Mark, Node as Node$1 } from '@tiptap/pm/model';
2
4
 
3
5
  /**
4
- * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
6
+ * This function returns the attributes of a node or mark that are defined by the given extension attributes.
7
+ * @param nodeOrMark The node or mark to get the attributes from
8
+ * @param extensionAttributes The extension attributes to use
9
+ * @param onlyRenderedAttributes If true, only attributes that are rendered in the HTML are returned
10
+ */
11
+ declare function getAttributes(nodeOrMark: NodeType | MarkType, extensionAttributes: ExtensionAttribute[], onlyRenderedAttributes?: boolean): Record<string, any>;
12
+ /**
13
+ * This function returns the HTML attributes of a node or mark that are defined by the given extension attributes.
14
+ * @param nodeOrMark The node or mark to get the attributes from
15
+ * @param extensionAttributes The extension attributes to use
16
+ */
17
+ declare function getHTMLAttributes(nodeOrMark: NodeType | MarkType, extensionAttributes: ExtensionAttribute[]): Record<string, any>;
18
+
19
+ /**
20
+ * Props for a node renderer
5
21
  */
6
- type MarkType<Type extends string = any, Attributes extends undefined | Record<string, any> = any> = {
7
- type: Type;
8
- attrs: Attributes;
22
+ type NodeProps<TNodeType = any, TChildren = any> = {
23
+ /**
24
+ * The current node to render
25
+ */
26
+ node: TNodeType;
27
+ /**
28
+ * Unless the node is the root node, this will always be defined
29
+ */
30
+ parent?: TNodeType;
31
+ /**
32
+ * The children of the current node
33
+ */
34
+ children?: TChildren;
35
+ /**
36
+ * Render a child element
37
+ */
38
+ renderElement: (props: {
39
+ /**
40
+ * Tiptap JSON content to render
41
+ */
42
+ content: TNodeType;
43
+ /**
44
+ * The parent node of the current node
45
+ */
46
+ parent?: TNodeType;
47
+ }) => TChildren;
9
48
  };
10
49
  /**
11
- * A node type is either a JSON representation of a node or a Prosemirror node instance
50
+ * Props for a mark renderer
12
51
  */
13
- type NodeType<Type extends string = any, Attributes extends undefined | Record<string, any> = any, NodeMarkType extends MarkType = any, Content extends NodeType[] = any> = {
14
- type: Type;
15
- attrs: Attributes;
16
- content?: Content;
17
- marks?: NodeMarkType[];
18
- text?: string;
52
+ type MarkProps<TMarkType = any, TChildren = any, TNodeType = any> = {
53
+ /**
54
+ * The current mark to render
55
+ */
56
+ mark: TMarkType;
57
+ /**
58
+ * The children of the current mark
59
+ */
60
+ children?: TChildren;
61
+ /**
62
+ * The node the current mark is applied to
63
+ */
64
+ node: TNodeType;
65
+ /**
66
+ * The node the current mark is applied to
67
+ */
68
+ parent?: TNodeType;
19
69
  };
70
+ type TiptapStaticRendererOptions<
71
+ /**
72
+ * The return type of the render function (e.g. React.ReactNode, string)
73
+ */
74
+ TReturnType,
75
+ /**
76
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
77
+ */
78
+ TMarkType extends {
79
+ type: any;
80
+ } = MarkType,
20
81
  /**
21
- * A node type is either a JSON representation of a doc node or a Prosemirror doc node instance
82
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
22
83
  */
23
- type DocumentType<TNodeAttributes extends Record<string, any> = Record<string, any>, TContentType extends NodeType[] = NodeType[]> = NodeType<'doc', TNodeAttributes, never, TContentType>;
84
+ TNodeType extends {
85
+ content?: {
86
+ forEach: (cb: (node: TNodeType) => void) => void;
87
+ };
88
+ marks?: readonly TMarkType[];
89
+ type: string | {
90
+ name: string;
91
+ };
92
+ } = NodeType,
24
93
  /**
25
- * A node type is either a JSON representation of a text node or a Prosemirror text node instance
94
+ * A node renderer is a function that takes a node and its children and returns the rendered output
26
95
  */
27
- type TextType<TMarkType extends MarkType = MarkType> = {
28
- type: 'text';
29
- text: string;
30
- marks: TMarkType[];
96
+ TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType,
97
+ /**
98
+ * A mark renderer is a function that takes a mark and its children and returns the rendered output
99
+ */
100
+ TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType> = {
101
+ /**
102
+ * Mapping of node types to react components
103
+ */
104
+ nodeMapping: Record<string, NoInfer<TNodeRender>>;
105
+ /**
106
+ * Mapping of mark types to react components
107
+ */
108
+ markMapping: Record<string, NoInfer<TMarkRender>>;
109
+ /**
110
+ * Component to render if a node type is not handled
111
+ */
112
+ unhandledNode?: NoInfer<TNodeRender>;
113
+ /**
114
+ * Component to render if a mark type is not handled
115
+ */
116
+ unhandledMark?: NoInfer<TMarkRender>;
31
117
  };
32
118
  /**
33
- * Describes the output of a `renderHTML` function in prosemirror
34
- * @see https://prosemirror.net/docs/ref/#model.DOMOutputSpec
119
+ * Tiptap Static Renderer
120
+ * ----------------------
121
+ *
122
+ * This function is a basis to allow for different renderers to be created.
123
+ * Generic enough to be able to statically render Prosemirror JSON or Prosemirror Nodes.
124
+ *
125
+ * Using this function, you can create a renderer that takes a JSON representation of a Prosemirror document
126
+ * and renders it using a mapping of node types to React components or even to a string.
127
+ * This function is used as the basis to create the `reactRenderer` and `stringRenderer` functions.
128
+ */
129
+ declare function TiptapStaticRenderer<
130
+ /**
131
+ * The return type of the render function (e.g. React.ReactNode, string)
132
+ */
133
+ TReturnType,
134
+ /**
135
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
136
+ */
137
+ TMarkType extends {
138
+ type: string | {
139
+ name: string;
140
+ };
141
+ } = MarkType,
142
+ /**
143
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
144
+ */
145
+ TNodeType extends {
146
+ content?: {
147
+ forEach: (cb: (node: TNodeType) => void) => void;
148
+ };
149
+ marks?: readonly TMarkType[];
150
+ type: string | {
151
+ name: string;
152
+ };
153
+ } = NodeType,
154
+ /**
155
+ * A node renderer is a function that takes a node and its children and returns the rendered output
156
+ */
157
+ TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType,
158
+ /**
159
+ * A mark renderer is a function that takes a mark and its children and returns the rendered output
160
+ */
161
+ TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType>(
162
+ /**
163
+ * The function that actually renders the component
35
164
  */
36
- type DOMOutputSpecArray = [string] | [string, Record<string, any>] | [string, 0] | [string, Record<string, any>, 0] | [string, Record<string, any>, DOMOutputSpecArray | 0] | [string, DOMOutputSpecArray];
165
+ renderComponent: (ctx: {
166
+ component: TNodeRender;
167
+ props: NodeProps<TNodeType, TReturnType | TReturnType[]>;
168
+ } | {
169
+ component: TMarkRender;
170
+ props: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>;
171
+ }) => TReturnType, { nodeMapping, markMapping, unhandledNode, unhandledMark, }: TiptapStaticRendererOptions<TReturnType, TMarkType, TNodeType, TNodeRender, TMarkRender>): ({ content, parent, }: {
172
+ /**
173
+ * Tiptap JSON content to render
174
+ */
175
+ content: TNodeType;
176
+ /**
177
+ * The parent node of the current node
178
+ */
179
+ parent?: TNodeType;
180
+ }) => TReturnType;
37
181
 
182
+ declare function renderJSONContentToString<
38
183
  /**
39
- * This function returns the attributes of a node or mark that are defined by the given extension attributes.
40
- * @param nodeOrMark The node or mark to get the attributes from
41
- * @param extensionAttributes The extension attributes to use
42
- * @param onlyRenderedAttributes If true, only attributes that are rendered in the HTML are returned
184
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
43
185
  */
44
- declare function getAttributes(nodeOrMark: NodeType | MarkType, extensionAttributes: ExtensionAttribute[], onlyRenderedAttributes?: boolean): Record<string, any>;
186
+ TMarkType extends {
187
+ type: any;
188
+ } = MarkType,
45
189
  /**
46
- * This function returns the HTML attributes of a node or mark that are defined by the given extension attributes.
47
- * @param nodeOrMark The node or mark to get the attributes from
48
- * @param extensionAttributes The extension attributes to use
190
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
49
191
  */
50
- declare function getHTMLAttributes(nodeOrMark: NodeType | MarkType, extensionAttributes: ExtensionAttribute[]): Record<string, any>;
192
+ TNodeType extends {
193
+ content?: {
194
+ forEach: (cb: (node: TNodeType) => void) => void;
195
+ };
196
+ marks?: readonly TMarkType[];
197
+ type: string | {
198
+ name: string;
199
+ };
200
+ } = NodeType>(options: TiptapStaticRendererOptions<string, TMarkType, TNodeType>): ({ content, parent, }: {
201
+ content: TNodeType;
202
+ parent?: TNodeType | undefined;
203
+ }) => string;
204
+ /**
205
+ * Serialize the attributes of a node or mark to a string
206
+ * @param attrs The attributes to serialize
207
+ * @returns The serialized attributes as a string
208
+ */
209
+ declare function serializeAttrsToHTMLString(attrs: Record<string, any> | undefined | null): string;
210
+ /**
211
+ * Serialize the children of a node or mark to a string
212
+ * @param children The children to serialize
213
+ * @returns The serialized children as a string
214
+ */
215
+ declare function serializeChildrenToHTMLString(children?: string | string[]): string;
216
+
217
+ declare function renderJSONContentToReactElement<
218
+ /**
219
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
220
+ */
221
+ TMarkType extends {
222
+ type: any;
223
+ } = MarkType,
224
+ /**
225
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
226
+ */
227
+ TNodeType extends {
228
+ content?: {
229
+ forEach: (cb: (node: TNodeType) => void) => void;
230
+ };
231
+ marks?: readonly TMarkType[];
232
+ type: string | {
233
+ name: string;
234
+ };
235
+ } = NodeType>(options: TiptapStaticRendererOptions<React.ReactNode, TMarkType, TNodeType>): ({ content, parent, }: {
236
+ content: TNodeType;
237
+ parent?: TNodeType | undefined;
238
+ }) => React.ReactNode;
239
+
240
+ type DomOutputSpecToElement<T> = (content: DOMOutputSpec) => (children?: T | T[]) => T;
241
+ /**
242
+ * This takes a NodeExtension and maps it to a React component
243
+ * @param extension The node extension to map to a React component
244
+ * @param extensionAttributes All available extension attributes
245
+ * @returns A tuple with the name of the extension and a React component that renders the extension
246
+ */
247
+ declare function mapNodeExtensionToReactNode<T>(domOutputSpecToElement: DomOutputSpecToElement<T>, extension: Node, extensionAttributes: ExtensionAttribute[], options?: Partial<Pick<TiptapStaticRendererOptions<T, Mark, Node$1>, 'unhandledNode'>>): [string, (props: NodeProps<Node$1, T | T[]>) => T];
248
+ /**
249
+ * This takes a MarkExtension and maps it to a React component
250
+ * @param extension The mark extension to map to a React component
251
+ * @param extensionAttributes All available extension attributes
252
+ * @returns A tuple with the name of the extension and a React component that renders the extension
253
+ */
254
+ declare function mapMarkExtensionToReactNode<T>(domOutputSpecToElement: DomOutputSpecToElement<T>, extension: Mark$1, extensionAttributes: ExtensionAttribute[], options?: Partial<Pick<TiptapStaticRendererOptions<T, Mark, Node$1>, 'unhandledMark'>>): [string, (props: MarkProps<Mark, T | T[]>) => T];
255
+ /**
256
+ * This function will statically render a Prosemirror Node to a target element type using the given extensions
257
+ * @param renderer The renderer to use to render the Prosemirror Node to the target element type
258
+ * @param domOutputSpecToElement A function that takes a Prosemirror DOMOutputSpec and returns a function that takes children and returns the target element type
259
+ * @param mapDefinedTypes An object with functions to map the doc and text types to the target element type
260
+ * @param content The Prosemirror Node to render
261
+ * @param extensions The extensions to use to render the Prosemirror Node
262
+ * @param options Additional options to pass to the renderer that can override the default behavior
263
+ * @returns The rendered target element type
264
+ */
265
+ declare function renderToElement<T>({ renderer, domOutputSpecToElement, mapDefinedTypes, content, extensions, options, }: {
266
+ renderer: (options: TiptapStaticRendererOptions<T, Mark, Node$1>) => (ctx: {
267
+ content: Node$1;
268
+ }) => T;
269
+ domOutputSpecToElement: DomOutputSpecToElement<T>;
270
+ mapDefinedTypes: {
271
+ doc: (props: NodeProps<Node$1, T | T[]>) => T;
272
+ text: (props: NodeProps<Node$1, T | T[]>) => T;
273
+ };
274
+ content: Node$1 | JSONContent;
275
+ extensions: Extensions;
276
+ options?: Partial<TiptapStaticRendererOptions<T, Mark, Node$1>>;
277
+ }): T;
278
+
279
+ /**
280
+ * Take a DOMOutputSpec and return a function that can render it to a string
281
+ * @param content The DOMOutputSpec to convert to a string
282
+ * @returns A function that can render the DOMOutputSpec to a string
283
+ */
284
+ declare function domOutputSpecToHTMLString(content: DOMOutputSpec): (children?: string | string[]) => string;
285
+ /**
286
+ * This function will statically render a Prosemirror Node to HTML using the provided extensions and options
287
+ * @param content The content to render to HTML
288
+ * @param extensions The extensions to use for rendering
289
+ * @param options The options to use for rendering
290
+ * @returns The rendered HTML string
291
+ */
292
+ declare function renderToHTMLString({ content, extensions, options, }: {
293
+ content: Node$1 | JSONContent;
294
+ extensions: Extensions;
295
+ options?: Partial<TiptapStaticRendererOptions<string, Mark, Node$1>>;
296
+ }): string;
297
+
298
+ /**
299
+ * This code is just to show the flexibility of this renderer. We can potentially render content to any format we want.
300
+ * This is a simple example of how we can render content to markdown. This is not a full implementation of a markdown renderer.
301
+ */
302
+ declare function renderToMarkdown({ content, extensions, options, }: {
303
+ content: Node$1 | JSONContent;
304
+ extensions: Extensions;
305
+ options?: Partial<TiptapStaticRendererOptions<string, Mark, Node$1>>;
306
+ }): string;
307
+
308
+ /**
309
+ * Take a DOMOutputSpec and return a function that can render it to a React element
310
+ * @param content The DOMOutputSpec to convert to a React element
311
+ * @returns A function that can render the DOMOutputSpec to a React element
312
+ */
313
+ declare function domOutputSpecToReactElement(content: DOMOutputSpec, key?: number): (children?: React.ReactNode) => React.ReactNode;
314
+ /**
315
+ * This function will statically render a Prosemirror Node to a React component using the given extensions
316
+ * @param content The content to render to a React component
317
+ * @param extensions The extensions to use for rendering
318
+ * @param options The options to use for rendering
319
+ * @returns The React element that represents the rendered content
320
+ */
321
+ declare function renderToReactElement({ content, extensions, options, }: {
322
+ content: Node$1 | JSONContent;
323
+ extensions: Extensions;
324
+ options?: Partial<TiptapStaticRendererOptions<React.ReactNode, Mark, Node$1>>;
325
+ }): React.ReactNode;
51
326
 
52
- export { type DOMOutputSpecArray, type DocumentType, type MarkType, type NodeType, type TextType, getAttributes, getHTMLAttributes };
327
+ export { type DomOutputSpecToElement, type MarkProps, type NodeProps, TiptapStaticRenderer, type TiptapStaticRendererOptions, domOutputSpecToHTMLString, domOutputSpecToReactElement, getAttributes, getHTMLAttributes, mapMarkExtensionToReactNode, mapNodeExtensionToReactNode, renderJSONContentToReactElement, renderJSONContentToString, renderToElement, renderToHTMLString, renderToMarkdown, renderToReactElement, serializeAttrsToHTMLString, serializeChildrenToHTMLString };