@tiptap/static-renderer 3.0.0-beta.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.
Files changed (59) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +18 -0
  3. package/dist/index.cjs +629 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +327 -0
  6. package/dist/index.d.ts +327 -0
  7. package/dist/index.js +584 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/json/html-string/index.cjs +111 -0
  10. package/dist/json/html-string/index.cjs.map +1 -0
  11. package/dist/json/html-string/index.d.cts +201 -0
  12. package/dist/json/html-string/index.d.ts +201 -0
  13. package/dist/json/html-string/index.js +81 -0
  14. package/dist/json/html-string/index.js.map +1 -0
  15. package/dist/json/react/index.cjs +117 -0
  16. package/dist/json/react/index.cjs.map +1 -0
  17. package/dist/json/react/index.d.cts +190 -0
  18. package/dist/json/react/index.d.ts +190 -0
  19. package/dist/json/react/index.js +79 -0
  20. package/dist/json/react/index.js.map +1 -0
  21. package/dist/json/renderer.cjs +89 -0
  22. package/dist/json/renderer.cjs.map +1 -0
  23. package/dist/json/renderer.d.cts +166 -0
  24. package/dist/json/renderer.d.ts +166 -0
  25. package/dist/json/renderer.js +64 -0
  26. package/dist/json/renderer.js.map +1 -0
  27. package/dist/pm/html-string/index.cjs +344 -0
  28. package/dist/pm/html-string/index.cjs.map +1 -0
  29. package/dist/pm/html-string/index.d.cts +175 -0
  30. package/dist/pm/html-string/index.d.ts +175 -0
  31. package/dist/pm/html-string/index.js +317 -0
  32. package/dist/pm/html-string/index.js.map +1 -0
  33. package/dist/pm/markdown/index.cjs +473 -0
  34. package/dist/pm/markdown/index.cjs.map +1 -0
  35. package/dist/pm/markdown/index.d.cts +153 -0
  36. package/dist/pm/markdown/index.d.ts +153 -0
  37. package/dist/pm/markdown/index.js +449 -0
  38. package/dist/pm/markdown/index.js.map +1 -0
  39. package/dist/pm/react/index.cjs +399 -0
  40. package/dist/pm/react/index.cjs.map +1 -0
  41. package/dist/pm/react/index.d.cts +163 -0
  42. package/dist/pm/react/index.d.ts +163 -0
  43. package/dist/pm/react/index.js +364 -0
  44. package/dist/pm/react/index.js.map +1 -0
  45. package/package.json +101 -0
  46. package/src/helpers.ts +54 -0
  47. package/src/index.ts +6 -0
  48. package/src/json/html-string/index.ts +2 -0
  49. package/src/json/html-string/string.ts +49 -0
  50. package/src/json/react/index.ts +2 -0
  51. package/src/json/react/react.ts +33 -0
  52. package/src/json/renderer.ts +241 -0
  53. package/src/pm/extensionRenderer.ts +215 -0
  54. package/src/pm/html-string/html-string.ts +105 -0
  55. package/src/pm/html-string/index.ts +2 -0
  56. package/src/pm/markdown/index.ts +2 -0
  57. package/src/pm/markdown/markdown.ts +142 -0
  58. package/src/pm/react/index.ts +2 -0
  59. package/src/pm/react/react.ts +152 -0
@@ -0,0 +1,153 @@
1
+ import { MarkType, NodeType, Node, ExtensionAttribute, Mark as Mark$1, JSONContent, Extensions } from '@tiptap/core';
2
+ import { DOMOutputSpec, Mark, Node as Node$1 } from '@tiptap/pm/model';
3
+
4
+ /**
5
+ * Props for a node renderer
6
+ */
7
+ type NodeProps<TNodeType = any, TChildren = any> = {
8
+ /**
9
+ * The current node to render
10
+ */
11
+ node: TNodeType;
12
+ /**
13
+ * Unless the node is the root node, this will always be defined
14
+ */
15
+ parent?: TNodeType;
16
+ /**
17
+ * The children of the current node
18
+ */
19
+ children?: TChildren;
20
+ /**
21
+ * Render a child element
22
+ */
23
+ renderElement: (props: {
24
+ /**
25
+ * Tiptap JSON content to render
26
+ */
27
+ content: TNodeType;
28
+ /**
29
+ * The parent node of the current node
30
+ */
31
+ parent?: TNodeType;
32
+ }) => TChildren;
33
+ };
34
+ /**
35
+ * Props for a mark renderer
36
+ */
37
+ type MarkProps<TMarkType = any, TChildren = any, TNodeType = any> = {
38
+ /**
39
+ * The current mark to render
40
+ */
41
+ mark: TMarkType;
42
+ /**
43
+ * The children of the current mark
44
+ */
45
+ children?: TChildren;
46
+ /**
47
+ * The node the current mark is applied to
48
+ */
49
+ node: TNodeType;
50
+ /**
51
+ * The node the current mark is applied to
52
+ */
53
+ parent?: TNodeType;
54
+ };
55
+ type TiptapStaticRendererOptions<
56
+ /**
57
+ * The return type of the render function (e.g. React.ReactNode, string)
58
+ */
59
+ TReturnType,
60
+ /**
61
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
62
+ */
63
+ TMarkType extends {
64
+ type: any;
65
+ } = MarkType,
66
+ /**
67
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
68
+ */
69
+ TNodeType extends {
70
+ content?: {
71
+ forEach: (cb: (node: TNodeType) => void) => void;
72
+ };
73
+ marks?: readonly TMarkType[];
74
+ type: string | {
75
+ name: string;
76
+ };
77
+ } = NodeType,
78
+ /**
79
+ * A node renderer is a function that takes a node and its children and returns the rendered output
80
+ */
81
+ TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType,
82
+ /**
83
+ * A mark renderer is a function that takes a mark and its children and returns the rendered output
84
+ */
85
+ TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType> = {
86
+ /**
87
+ * Mapping of node types to react components
88
+ */
89
+ nodeMapping: Record<string, NoInfer<TNodeRender>>;
90
+ /**
91
+ * Mapping of mark types to react components
92
+ */
93
+ markMapping: Record<string, NoInfer<TMarkRender>>;
94
+ /**
95
+ * Component to render if a node type is not handled
96
+ */
97
+ unhandledNode?: NoInfer<TNodeRender>;
98
+ /**
99
+ * Component to render if a mark type is not handled
100
+ */
101
+ unhandledMark?: NoInfer<TMarkRender>;
102
+ };
103
+
104
+ type DomOutputSpecToElement<T> = (content: DOMOutputSpec) => (children?: T | T[]) => T;
105
+ /**
106
+ * This takes a NodeExtension and maps it to a React component
107
+ * @param extension The node extension to map to a React component
108
+ * @param extensionAttributes All available extension attributes
109
+ * @returns A tuple with the name of the extension and a React component that renders the extension
110
+ */
111
+ 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];
112
+ /**
113
+ * This takes a MarkExtension and maps it to a React component
114
+ * @param extension The mark extension to map to a React component
115
+ * @param extensionAttributes All available extension attributes
116
+ * @returns A tuple with the name of the extension and a React component that renders the extension
117
+ */
118
+ 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];
119
+ /**
120
+ * This function will statically render a Prosemirror Node to a target element type using the given extensions
121
+ * @param renderer The renderer to use to render the Prosemirror Node to the target element type
122
+ * @param domOutputSpecToElement A function that takes a Prosemirror DOMOutputSpec and returns a function that takes children and returns the target element type
123
+ * @param mapDefinedTypes An object with functions to map the doc and text types to the target element type
124
+ * @param content The Prosemirror Node to render
125
+ * @param extensions The extensions to use to render the Prosemirror Node
126
+ * @param options Additional options to pass to the renderer that can override the default behavior
127
+ * @returns The rendered target element type
128
+ */
129
+ declare function renderToElement<T>({ renderer, domOutputSpecToElement, mapDefinedTypes, content, extensions, options, }: {
130
+ renderer: (options: TiptapStaticRendererOptions<T, Mark, Node$1>) => (ctx: {
131
+ content: Node$1;
132
+ }) => T;
133
+ domOutputSpecToElement: DomOutputSpecToElement<T>;
134
+ mapDefinedTypes: {
135
+ doc: (props: NodeProps<Node$1, T | T[]>) => T;
136
+ text: (props: NodeProps<Node$1, T | T[]>) => T;
137
+ };
138
+ content: Node$1 | JSONContent;
139
+ extensions: Extensions;
140
+ options?: Partial<TiptapStaticRendererOptions<T, Mark, Node$1>>;
141
+ }): T;
142
+
143
+ /**
144
+ * This code is just to show the flexibility of this renderer. We can potentially render content to any format we want.
145
+ * This is a simple example of how we can render content to markdown. This is not a full implementation of a markdown renderer.
146
+ */
147
+ declare function renderToMarkdown({ content, extensions, options, }: {
148
+ content: Node$1 | JSONContent;
149
+ extensions: Extensions;
150
+ options?: Partial<TiptapStaticRendererOptions<string, Mark, Node$1>>;
151
+ }): string;
152
+
153
+ export { type DomOutputSpecToElement, mapMarkExtensionToReactNode, mapNodeExtensionToReactNode, renderToElement, renderToMarkdown };
@@ -0,0 +1,153 @@
1
+ import { MarkType, NodeType, Node, ExtensionAttribute, Mark as Mark$1, JSONContent, Extensions } from '@tiptap/core';
2
+ import { DOMOutputSpec, Mark, Node as Node$1 } from '@tiptap/pm/model';
3
+
4
+ /**
5
+ * Props for a node renderer
6
+ */
7
+ type NodeProps<TNodeType = any, TChildren = any> = {
8
+ /**
9
+ * The current node to render
10
+ */
11
+ node: TNodeType;
12
+ /**
13
+ * Unless the node is the root node, this will always be defined
14
+ */
15
+ parent?: TNodeType;
16
+ /**
17
+ * The children of the current node
18
+ */
19
+ children?: TChildren;
20
+ /**
21
+ * Render a child element
22
+ */
23
+ renderElement: (props: {
24
+ /**
25
+ * Tiptap JSON content to render
26
+ */
27
+ content: TNodeType;
28
+ /**
29
+ * The parent node of the current node
30
+ */
31
+ parent?: TNodeType;
32
+ }) => TChildren;
33
+ };
34
+ /**
35
+ * Props for a mark renderer
36
+ */
37
+ type MarkProps<TMarkType = any, TChildren = any, TNodeType = any> = {
38
+ /**
39
+ * The current mark to render
40
+ */
41
+ mark: TMarkType;
42
+ /**
43
+ * The children of the current mark
44
+ */
45
+ children?: TChildren;
46
+ /**
47
+ * The node the current mark is applied to
48
+ */
49
+ node: TNodeType;
50
+ /**
51
+ * The node the current mark is applied to
52
+ */
53
+ parent?: TNodeType;
54
+ };
55
+ type TiptapStaticRendererOptions<
56
+ /**
57
+ * The return type of the render function (e.g. React.ReactNode, string)
58
+ */
59
+ TReturnType,
60
+ /**
61
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
62
+ */
63
+ TMarkType extends {
64
+ type: any;
65
+ } = MarkType,
66
+ /**
67
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
68
+ */
69
+ TNodeType extends {
70
+ content?: {
71
+ forEach: (cb: (node: TNodeType) => void) => void;
72
+ };
73
+ marks?: readonly TMarkType[];
74
+ type: string | {
75
+ name: string;
76
+ };
77
+ } = NodeType,
78
+ /**
79
+ * A node renderer is a function that takes a node and its children and returns the rendered output
80
+ */
81
+ TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType,
82
+ /**
83
+ * A mark renderer is a function that takes a mark and its children and returns the rendered output
84
+ */
85
+ TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType> = {
86
+ /**
87
+ * Mapping of node types to react components
88
+ */
89
+ nodeMapping: Record<string, NoInfer<TNodeRender>>;
90
+ /**
91
+ * Mapping of mark types to react components
92
+ */
93
+ markMapping: Record<string, NoInfer<TMarkRender>>;
94
+ /**
95
+ * Component to render if a node type is not handled
96
+ */
97
+ unhandledNode?: NoInfer<TNodeRender>;
98
+ /**
99
+ * Component to render if a mark type is not handled
100
+ */
101
+ unhandledMark?: NoInfer<TMarkRender>;
102
+ };
103
+
104
+ type DomOutputSpecToElement<T> = (content: DOMOutputSpec) => (children?: T | T[]) => T;
105
+ /**
106
+ * This takes a NodeExtension and maps it to a React component
107
+ * @param extension The node extension to map to a React component
108
+ * @param extensionAttributes All available extension attributes
109
+ * @returns A tuple with the name of the extension and a React component that renders the extension
110
+ */
111
+ 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];
112
+ /**
113
+ * This takes a MarkExtension and maps it to a React component
114
+ * @param extension The mark extension to map to a React component
115
+ * @param extensionAttributes All available extension attributes
116
+ * @returns A tuple with the name of the extension and a React component that renders the extension
117
+ */
118
+ 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];
119
+ /**
120
+ * This function will statically render a Prosemirror Node to a target element type using the given extensions
121
+ * @param renderer The renderer to use to render the Prosemirror Node to the target element type
122
+ * @param domOutputSpecToElement A function that takes a Prosemirror DOMOutputSpec and returns a function that takes children and returns the target element type
123
+ * @param mapDefinedTypes An object with functions to map the doc and text types to the target element type
124
+ * @param content The Prosemirror Node to render
125
+ * @param extensions The extensions to use to render the Prosemirror Node
126
+ * @param options Additional options to pass to the renderer that can override the default behavior
127
+ * @returns The rendered target element type
128
+ */
129
+ declare function renderToElement<T>({ renderer, domOutputSpecToElement, mapDefinedTypes, content, extensions, options, }: {
130
+ renderer: (options: TiptapStaticRendererOptions<T, Mark, Node$1>) => (ctx: {
131
+ content: Node$1;
132
+ }) => T;
133
+ domOutputSpecToElement: DomOutputSpecToElement<T>;
134
+ mapDefinedTypes: {
135
+ doc: (props: NodeProps<Node$1, T | T[]>) => T;
136
+ text: (props: NodeProps<Node$1, T | T[]>) => T;
137
+ };
138
+ content: Node$1 | JSONContent;
139
+ extensions: Extensions;
140
+ options?: Partial<TiptapStaticRendererOptions<T, Mark, Node$1>>;
141
+ }): T;
142
+
143
+ /**
144
+ * This code is just to show the flexibility of this renderer. We can potentially render content to any format we want.
145
+ * This is a simple example of how we can render content to markdown. This is not a full implementation of a markdown renderer.
146
+ */
147
+ declare function renderToMarkdown({ content, extensions, options, }: {
148
+ content: Node$1 | JSONContent;
149
+ extensions: Extensions;
150
+ options?: Partial<TiptapStaticRendererOptions<string, Mark, Node$1>>;
151
+ }): string;
152
+
153
+ export { type DomOutputSpecToElement, mapMarkExtensionToReactNode, mapNodeExtensionToReactNode, renderToElement, renderToMarkdown };