@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,181 @@
1
+ import { Node, ExtensionAttribute, Mark as Mark$1, JSONContent, Extensions } from '@tiptap/core';
2
+ import { DOMOutputSpec, Mark, Node as Node$1 } from '@tiptap/pm/model';
3
+ import React from 'react';
4
+
5
+ /**
6
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
7
+ */
8
+ type MarkType<Type extends string = any, Attributes extends undefined | Record<string, any> = any> = {
9
+ type: Type;
10
+ attrs: Attributes;
11
+ };
12
+ /**
13
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
14
+ */
15
+ type NodeType<Type extends string = any, Attributes extends undefined | Record<string, any> = any, NodeMarkType extends MarkType = any, Content extends NodeType[] = any> = {
16
+ type: Type;
17
+ attrs: Attributes;
18
+ content?: Content;
19
+ marks?: NodeMarkType[];
20
+ text?: string;
21
+ };
22
+
23
+ /**
24
+ * Props for a node renderer
25
+ */
26
+ type NodeProps<TNodeType = any, TChildren = any> = {
27
+ /**
28
+ * The current node to render
29
+ */
30
+ node: TNodeType;
31
+ /**
32
+ * Unless the node is the root node, this will always be defined
33
+ */
34
+ parent?: TNodeType;
35
+ /**
36
+ * The children of the current node
37
+ */
38
+ children?: TChildren;
39
+ /**
40
+ * Render a child element
41
+ */
42
+ renderElement: (props: {
43
+ /**
44
+ * Tiptap JSON content to render
45
+ */
46
+ content: TNodeType;
47
+ /**
48
+ * The parent node of the current node
49
+ */
50
+ parent?: TNodeType;
51
+ }) => TChildren;
52
+ };
53
+ /**
54
+ * Props for a mark renderer
55
+ */
56
+ type MarkProps<TMarkType = any, TChildren = any, TNodeType = any> = {
57
+ /**
58
+ * The current mark to render
59
+ */
60
+ mark: TMarkType;
61
+ /**
62
+ * The children of the current mark
63
+ */
64
+ children?: TChildren;
65
+ /**
66
+ * The node the current mark is applied to
67
+ */
68
+ node: TNodeType;
69
+ /**
70
+ * The node the current mark is applied to
71
+ */
72
+ parent?: TNodeType;
73
+ };
74
+ type TiptapStaticRendererOptions<
75
+ /**
76
+ * The return type of the render function (e.g. React.ReactNode, string)
77
+ */
78
+ TReturnType,
79
+ /**
80
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
81
+ */
82
+ TMarkType extends {
83
+ type: any;
84
+ } = MarkType,
85
+ /**
86
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
87
+ */
88
+ TNodeType extends {
89
+ content?: {
90
+ forEach: (cb: (node: TNodeType) => void) => void;
91
+ };
92
+ marks?: readonly TMarkType[];
93
+ type: string | {
94
+ name: string;
95
+ };
96
+ } = NodeType,
97
+ /**
98
+ * A node renderer is a function that takes a node and its children and returns the rendered output
99
+ */
100
+ TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType,
101
+ /**
102
+ * A mark renderer is a function that takes a mark and its children and returns the rendered output
103
+ */
104
+ TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType> = {
105
+ /**
106
+ * Mapping of node types to react components
107
+ */
108
+ nodeMapping: Record<string, TNodeRender>;
109
+ /**
110
+ * Mapping of mark types to react components
111
+ */
112
+ markMapping: Record<string, TMarkRender>;
113
+ /**
114
+ * Component to render if a node type is not handled
115
+ */
116
+ unhandledNode?: TNodeRender;
117
+ /**
118
+ * Component to render if a mark type is not handled
119
+ */
120
+ unhandledMark?: TMarkRender;
121
+ };
122
+
123
+ type DomOutputSpecToElement<T> = (content: DOMOutputSpec) => (children?: T | T[]) => T;
124
+ /**
125
+ * This takes a NodeExtension and maps it to a React component
126
+ * @param extension The node extension to map to a React component
127
+ * @param extensionAttributes All available extension attributes
128
+ * @returns A tuple with the name of the extension and a React component that renders the extension
129
+ */
130
+ 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];
131
+ /**
132
+ * This takes a MarkExtension and maps it to a React component
133
+ * @param extension The mark extension to map to a React component
134
+ * @param extensionAttributes All available extension attributes
135
+ * @returns A tuple with the name of the extension and a React component that renders the extension
136
+ */
137
+ 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];
138
+ /**
139
+ * This function will statically render a Prosemirror Node to a target element type using the given extensions
140
+ * @param renderer The renderer to use to render the Prosemirror Node to the target element type
141
+ * @param domOutputSpecToElement A function that takes a Prosemirror DOMOutputSpec and returns a function that takes children and returns the target element type
142
+ * @param mapDefinedTypes An object with functions to map the doc and text types to the target element type
143
+ * @param content The Prosemirror Node to render
144
+ * @param extensions The extensions to use to render the Prosemirror Node
145
+ * @param options Additional options to pass to the renderer that can override the default behavior
146
+ * @returns The rendered target element type
147
+ */
148
+ declare function renderToElement<T>({ renderer, domOutputSpecToElement, mapDefinedTypes, content, extensions, options, }: {
149
+ renderer: (options: TiptapStaticRendererOptions<T, Mark, Node$1>) => (ctx: {
150
+ content: Node$1;
151
+ }) => T;
152
+ domOutputSpecToElement: DomOutputSpecToElement<T>;
153
+ mapDefinedTypes: {
154
+ doc: (props: NodeProps<Node$1, T | T[]>) => T;
155
+ text: (props: NodeProps<Node$1, T | T[]>) => T;
156
+ };
157
+ content: Node$1 | JSONContent;
158
+ extensions: Extensions;
159
+ options?: Partial<TiptapStaticRendererOptions<T, Mark, Node$1>>;
160
+ }): T;
161
+
162
+ /**
163
+ * Take a DOMOutputSpec and return a function that can render it to a React element
164
+ * @param content The DOMOutputSpec to convert to a React element
165
+ * @returns A function that can render the DOMOutputSpec to a React element
166
+ */
167
+ declare function domOutputSpecToReactElement(content: DOMOutputSpec, key?: number): (children?: React.ReactNode) => React.ReactNode;
168
+ /**
169
+ * This function will statically render a Prosemirror Node to a React component using the given extensions
170
+ * @param content The content to render to a React component
171
+ * @param extensions The extensions to use for rendering
172
+ * @param options The options to use for rendering
173
+ * @returns The React element that represents the rendered content
174
+ */
175
+ declare function renderToReactElement({ content, extensions, options, }: {
176
+ content: Node$1 | JSONContent;
177
+ extensions: Extensions;
178
+ options?: Partial<TiptapStaticRendererOptions<React.ReactNode, Mark, Node$1>>;
179
+ }): React.ReactNode;
180
+
181
+ export { type DomOutputSpecToElement, domOutputSpecToReactElement, mapMarkExtensionToReactNode, mapNodeExtensionToReactNode, renderToElement, renderToReactElement };