@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,117 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/json/react/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ TiptapStaticRenderer: () => TiptapStaticRenderer,
34
+ renderJSONContentToReactElement: () => renderJSONContentToReactElement
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+
38
+ // src/json/renderer.ts
39
+ function TiptapStaticRenderer(renderComponent, {
40
+ nodeMapping,
41
+ markMapping,
42
+ unhandledNode,
43
+ unhandledMark
44
+ }) {
45
+ return function renderContent({
46
+ content,
47
+ parent
48
+ }) {
49
+ var _a;
50
+ const nodeType = typeof content.type === "string" ? content.type : content.type.name;
51
+ const NodeHandler = (_a = nodeMapping[nodeType]) != null ? _a : unhandledNode;
52
+ if (!NodeHandler) {
53
+ throw new Error(`missing handler for node type ${nodeType}`);
54
+ }
55
+ const nodeContent = renderComponent({
56
+ component: NodeHandler,
57
+ props: {
58
+ node: content,
59
+ parent,
60
+ renderElement: renderContent,
61
+ // Lazily compute the children to avoid unnecessary recursion
62
+ get children() {
63
+ const children = [];
64
+ if (content.content) {
65
+ content.content.forEach((child) => {
66
+ children.push(
67
+ renderContent({
68
+ content: child,
69
+ parent: content
70
+ })
71
+ );
72
+ });
73
+ }
74
+ return children;
75
+ }
76
+ }
77
+ });
78
+ const markedContent = content.marks ? content.marks.reduce((acc, mark) => {
79
+ var _a2;
80
+ const markType = typeof mark.type === "string" ? mark.type : mark.type.name;
81
+ const MarkHandler = (_a2 = markMapping[markType]) != null ? _a2 : unhandledMark;
82
+ if (!MarkHandler) {
83
+ throw new Error(`missing handler for mark type ${markType}`);
84
+ }
85
+ return renderComponent({
86
+ component: MarkHandler,
87
+ props: {
88
+ mark,
89
+ parent,
90
+ node: content,
91
+ children: acc
92
+ }
93
+ });
94
+ }, nodeContent) : nodeContent;
95
+ return markedContent;
96
+ };
97
+ }
98
+
99
+ // src/json/react/react.ts
100
+ var import_react = __toESM(require("react"), 1);
101
+ function renderJSONContentToReactElement(options) {
102
+ let key = 0;
103
+ return TiptapStaticRenderer(({ component, props: { children, ...props } }) => {
104
+ return import_react.default.createElement(
105
+ component,
106
+ // eslint-disable-next-line no-plusplus
107
+ Object.assign(props, { key: key++ }),
108
+ [].concat(children)
109
+ );
110
+ }, options);
111
+ }
112
+ // Annotate the CommonJS export names for ESM import in node:
113
+ 0 && (module.exports = {
114
+ TiptapStaticRenderer,
115
+ renderJSONContentToReactElement
116
+ });
117
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/json/react/index.ts","../../../src/json/renderer.ts","../../../src/json/react/react.ts"],"sourcesContent":["export * from '../renderer.js'\nexport * from './react.js'\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { MarkType, NodeType } from '@tiptap/core'\n\n/**\n * Props for a node renderer\n */\nexport type NodeProps<TNodeType = any, TChildren = any> = {\n /**\n * The current node to render\n */\n node: TNodeType\n /**\n * Unless the node is the root node, this will always be defined\n */\n parent?: TNodeType\n /**\n * The children of the current node\n */\n children?: TChildren\n /**\n * Render a child element\n */\n renderElement: (props: {\n /**\n * Tiptap JSON content to render\n */\n content: TNodeType\n /**\n * The parent node of the current node\n */\n parent?: TNodeType\n }) => TChildren\n}\n\n/**\n * Props for a mark renderer\n */\nexport type MarkProps<TMarkType = any, TChildren = any, TNodeType = any> = {\n /**\n * The current mark to render\n */\n mark: TMarkType\n /**\n * The children of the current mark\n */\n children?: TChildren\n /**\n * The node the current mark is applied to\n */\n node: TNodeType\n /**\n * The node the current mark is applied to\n */\n parent?: TNodeType\n}\n\nexport type TiptapStaticRendererOptions<\n /**\n * The return type of the render function (e.g. React.ReactNode, string)\n */\n TReturnType,\n /**\n * A mark type is either a JSON representation of a mark or a Prosemirror mark instance\n */\n TMarkType extends { type: any } = MarkType,\n /**\n * A node type is either a JSON representation of a node or a Prosemirror node instance\n */\n TNodeType extends {\n content?: { forEach: (cb: (node: TNodeType) => void) => void }\n marks?: readonly TMarkType[]\n type: string | { name: string }\n } = NodeType,\n /**\n * A node renderer is a function that takes a node and its children and returns the rendered output\n */\n TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (\n ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>,\n ) => TReturnType,\n /**\n * A mark renderer is a function that takes a mark and its children and returns the rendered output\n */\n TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (\n ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>,\n ) => TReturnType,\n> = {\n /**\n * Mapping of node types to react components\n */\n nodeMapping: Record<string, NoInfer<TNodeRender>>\n /**\n * Mapping of mark types to react components\n */\n markMapping: Record<string, NoInfer<TMarkRender>>\n /**\n * Component to render if a node type is not handled\n */\n unhandledNode?: NoInfer<TNodeRender>\n /**\n * Component to render if a mark type is not handled\n */\n unhandledMark?: NoInfer<TMarkRender>\n}\n\n/**\n * Tiptap Static Renderer\n * ----------------------\n *\n * This function is a basis to allow for different renderers to be created.\n * Generic enough to be able to statically render Prosemirror JSON or Prosemirror Nodes.\n *\n * Using this function, you can create a renderer that takes a JSON representation of a Prosemirror document\n * and renders it using a mapping of node types to React components or even to a string.\n * This function is used as the basis to create the `reactRenderer` and `stringRenderer` functions.\n */\nexport function TiptapStaticRenderer<\n /**\n * The return type of the render function (e.g. React.ReactNode, string)\n */\n TReturnType,\n /**\n * A mark type is either a JSON representation of a mark or a Prosemirror mark instance\n */\n TMarkType extends { type: string | { name: string } } = MarkType,\n /**\n * A node type is either a JSON representation of a node or a Prosemirror node instance\n */\n TNodeType extends {\n content?: { forEach: (cb: (node: TNodeType) => void) => void }\n marks?: readonly TMarkType[]\n type: string | { name: string }\n } = NodeType,\n /**\n * A node renderer is a function that takes a node and its children and returns the rendered output\n */\n TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (\n ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>,\n ) => TReturnType,\n /**\n * A mark renderer is a function that takes a mark and its children and returns the rendered output\n */\n TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (\n ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>,\n ) => TReturnType,\n>(\n /**\n * The function that actually renders the component\n */\n renderComponent: (\n ctx:\n | {\n component: TNodeRender\n props: NodeProps<TNodeType, TReturnType | TReturnType[]>\n }\n | {\n component: TMarkRender\n props: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>\n },\n ) => TReturnType,\n {\n nodeMapping,\n markMapping,\n unhandledNode,\n unhandledMark,\n }: TiptapStaticRendererOptions<TReturnType, TMarkType, TNodeType, TNodeRender, TMarkRender>,\n) {\n /**\n * Render Tiptap JSON and all its children using the provided node and mark mappings.\n */\n return function renderContent({\n content,\n parent,\n }: {\n /**\n * Tiptap JSON content to render\n */\n content: TNodeType\n /**\n * The parent node of the current node\n */\n parent?: TNodeType\n }): TReturnType {\n const nodeType = typeof content.type === 'string' ? content.type : content.type.name\n const NodeHandler = nodeMapping[nodeType] ?? unhandledNode\n\n if (!NodeHandler) {\n throw new Error(`missing handler for node type ${nodeType}`)\n }\n\n const nodeContent = renderComponent({\n component: NodeHandler,\n props: {\n node: content,\n parent,\n renderElement: renderContent,\n // Lazily compute the children to avoid unnecessary recursion\n get children() {\n // recursively render child content nodes\n const children: TReturnType[] = []\n\n if (content.content) {\n content.content.forEach(child => {\n children.push(\n renderContent({\n content: child,\n parent: content,\n }),\n )\n })\n }\n\n return children\n },\n },\n })\n\n // apply marks to the content\n const markedContent = content.marks\n ? content.marks.reduce((acc, mark) => {\n const markType = typeof mark.type === 'string' ? mark.type : mark.type.name\n const MarkHandler = markMapping[markType] ?? unhandledMark\n\n if (!MarkHandler) {\n throw new Error(`missing handler for mark type ${markType}`)\n }\n\n return renderComponent({\n component: MarkHandler,\n props: {\n mark,\n parent,\n node: content,\n children: acc,\n },\n })\n }, nodeContent)\n : nodeContent\n\n return markedContent\n }\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport type { MarkType, NodeType } from '@tiptap/core'\nimport React from 'react'\n\nimport type { TiptapStaticRendererOptions } from '../renderer.js'\nimport { TiptapStaticRenderer } from '../renderer.js'\n\nexport function renderJSONContentToReactElement<\n /**\n * A mark type is either a JSON representation of a mark or a Prosemirror mark instance\n */\n TMarkType extends { type: any } = MarkType,\n /**\n * A node type is either a JSON representation of a node or a Prosemirror node instance\n */\n TNodeType extends {\n content?: { forEach: (cb: (node: TNodeType) => void) => void }\n marks?: readonly TMarkType[]\n type: string | { name: string }\n } = NodeType,\n>(options: TiptapStaticRendererOptions<React.ReactNode, TMarkType, TNodeType>) {\n let key = 0\n\n return TiptapStaticRenderer<React.ReactNode, TMarkType, TNodeType>(({ component, props: { children, ...props } }) => {\n return React.createElement(\n component as React.FC<typeof props>,\n // eslint-disable-next-line no-plusplus\n Object.assign(props, { key: key++ }),\n ([] as React.ReactNode[]).concat(children),\n )\n }, options)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmHO,SAAS,qBAiCd,iBAWA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GACA;AAIA,SAAO,SAAS,cAAc;AAAA,IAC5B;AAAA,IACA;AAAA,EACF,GASgB;AArLlB;AAsLI,UAAM,WAAW,OAAO,QAAQ,SAAS,WAAW,QAAQ,OAAO,QAAQ,KAAK;AAChF,UAAM,eAAc,iBAAY,QAAQ,MAApB,YAAyB;AAE7C,QAAI,CAAC,aAAa;AAChB,YAAM,IAAI,MAAM,iCAAiC,QAAQ,EAAE;AAAA,IAC7D;AAEA,UAAM,cAAc,gBAAgB;AAAA,MAClC,WAAW;AAAA,MACX,OAAO;AAAA,QACL,MAAM;AAAA,QACN;AAAA,QACA,eAAe;AAAA;AAAA,QAEf,IAAI,WAAW;AAEb,gBAAM,WAA0B,CAAC;AAEjC,cAAI,QAAQ,SAAS;AACnB,oBAAQ,QAAQ,QAAQ,WAAS;AAC/B,uBAAS;AAAA,gBACP,cAAc;AAAA,kBACZ,SAAS;AAAA,kBACT,QAAQ;AAAA,gBACV,CAAC;AAAA,cACH;AAAA,YACF,CAAC;AAAA,UACH;AAEA,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF,CAAC;AAGD,UAAM,gBAAgB,QAAQ,QAC1B,QAAQ,MAAM,OAAO,CAAC,KAAK,SAAS;AA1N5C,UAAAA;AA2NU,YAAM,WAAW,OAAO,KAAK,SAAS,WAAW,KAAK,OAAO,KAAK,KAAK;AACvE,YAAM,eAAcA,MAAA,YAAY,QAAQ,MAApB,OAAAA,MAAyB;AAE7C,UAAI,CAAC,aAAa;AAChB,cAAM,IAAI,MAAM,iCAAiC,QAAQ,EAAE;AAAA,MAC7D;AAEA,aAAO,gBAAgB;AAAA,QACrB,WAAW;AAAA,QACX,OAAO;AAAA,UACL;AAAA,UACA;AAAA,UACA,MAAM;AAAA,UACN,UAAU;AAAA,QACZ;AAAA,MACF,CAAC;AAAA,IACH,GAAG,WAAW,IACd;AAEJ,WAAO;AAAA,EACT;AACF;;;AC7OA,mBAAkB;AAKX,SAAS,gCAad,SAA6E;AAC7E,MAAI,MAAM;AAEV,SAAO,qBAA4D,CAAC,EAAE,WAAW,OAAO,EAAE,UAAU,GAAG,MAAM,EAAE,MAAM;AACnH,WAAO,aAAAC,QAAM;AAAA,MACX;AAAA;AAAA,MAEA,OAAO,OAAO,OAAO,EAAE,KAAK,MAAM,CAAC;AAAA,MAClC,CAAC,EAAwB,OAAO,QAAQ;AAAA,IAC3C;AAAA,EACF,GAAG,OAAO;AACZ;","names":["_a","React"]}
@@ -0,0 +1,190 @@
1
+ import { MarkType, NodeType } from '@tiptap/core';
2
+ import React from 'react';
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
+ * Tiptap Static Renderer
105
+ * ----------------------
106
+ *
107
+ * This function is a basis to allow for different renderers to be created.
108
+ * Generic enough to be able to statically render Prosemirror JSON or Prosemirror Nodes.
109
+ *
110
+ * Using this function, you can create a renderer that takes a JSON representation of a Prosemirror document
111
+ * and renders it using a mapping of node types to React components or even to a string.
112
+ * This function is used as the basis to create the `reactRenderer` and `stringRenderer` functions.
113
+ */
114
+ declare function TiptapStaticRenderer<
115
+ /**
116
+ * The return type of the render function (e.g. React.ReactNode, string)
117
+ */
118
+ TReturnType,
119
+ /**
120
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
121
+ */
122
+ TMarkType extends {
123
+ type: string | {
124
+ name: string;
125
+ };
126
+ } = MarkType,
127
+ /**
128
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
129
+ */
130
+ TNodeType extends {
131
+ content?: {
132
+ forEach: (cb: (node: TNodeType) => void) => void;
133
+ };
134
+ marks?: readonly TMarkType[];
135
+ type: string | {
136
+ name: string;
137
+ };
138
+ } = NodeType,
139
+ /**
140
+ * A node renderer is a function that takes a node and its children and returns the rendered output
141
+ */
142
+ TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType,
143
+ /**
144
+ * A mark renderer is a function that takes a mark and its children and returns the rendered output
145
+ */
146
+ TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType>(
147
+ /**
148
+ * The function that actually renders the component
149
+ */
150
+ renderComponent: (ctx: {
151
+ component: TNodeRender;
152
+ props: NodeProps<TNodeType, TReturnType | TReturnType[]>;
153
+ } | {
154
+ component: TMarkRender;
155
+ props: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>;
156
+ }) => TReturnType, { nodeMapping, markMapping, unhandledNode, unhandledMark, }: TiptapStaticRendererOptions<TReturnType, TMarkType, TNodeType, TNodeRender, TMarkRender>): ({ content, parent, }: {
157
+ /**
158
+ * Tiptap JSON content to render
159
+ */
160
+ content: TNodeType;
161
+ /**
162
+ * The parent node of the current node
163
+ */
164
+ parent?: TNodeType;
165
+ }) => TReturnType;
166
+
167
+ declare function renderJSONContentToReactElement<
168
+ /**
169
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
170
+ */
171
+ TMarkType extends {
172
+ type: any;
173
+ } = MarkType,
174
+ /**
175
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
176
+ */
177
+ TNodeType extends {
178
+ content?: {
179
+ forEach: (cb: (node: TNodeType) => void) => void;
180
+ };
181
+ marks?: readonly TMarkType[];
182
+ type: string | {
183
+ name: string;
184
+ };
185
+ } = NodeType>(options: TiptapStaticRendererOptions<React.ReactNode, TMarkType, TNodeType>): ({ content, parent, }: {
186
+ content: TNodeType;
187
+ parent?: TNodeType | undefined;
188
+ }) => React.ReactNode;
189
+
190
+ export { type MarkProps, type NodeProps, TiptapStaticRenderer, type TiptapStaticRendererOptions, renderJSONContentToReactElement };
@@ -0,0 +1,190 @@
1
+ import { MarkType, NodeType } from '@tiptap/core';
2
+ import React from 'react';
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
+ * Tiptap Static Renderer
105
+ * ----------------------
106
+ *
107
+ * This function is a basis to allow for different renderers to be created.
108
+ * Generic enough to be able to statically render Prosemirror JSON or Prosemirror Nodes.
109
+ *
110
+ * Using this function, you can create a renderer that takes a JSON representation of a Prosemirror document
111
+ * and renders it using a mapping of node types to React components or even to a string.
112
+ * This function is used as the basis to create the `reactRenderer` and `stringRenderer` functions.
113
+ */
114
+ declare function TiptapStaticRenderer<
115
+ /**
116
+ * The return type of the render function (e.g. React.ReactNode, string)
117
+ */
118
+ TReturnType,
119
+ /**
120
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
121
+ */
122
+ TMarkType extends {
123
+ type: string | {
124
+ name: string;
125
+ };
126
+ } = MarkType,
127
+ /**
128
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
129
+ */
130
+ TNodeType extends {
131
+ content?: {
132
+ forEach: (cb: (node: TNodeType) => void) => void;
133
+ };
134
+ marks?: readonly TMarkType[];
135
+ type: string | {
136
+ name: string;
137
+ };
138
+ } = NodeType,
139
+ /**
140
+ * A node renderer is a function that takes a node and its children and returns the rendered output
141
+ */
142
+ TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType,
143
+ /**
144
+ * A mark renderer is a function that takes a mark and its children and returns the rendered output
145
+ */
146
+ TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType>(
147
+ /**
148
+ * The function that actually renders the component
149
+ */
150
+ renderComponent: (ctx: {
151
+ component: TNodeRender;
152
+ props: NodeProps<TNodeType, TReturnType | TReturnType[]>;
153
+ } | {
154
+ component: TMarkRender;
155
+ props: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>;
156
+ }) => TReturnType, { nodeMapping, markMapping, unhandledNode, unhandledMark, }: TiptapStaticRendererOptions<TReturnType, TMarkType, TNodeType, TNodeRender, TMarkRender>): ({ content, parent, }: {
157
+ /**
158
+ * Tiptap JSON content to render
159
+ */
160
+ content: TNodeType;
161
+ /**
162
+ * The parent node of the current node
163
+ */
164
+ parent?: TNodeType;
165
+ }) => TReturnType;
166
+
167
+ declare function renderJSONContentToReactElement<
168
+ /**
169
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
170
+ */
171
+ TMarkType extends {
172
+ type: any;
173
+ } = MarkType,
174
+ /**
175
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
176
+ */
177
+ TNodeType extends {
178
+ content?: {
179
+ forEach: (cb: (node: TNodeType) => void) => void;
180
+ };
181
+ marks?: readonly TMarkType[];
182
+ type: string | {
183
+ name: string;
184
+ };
185
+ } = NodeType>(options: TiptapStaticRendererOptions<React.ReactNode, TMarkType, TNodeType>): ({ content, parent, }: {
186
+ content: TNodeType;
187
+ parent?: TNodeType | undefined;
188
+ }) => React.ReactNode;
189
+
190
+ export { type MarkProps, type NodeProps, TiptapStaticRenderer, type TiptapStaticRendererOptions, renderJSONContentToReactElement };
@@ -0,0 +1,79 @@
1
+ // src/json/renderer.ts
2
+ function TiptapStaticRenderer(renderComponent, {
3
+ nodeMapping,
4
+ markMapping,
5
+ unhandledNode,
6
+ unhandledMark
7
+ }) {
8
+ return function renderContent({
9
+ content,
10
+ parent
11
+ }) {
12
+ var _a;
13
+ const nodeType = typeof content.type === "string" ? content.type : content.type.name;
14
+ const NodeHandler = (_a = nodeMapping[nodeType]) != null ? _a : unhandledNode;
15
+ if (!NodeHandler) {
16
+ throw new Error(`missing handler for node type ${nodeType}`);
17
+ }
18
+ const nodeContent = renderComponent({
19
+ component: NodeHandler,
20
+ props: {
21
+ node: content,
22
+ parent,
23
+ renderElement: renderContent,
24
+ // Lazily compute the children to avoid unnecessary recursion
25
+ get children() {
26
+ const children = [];
27
+ if (content.content) {
28
+ content.content.forEach((child) => {
29
+ children.push(
30
+ renderContent({
31
+ content: child,
32
+ parent: content
33
+ })
34
+ );
35
+ });
36
+ }
37
+ return children;
38
+ }
39
+ }
40
+ });
41
+ const markedContent = content.marks ? content.marks.reduce((acc, mark) => {
42
+ var _a2;
43
+ const markType = typeof mark.type === "string" ? mark.type : mark.type.name;
44
+ const MarkHandler = (_a2 = markMapping[markType]) != null ? _a2 : unhandledMark;
45
+ if (!MarkHandler) {
46
+ throw new Error(`missing handler for mark type ${markType}`);
47
+ }
48
+ return renderComponent({
49
+ component: MarkHandler,
50
+ props: {
51
+ mark,
52
+ parent,
53
+ node: content,
54
+ children: acc
55
+ }
56
+ });
57
+ }, nodeContent) : nodeContent;
58
+ return markedContent;
59
+ };
60
+ }
61
+
62
+ // src/json/react/react.ts
63
+ import React from "react";
64
+ function renderJSONContentToReactElement(options) {
65
+ let key = 0;
66
+ return TiptapStaticRenderer(({ component, props: { children, ...props } }) => {
67
+ return React.createElement(
68
+ component,
69
+ // eslint-disable-next-line no-plusplus
70
+ Object.assign(props, { key: key++ }),
71
+ [].concat(children)
72
+ );
73
+ }, options);
74
+ }
75
+ export {
76
+ TiptapStaticRenderer,
77
+ renderJSONContentToReactElement
78
+ };
79
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/json/renderer.ts","../../../src/json/react/react.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { MarkType, NodeType } from '@tiptap/core'\n\n/**\n * Props for a node renderer\n */\nexport type NodeProps<TNodeType = any, TChildren = any> = {\n /**\n * The current node to render\n */\n node: TNodeType\n /**\n * Unless the node is the root node, this will always be defined\n */\n parent?: TNodeType\n /**\n * The children of the current node\n */\n children?: TChildren\n /**\n * Render a child element\n */\n renderElement: (props: {\n /**\n * Tiptap JSON content to render\n */\n content: TNodeType\n /**\n * The parent node of the current node\n */\n parent?: TNodeType\n }) => TChildren\n}\n\n/**\n * Props for a mark renderer\n */\nexport type MarkProps<TMarkType = any, TChildren = any, TNodeType = any> = {\n /**\n * The current mark to render\n */\n mark: TMarkType\n /**\n * The children of the current mark\n */\n children?: TChildren\n /**\n * The node the current mark is applied to\n */\n node: TNodeType\n /**\n * The node the current mark is applied to\n */\n parent?: TNodeType\n}\n\nexport type TiptapStaticRendererOptions<\n /**\n * The return type of the render function (e.g. React.ReactNode, string)\n */\n TReturnType,\n /**\n * A mark type is either a JSON representation of a mark or a Prosemirror mark instance\n */\n TMarkType extends { type: any } = MarkType,\n /**\n * A node type is either a JSON representation of a node or a Prosemirror node instance\n */\n TNodeType extends {\n content?: { forEach: (cb: (node: TNodeType) => void) => void }\n marks?: readonly TMarkType[]\n type: string | { name: string }\n } = NodeType,\n /**\n * A node renderer is a function that takes a node and its children and returns the rendered output\n */\n TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (\n ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>,\n ) => TReturnType,\n /**\n * A mark renderer is a function that takes a mark and its children and returns the rendered output\n */\n TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (\n ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>,\n ) => TReturnType,\n> = {\n /**\n * Mapping of node types to react components\n */\n nodeMapping: Record<string, NoInfer<TNodeRender>>\n /**\n * Mapping of mark types to react components\n */\n markMapping: Record<string, NoInfer<TMarkRender>>\n /**\n * Component to render if a node type is not handled\n */\n unhandledNode?: NoInfer<TNodeRender>\n /**\n * Component to render if a mark type is not handled\n */\n unhandledMark?: NoInfer<TMarkRender>\n}\n\n/**\n * Tiptap Static Renderer\n * ----------------------\n *\n * This function is a basis to allow for different renderers to be created.\n * Generic enough to be able to statically render Prosemirror JSON or Prosemirror Nodes.\n *\n * Using this function, you can create a renderer that takes a JSON representation of a Prosemirror document\n * and renders it using a mapping of node types to React components or even to a string.\n * This function is used as the basis to create the `reactRenderer` and `stringRenderer` functions.\n */\nexport function TiptapStaticRenderer<\n /**\n * The return type of the render function (e.g. React.ReactNode, string)\n */\n TReturnType,\n /**\n * A mark type is either a JSON representation of a mark or a Prosemirror mark instance\n */\n TMarkType extends { type: string | { name: string } } = MarkType,\n /**\n * A node type is either a JSON representation of a node or a Prosemirror node instance\n */\n TNodeType extends {\n content?: { forEach: (cb: (node: TNodeType) => void) => void }\n marks?: readonly TMarkType[]\n type: string | { name: string }\n } = NodeType,\n /**\n * A node renderer is a function that takes a node and its children and returns the rendered output\n */\n TNodeRender extends (ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>) => TReturnType = (\n ctx: NodeProps<TNodeType, TReturnType | TReturnType[]>,\n ) => TReturnType,\n /**\n * A mark renderer is a function that takes a mark and its children and returns the rendered output\n */\n TMarkRender extends (ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>) => TReturnType = (\n ctx: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>,\n ) => TReturnType,\n>(\n /**\n * The function that actually renders the component\n */\n renderComponent: (\n ctx:\n | {\n component: TNodeRender\n props: NodeProps<TNodeType, TReturnType | TReturnType[]>\n }\n | {\n component: TMarkRender\n props: MarkProps<TMarkType, TReturnType | TReturnType[], TNodeType>\n },\n ) => TReturnType,\n {\n nodeMapping,\n markMapping,\n unhandledNode,\n unhandledMark,\n }: TiptapStaticRendererOptions<TReturnType, TMarkType, TNodeType, TNodeRender, TMarkRender>,\n) {\n /**\n * Render Tiptap JSON and all its children using the provided node and mark mappings.\n */\n return function renderContent({\n content,\n parent,\n }: {\n /**\n * Tiptap JSON content to render\n */\n content: TNodeType\n /**\n * The parent node of the current node\n */\n parent?: TNodeType\n }): TReturnType {\n const nodeType = typeof content.type === 'string' ? content.type : content.type.name\n const NodeHandler = nodeMapping[nodeType] ?? unhandledNode\n\n if (!NodeHandler) {\n throw new Error(`missing handler for node type ${nodeType}`)\n }\n\n const nodeContent = renderComponent({\n component: NodeHandler,\n props: {\n node: content,\n parent,\n renderElement: renderContent,\n // Lazily compute the children to avoid unnecessary recursion\n get children() {\n // recursively render child content nodes\n const children: TReturnType[] = []\n\n if (content.content) {\n content.content.forEach(child => {\n children.push(\n renderContent({\n content: child,\n parent: content,\n }),\n )\n })\n }\n\n return children\n },\n },\n })\n\n // apply marks to the content\n const markedContent = content.marks\n ? content.marks.reduce((acc, mark) => {\n const markType = typeof mark.type === 'string' ? mark.type : mark.type.name\n const MarkHandler = markMapping[markType] ?? unhandledMark\n\n if (!MarkHandler) {\n throw new Error(`missing handler for mark type ${markType}`)\n }\n\n return renderComponent({\n component: MarkHandler,\n props: {\n mark,\n parent,\n node: content,\n children: acc,\n },\n })\n }, nodeContent)\n : nodeContent\n\n return markedContent\n }\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport type { MarkType, NodeType } from '@tiptap/core'\nimport React from 'react'\n\nimport type { TiptapStaticRendererOptions } from '../renderer.js'\nimport { TiptapStaticRenderer } from '../renderer.js'\n\nexport function renderJSONContentToReactElement<\n /**\n * A mark type is either a JSON representation of a mark or a Prosemirror mark instance\n */\n TMarkType extends { type: any } = MarkType,\n /**\n * A node type is either a JSON representation of a node or a Prosemirror node instance\n */\n TNodeType extends {\n content?: { forEach: (cb: (node: TNodeType) => void) => void }\n marks?: readonly TMarkType[]\n type: string | { name: string }\n } = NodeType,\n>(options: TiptapStaticRendererOptions<React.ReactNode, TMarkType, TNodeType>) {\n let key = 0\n\n return TiptapStaticRenderer<React.ReactNode, TMarkType, TNodeType>(({ component, props: { children, ...props } }) => {\n return React.createElement(\n component as React.FC<typeof props>,\n // eslint-disable-next-line no-plusplus\n Object.assign(props, { key: key++ }),\n ([] as React.ReactNode[]).concat(children),\n )\n }, options)\n}\n"],"mappings":";AAmHO,SAAS,qBAiCd,iBAWA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GACA;AAIA,SAAO,SAAS,cAAc;AAAA,IAC5B;AAAA,IACA;AAAA,EACF,GASgB;AArLlB;AAsLI,UAAM,WAAW,OAAO,QAAQ,SAAS,WAAW,QAAQ,OAAO,QAAQ,KAAK;AAChF,UAAM,eAAc,iBAAY,QAAQ,MAApB,YAAyB;AAE7C,QAAI,CAAC,aAAa;AAChB,YAAM,IAAI,MAAM,iCAAiC,QAAQ,EAAE;AAAA,IAC7D;AAEA,UAAM,cAAc,gBAAgB;AAAA,MAClC,WAAW;AAAA,MACX,OAAO;AAAA,QACL,MAAM;AAAA,QACN;AAAA,QACA,eAAe;AAAA;AAAA,QAEf,IAAI,WAAW;AAEb,gBAAM,WAA0B,CAAC;AAEjC,cAAI,QAAQ,SAAS;AACnB,oBAAQ,QAAQ,QAAQ,WAAS;AAC/B,uBAAS;AAAA,gBACP,cAAc;AAAA,kBACZ,SAAS;AAAA,kBACT,QAAQ;AAAA,gBACV,CAAC;AAAA,cACH;AAAA,YACF,CAAC;AAAA,UACH;AAEA,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF,CAAC;AAGD,UAAM,gBAAgB,QAAQ,QAC1B,QAAQ,MAAM,OAAO,CAAC,KAAK,SAAS;AA1N5C,UAAAA;AA2NU,YAAM,WAAW,OAAO,KAAK,SAAS,WAAW,KAAK,OAAO,KAAK,KAAK;AACvE,YAAM,eAAcA,MAAA,YAAY,QAAQ,MAApB,OAAAA,MAAyB;AAE7C,UAAI,CAAC,aAAa;AAChB,cAAM,IAAI,MAAM,iCAAiC,QAAQ,EAAE;AAAA,MAC7D;AAEA,aAAO,gBAAgB;AAAA,QACrB,WAAW;AAAA,QACX,OAAO;AAAA,UACL;AAAA,UACA;AAAA,UACA,MAAM;AAAA,UACN,UAAU;AAAA,QACZ;AAAA,MACF,CAAC;AAAA,IACH,GAAG,WAAW,IACd;AAEJ,WAAO;AAAA,EACT;AACF;;;AC7OA,OAAO,WAAW;AAKX,SAAS,gCAad,SAA6E;AAC7E,MAAI,MAAM;AAEV,SAAO,qBAA4D,CAAC,EAAE,WAAW,OAAO,EAAE,UAAU,GAAG,MAAM,EAAE,MAAM;AACnH,WAAO,MAAM;AAAA,MACX;AAAA;AAAA,MAEA,OAAO,OAAO,OAAO,EAAE,KAAK,MAAM,CAAC;AAAA,MAClC,CAAC,EAAwB,OAAO,QAAQ;AAAA,IAC3C;AAAA,EACF,GAAG,OAAO;AACZ;","names":["_a"]}