@tiptap/static-renderer 3.0.0-next.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.
Files changed (57) hide show
  1. package/README.md +18 -0
  2. package/dist/index.cjs +62 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.cts +52 -0
  5. package/dist/index.d.ts +52 -0
  6. package/dist/index.js +34 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/json/html-string/index.cjs +100 -0
  9. package/dist/json/html-string/index.cjs.map +1 -0
  10. package/dist/json/html-string/index.d.cts +205 -0
  11. package/dist/json/html-string/index.d.ts +205 -0
  12. package/dist/json/html-string/index.js +72 -0
  13. package/dist/json/html-string/index.js.map +1 -0
  14. package/dist/json/react/index.cjs +2306 -0
  15. package/dist/json/react/index.cjs.map +1 -0
  16. package/dist/json/react/index.d.cts +207 -0
  17. package/dist/json/react/index.d.ts +207 -0
  18. package/dist/json/react/index.js +2291 -0
  19. package/dist/json/react/index.js.map +1 -0
  20. package/dist/json/renderer.cjs +89 -0
  21. package/dist/json/renderer.cjs.map +1 -0
  22. package/dist/json/renderer.d.cts +182 -0
  23. package/dist/json/renderer.d.ts +182 -0
  24. package/dist/json/renderer.js +64 -0
  25. package/dist/json/renderer.js.map +1 -0
  26. package/dist/pm/html-string/index.cjs +359 -0
  27. package/dist/pm/html-string/index.cjs.map +1 -0
  28. package/dist/pm/html-string/index.d.cts +192 -0
  29. package/dist/pm/html-string/index.d.ts +192 -0
  30. package/dist/pm/html-string/index.js +332 -0
  31. package/dist/pm/html-string/index.js.map +1 -0
  32. package/dist/pm/react/index.cjs +2588 -0
  33. package/dist/pm/react/index.cjs.map +1 -0
  34. package/dist/pm/react/index.d.cts +181 -0
  35. package/dist/pm/react/index.d.ts +181 -0
  36. package/dist/pm/react/index.js +2576 -0
  37. package/dist/pm/react/index.js.map +1 -0
  38. package/package.json +82 -0
  39. package/src/helpers.example.ts +35 -0
  40. package/src/helpers.ts +65 -0
  41. package/src/index.ts +2 -0
  42. package/src/json/html-string/index.ts +2 -0
  43. package/src/json/html-string/string.example.ts +46 -0
  44. package/src/json/html-string/string.ts +22 -0
  45. package/src/json/react/index.ts +2 -0
  46. package/src/json/react/react.example.ts +45 -0
  47. package/src/json/react/react.tsx +35 -0
  48. package/src/json/renderer.ts +242 -0
  49. package/src/pm/extensionRenderer.ts +230 -0
  50. package/src/pm/html-string/html-string.example.ts +225 -0
  51. package/src/pm/html-string/html-string.ts +121 -0
  52. package/src/pm/html-string/index.ts +2 -0
  53. package/src/pm/markdown/markdown.example.ts +296 -0
  54. package/src/pm/react/index.ts +2 -0
  55. package/src/pm/react/react.example.tsx +306 -0
  56. package/src/pm/react/react.tsx +133 -0
  57. package/src/types.ts +57 -0
@@ -0,0 +1,207 @@
1
+ import React from 'react';
2
+
3
+ /**
4
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
5
+ */
6
+ type MarkType<Type extends string = any, Attributes extends undefined | Record<string, any> = any> = {
7
+ type: Type;
8
+ attrs: Attributes;
9
+ };
10
+ /**
11
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
12
+ */
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;
19
+ };
20
+
21
+ /**
22
+ * Props for a node renderer
23
+ */
24
+ type NodeProps<TNodeType = any, TChildren = any> = {
25
+ /**
26
+ * The current node to render
27
+ */
28
+ node: TNodeType;
29
+ /**
30
+ * Unless the node is the root node, this will always be defined
31
+ */
32
+ parent?: TNodeType;
33
+ /**
34
+ * The children of the current node
35
+ */
36
+ children?: TChildren;
37
+ /**
38
+ * Render a child element
39
+ */
40
+ renderElement: (props: {
41
+ /**
42
+ * Tiptap JSON content to render
43
+ */
44
+ content: TNodeType;
45
+ /**
46
+ * The parent node of the current node
47
+ */
48
+ parent?: TNodeType;
49
+ }) => TChildren;
50
+ };
51
+ /**
52
+ * Props for a mark renderer
53
+ */
54
+ type MarkProps<TMarkType = any, TChildren = any, TNodeType = any> = {
55
+ /**
56
+ * The current mark to render
57
+ */
58
+ mark: TMarkType;
59
+ /**
60
+ * The children of the current mark
61
+ */
62
+ children?: TChildren;
63
+ /**
64
+ * The node the current mark is applied to
65
+ */
66
+ node: TNodeType;
67
+ /**
68
+ * The node the current mark is applied to
69
+ */
70
+ parent?: TNodeType;
71
+ };
72
+ type TiptapStaticRendererOptions<
73
+ /**
74
+ * The return type of the render function (e.g. React.ReactNode, string)
75
+ */
76
+ TReturnType,
77
+ /**
78
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
79
+ */
80
+ TMarkType extends {
81
+ type: any;
82
+ } = MarkType,
83
+ /**
84
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
85
+ */
86
+ TNodeType extends {
87
+ content?: {
88
+ forEach: (cb: (node: TNodeType) => void) => void;
89
+ };
90
+ marks?: readonly TMarkType[];
91
+ type: string | {
92
+ name: string;
93
+ };
94
+ } = NodeType,
95
+ /**
96
+ * A node renderer is a function that takes a node and its children and returns the rendered output
97
+ */
98
+ TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType,
99
+ /**
100
+ * A mark renderer is a function that takes a mark and its children and returns the rendered output
101
+ */
102
+ TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType> = {
103
+ /**
104
+ * Mapping of node types to react components
105
+ */
106
+ nodeMapping: Record<string, TNodeRender>;
107
+ /**
108
+ * Mapping of mark types to react components
109
+ */
110
+ markMapping: Record<string, TMarkRender>;
111
+ /**
112
+ * Component to render if a node type is not handled
113
+ */
114
+ unhandledNode?: TNodeRender;
115
+ /**
116
+ * Component to render if a mark type is not handled
117
+ */
118
+ unhandledMark?: TMarkRender;
119
+ };
120
+ /**
121
+ * Tiptap Static Renderer
122
+ * ----------------------
123
+ *
124
+ * This function is a basis to allow for different renderers to be created.
125
+ * Generic enough to be able to statically render Prosemirror JSON or Prosemirror Nodes.
126
+ *
127
+ * Using this function, you can create a renderer that takes a JSON representation of a Prosemirror document
128
+ * and renders it using a mapping of node types to React components or even to a string.
129
+ * This function is used as the basis to create the `reactRenderer` and `stringRenderer` functions.
130
+ */
131
+ declare function TiptapStaticRenderer<
132
+ /**
133
+ * The return type of the render function (e.g. React.ReactNode, string)
134
+ */
135
+ TReturnType,
136
+ /**
137
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
138
+ */
139
+ TMarkType extends {
140
+ type: string | {
141
+ name: string;
142
+ };
143
+ } = MarkType,
144
+ /**
145
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
146
+ */
147
+ TNodeType extends {
148
+ content?: {
149
+ forEach: (cb: (node: TNodeType) => void) => void;
150
+ };
151
+ marks?: readonly TMarkType[];
152
+ type: string | {
153
+ name: string;
154
+ };
155
+ } = NodeType,
156
+ /**
157
+ * A node renderer is a function that takes a node and its children and returns the rendered output
158
+ */
159
+ TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType,
160
+ /**
161
+ * A mark renderer is a function that takes a mark and its children and returns the rendered output
162
+ */
163
+ TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType>(
164
+ /**
165
+ * The function that actually renders the component
166
+ */
167
+ renderComponent: (ctx: {
168
+ component: TNodeRender;
169
+ props: NodeProps<TNodeType, TReturnType | TReturnType[]>;
170
+ } | {
171
+ component: TMarkRender;
172
+ props: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>;
173
+ }) => TReturnType, { nodeMapping, markMapping, unhandledNode, unhandledMark, }: TiptapStaticRendererOptions<TReturnType, TMarkType, TNodeType, TNodeRender, TMarkRender>): ({ content, parent, }: {
174
+ /**
175
+ * Tiptap JSON content to render
176
+ */
177
+ content: TNodeType;
178
+ /**
179
+ * The parent node of the current node
180
+ */
181
+ parent?: TNodeType;
182
+ }) => TReturnType;
183
+
184
+ declare function renderJSONContentToReactElement<
185
+ /**
186
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
187
+ */
188
+ TMarkType extends {
189
+ type: any;
190
+ } = MarkType,
191
+ /**
192
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
193
+ */
194
+ TNodeType extends {
195
+ content?: {
196
+ forEach: (cb: (node: TNodeType) => void) => void;
197
+ };
198
+ marks?: readonly TMarkType[];
199
+ type: string | {
200
+ name: string;
201
+ };
202
+ } = NodeType>(options: TiptapStaticRendererOptions<React.ReactNode, TMarkType, TNodeType>): ({ content, parent, }: {
203
+ content: TNodeType;
204
+ parent?: TNodeType | undefined;
205
+ }) => React.ReactNode;
206
+
207
+ export { type MarkProps, type NodeProps, TiptapStaticRenderer, type TiptapStaticRendererOptions, renderJSONContentToReactElement };