@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
package/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # @tiptap/static-renderer
2
+
3
+ [![Version](https://img.shields.io/npm/v/@tiptap/static-renderer.svg?label=version)](https://www.npmjs.com/package/@tiptap/static-renderer)
4
+ [![Downloads](https://img.shields.io/npm/dm/@tiptap/static-renderer.svg)](https://npmcharts.com/compare/tiptap?minimal=true)
5
+ [![License](https://img.shields.io/npm/l/@tiptap/static-renderer.svg)](https://www.npmjs.com/package/@tiptap/static-renderer)
6
+ [![Sponsor](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub)](https://github.com/sponsors/ueberdosis)
7
+
8
+ ## Introduction
9
+
10
+ Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as *New York Times*, *The Guardian* or *Atlassian*.
11
+
12
+ ## Official Documentation
13
+
14
+ Documentation can be found on the [Tiptap website](https://tiptap.dev).
15
+
16
+ ## License
17
+
18
+ Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
package/dist/index.cjs ADDED
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ getAttributes: () => getAttributes,
24
+ getHTMLAttributes: () => getHTMLAttributes
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/helpers.ts
29
+ var import_core = require("@tiptap/core");
30
+ function getAttributes(nodeOrMark, extensionAttributes, onlyRenderedAttributes) {
31
+ const nodeOrMarkAttributes = nodeOrMark.attrs;
32
+ if (!nodeOrMarkAttributes) {
33
+ return {};
34
+ }
35
+ return extensionAttributes.filter((item) => {
36
+ if (item.type !== nodeOrMark.type) {
37
+ return false;
38
+ }
39
+ if (onlyRenderedAttributes) {
40
+ return item.attribute.rendered;
41
+ }
42
+ return true;
43
+ }).map((item) => {
44
+ if (!item.attribute.renderHTML) {
45
+ return {
46
+ [item.name]: item.name in nodeOrMarkAttributes ? nodeOrMarkAttributes[item.name] : item.attribute.default
47
+ };
48
+ }
49
+ return item.attribute.renderHTML(nodeOrMarkAttributes) || {
50
+ [item.name]: item.name in nodeOrMarkAttributes ? nodeOrMarkAttributes[item.name] : item.attribute.default
51
+ };
52
+ }).reduce((attributes, attribute) => (0, import_core.mergeAttributes)(attributes, attribute), {});
53
+ }
54
+ function getHTMLAttributes(nodeOrMark, extensionAttributes) {
55
+ return getAttributes(nodeOrMark, extensionAttributes, true);
56
+ }
57
+ // Annotate the CommonJS export names for ESM import in node:
58
+ 0 && (module.exports = {
59
+ getAttributes,
60
+ getHTMLAttributes
61
+ });
62
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/helpers.ts"],"sourcesContent":["export * from './helpers.js'\nexport * from './types.js'\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { ExtensionAttribute, mergeAttributes } from '@tiptap/core'\n\nimport type { MarkType, NodeType } from './types'\n\n/**\n * This function returns the attributes of a node or mark that are defined by the given extension attributes.\n * @param nodeOrMark The node or mark to get the attributes from\n * @param extensionAttributes The extension attributes to use\n * @param onlyRenderedAttributes If true, only attributes that are rendered in the HTML are returned\n */\nexport function getAttributes(\n nodeOrMark: NodeType | MarkType,\n extensionAttributes: ExtensionAttribute[],\n onlyRenderedAttributes?: boolean,\n): Record<string, any> {\n const nodeOrMarkAttributes = nodeOrMark.attrs\n\n if (!nodeOrMarkAttributes) {\n return {}\n }\n\n return extensionAttributes\n .filter(item => {\n if (item.type !== nodeOrMark.type) {\n return false\n }\n if (onlyRenderedAttributes) {\n return item.attribute.rendered\n }\n return true\n })\n .map(item => {\n if (!item.attribute.renderHTML) {\n return {\n [item.name]:\n item.name in nodeOrMarkAttributes\n ? nodeOrMarkAttributes[item.name]\n : item.attribute.default,\n }\n }\n\n return (\n item.attribute.renderHTML(nodeOrMarkAttributes) || {\n [item.name]:\n item.name in nodeOrMarkAttributes\n ? nodeOrMarkAttributes[item.name]\n : item.attribute.default,\n }\n )\n })\n .reduce((attributes, attribute) => mergeAttributes(attributes, attribute), {})\n}\n\n/**\n * This function returns the HTML attributes of a node or mark that are defined by the given extension attributes.\n * @param nodeOrMark The node or mark to get the attributes from\n * @param extensionAttributes The extension attributes to use\n */\nexport function getHTMLAttributes(\n nodeOrMark: NodeType | MarkType,\n extensionAttributes: ExtensionAttribute[],\n) {\n return getAttributes(nodeOrMark, extensionAttributes, true)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,kBAAoD;AAU7C,SAAS,cACd,YACA,qBACA,wBACqB;AACrB,QAAM,uBAAuB,WAAW;AAExC,MAAI,CAAC,sBAAsB;AACzB,WAAO,CAAC;AAAA,EACV;AAEA,SAAO,oBACJ,OAAO,UAAQ;AACd,QAAI,KAAK,SAAS,WAAW,MAAM;AACjC,aAAO;AAAA,IACT;AACA,QAAI,wBAAwB;AAC1B,aAAO,KAAK,UAAU;AAAA,IACxB;AACA,WAAO;AAAA,EACT,CAAC,EACA,IAAI,UAAQ;AACX,QAAI,CAAC,KAAK,UAAU,YAAY;AAC9B,aAAO;AAAA,QACL,CAAC,KAAK,IAAI,GACR,KAAK,QAAQ,uBACT,qBAAqB,KAAK,IAAI,IAC9B,KAAK,UAAU;AAAA,MACvB;AAAA,IACF;AAEA,WACE,KAAK,UAAU,WAAW,oBAAoB,KAAK;AAAA,MACjD,CAAC,KAAK,IAAI,GACR,KAAK,QAAQ,uBACT,qBAAqB,KAAK,IAAI,IAC9B,KAAK,UAAU;AAAA,IACvB;AAAA,EAEJ,CAAC,EACA,OAAO,CAAC,YAAY,kBAAc,6BAAgB,YAAY,SAAS,GAAG,CAAC,CAAC;AACjF;AAOO,SAAS,kBACd,YACA,qBACA;AACA,SAAO,cAAc,YAAY,qBAAqB,IAAI;AAC5D;","names":[]}
@@ -0,0 +1,52 @@
1
+ import { ExtensionAttribute } from '@tiptap/core';
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
+ * A node type is either a JSON representation of a doc node or a Prosemirror doc node instance
22
+ */
23
+ type DocumentType<TNodeAttributes extends Record<string, any> = Record<string, any>, TContentType extends NodeType[] = NodeType[]> = NodeType<'doc', TNodeAttributes, never, TContentType>;
24
+ /**
25
+ * A node type is either a JSON representation of a text node or a Prosemirror text node instance
26
+ */
27
+ type TextType<TMarkType extends MarkType = MarkType> = {
28
+ type: 'text';
29
+ text: string;
30
+ marks: TMarkType[];
31
+ };
32
+ /**
33
+ * Describes the output of a `renderHTML` function in prosemirror
34
+ * @see https://prosemirror.net/docs/ref/#model.DOMOutputSpec
35
+ */
36
+ type DOMOutputSpecArray = [string] | [string, Record<string, any>] | [string, 0] | [string, Record<string, any>, 0] | [string, Record<string, any>, DOMOutputSpecArray | 0] | [string, DOMOutputSpecArray];
37
+
38
+ /**
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
43
+ */
44
+ declare function getAttributes(nodeOrMark: NodeType | MarkType, extensionAttributes: ExtensionAttribute[], onlyRenderedAttributes?: boolean): Record<string, any>;
45
+ /**
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
49
+ */
50
+ declare function getHTMLAttributes(nodeOrMark: NodeType | MarkType, extensionAttributes: ExtensionAttribute[]): Record<string, any>;
51
+
52
+ export { type DOMOutputSpecArray, type DocumentType, type MarkType, type NodeType, type TextType, getAttributes, getHTMLAttributes };
@@ -0,0 +1,52 @@
1
+ import { ExtensionAttribute } from '@tiptap/core';
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
+ * A node type is either a JSON representation of a doc node or a Prosemirror doc node instance
22
+ */
23
+ type DocumentType<TNodeAttributes extends Record<string, any> = Record<string, any>, TContentType extends NodeType[] = NodeType[]> = NodeType<'doc', TNodeAttributes, never, TContentType>;
24
+ /**
25
+ * A node type is either a JSON representation of a text node or a Prosemirror text node instance
26
+ */
27
+ type TextType<TMarkType extends MarkType = MarkType> = {
28
+ type: 'text';
29
+ text: string;
30
+ marks: TMarkType[];
31
+ };
32
+ /**
33
+ * Describes the output of a `renderHTML` function in prosemirror
34
+ * @see https://prosemirror.net/docs/ref/#model.DOMOutputSpec
35
+ */
36
+ type DOMOutputSpecArray = [string] | [string, Record<string, any>] | [string, 0] | [string, Record<string, any>, 0] | [string, Record<string, any>, DOMOutputSpecArray | 0] | [string, DOMOutputSpecArray];
37
+
38
+ /**
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
43
+ */
44
+ declare function getAttributes(nodeOrMark: NodeType | MarkType, extensionAttributes: ExtensionAttribute[], onlyRenderedAttributes?: boolean): Record<string, any>;
45
+ /**
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
49
+ */
50
+ declare function getHTMLAttributes(nodeOrMark: NodeType | MarkType, extensionAttributes: ExtensionAttribute[]): Record<string, any>;
51
+
52
+ export { type DOMOutputSpecArray, type DocumentType, type MarkType, type NodeType, type TextType, getAttributes, getHTMLAttributes };
package/dist/index.js ADDED
@@ -0,0 +1,34 @@
1
+ // src/helpers.ts
2
+ import { mergeAttributes } from "@tiptap/core";
3
+ function getAttributes(nodeOrMark, extensionAttributes, onlyRenderedAttributes) {
4
+ const nodeOrMarkAttributes = nodeOrMark.attrs;
5
+ if (!nodeOrMarkAttributes) {
6
+ return {};
7
+ }
8
+ return extensionAttributes.filter((item) => {
9
+ if (item.type !== nodeOrMark.type) {
10
+ return false;
11
+ }
12
+ if (onlyRenderedAttributes) {
13
+ return item.attribute.rendered;
14
+ }
15
+ return true;
16
+ }).map((item) => {
17
+ if (!item.attribute.renderHTML) {
18
+ return {
19
+ [item.name]: item.name in nodeOrMarkAttributes ? nodeOrMarkAttributes[item.name] : item.attribute.default
20
+ };
21
+ }
22
+ return item.attribute.renderHTML(nodeOrMarkAttributes) || {
23
+ [item.name]: item.name in nodeOrMarkAttributes ? nodeOrMarkAttributes[item.name] : item.attribute.default
24
+ };
25
+ }).reduce((attributes, attribute) => mergeAttributes(attributes, attribute), {});
26
+ }
27
+ function getHTMLAttributes(nodeOrMark, extensionAttributes) {
28
+ return getAttributes(nodeOrMark, extensionAttributes, true);
29
+ }
30
+ export {
31
+ getAttributes,
32
+ getHTMLAttributes
33
+ };
34
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/helpers.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { ExtensionAttribute, mergeAttributes } from '@tiptap/core'\n\nimport type { MarkType, NodeType } from './types'\n\n/**\n * This function returns the attributes of a node or mark that are defined by the given extension attributes.\n * @param nodeOrMark The node or mark to get the attributes from\n * @param extensionAttributes The extension attributes to use\n * @param onlyRenderedAttributes If true, only attributes that are rendered in the HTML are returned\n */\nexport function getAttributes(\n nodeOrMark: NodeType | MarkType,\n extensionAttributes: ExtensionAttribute[],\n onlyRenderedAttributes?: boolean,\n): Record<string, any> {\n const nodeOrMarkAttributes = nodeOrMark.attrs\n\n if (!nodeOrMarkAttributes) {\n return {}\n }\n\n return extensionAttributes\n .filter(item => {\n if (item.type !== nodeOrMark.type) {\n return false\n }\n if (onlyRenderedAttributes) {\n return item.attribute.rendered\n }\n return true\n })\n .map(item => {\n if (!item.attribute.renderHTML) {\n return {\n [item.name]:\n item.name in nodeOrMarkAttributes\n ? nodeOrMarkAttributes[item.name]\n : item.attribute.default,\n }\n }\n\n return (\n item.attribute.renderHTML(nodeOrMarkAttributes) || {\n [item.name]:\n item.name in nodeOrMarkAttributes\n ? nodeOrMarkAttributes[item.name]\n : item.attribute.default,\n }\n )\n })\n .reduce((attributes, attribute) => mergeAttributes(attributes, attribute), {})\n}\n\n/**\n * This function returns the HTML attributes of a node or mark that are defined by the given extension attributes.\n * @param nodeOrMark The node or mark to get the attributes from\n * @param extensionAttributes The extension attributes to use\n */\nexport function getHTMLAttributes(\n nodeOrMark: NodeType | MarkType,\n extensionAttributes: ExtensionAttribute[],\n) {\n return getAttributes(nodeOrMark, extensionAttributes, true)\n}\n"],"mappings":";AACA,SAA6B,uBAAuB;AAU7C,SAAS,cACd,YACA,qBACA,wBACqB;AACrB,QAAM,uBAAuB,WAAW;AAExC,MAAI,CAAC,sBAAsB;AACzB,WAAO,CAAC;AAAA,EACV;AAEA,SAAO,oBACJ,OAAO,UAAQ;AACd,QAAI,KAAK,SAAS,WAAW,MAAM;AACjC,aAAO;AAAA,IACT;AACA,QAAI,wBAAwB;AAC1B,aAAO,KAAK,UAAU;AAAA,IACxB;AACA,WAAO;AAAA,EACT,CAAC,EACA,IAAI,UAAQ;AACX,QAAI,CAAC,KAAK,UAAU,YAAY;AAC9B,aAAO;AAAA,QACL,CAAC,KAAK,IAAI,GACR,KAAK,QAAQ,uBACT,qBAAqB,KAAK,IAAI,IAC9B,KAAK,UAAU;AAAA,MACvB;AAAA,IACF;AAEA,WACE,KAAK,UAAU,WAAW,oBAAoB,KAAK;AAAA,MACjD,CAAC,KAAK,IAAI,GACR,KAAK,QAAQ,uBACT,qBAAqB,KAAK,IAAI,IAC9B,KAAK,UAAU;AAAA,IACvB;AAAA,EAEJ,CAAC,EACA,OAAO,CAAC,YAAY,cAAc,gBAAgB,YAAY,SAAS,GAAG,CAAC,CAAC;AACjF;AAOO,SAAS,kBACd,YACA,qBACA;AACA,SAAO,cAAc,YAAY,qBAAqB,IAAI;AAC5D;","names":[]}
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/json/html-string/index.ts
21
+ var html_string_exports = {};
22
+ __export(html_string_exports, {
23
+ TiptapStaticRenderer: () => TiptapStaticRenderer,
24
+ renderJSONContentToString: () => renderJSONContentToString
25
+ });
26
+ module.exports = __toCommonJS(html_string_exports);
27
+
28
+ // src/json/renderer.ts
29
+ function TiptapStaticRenderer(renderComponent, {
30
+ nodeMapping,
31
+ markMapping,
32
+ unhandledNode,
33
+ unhandledMark
34
+ }) {
35
+ return function renderContent({
36
+ content,
37
+ parent
38
+ }) {
39
+ var _a;
40
+ const nodeType = typeof content.type === "string" ? content.type : content.type.name;
41
+ const NodeHandler = (_a = nodeMapping[nodeType]) != null ? _a : unhandledNode;
42
+ if (!NodeHandler) {
43
+ throw new Error(`missing handler for node type ${nodeType}`);
44
+ }
45
+ const nodeContent = renderComponent({
46
+ component: NodeHandler,
47
+ props: {
48
+ node: content,
49
+ parent,
50
+ renderElement: renderContent,
51
+ // Lazily compute the children to avoid unnecessary recursion
52
+ get children() {
53
+ const children = [];
54
+ if (content.content) {
55
+ content.content.forEach((child) => {
56
+ children.push(
57
+ renderContent({
58
+ content: child,
59
+ parent: content
60
+ })
61
+ );
62
+ });
63
+ }
64
+ return children;
65
+ }
66
+ }
67
+ });
68
+ const markedContent = content.marks ? content.marks.reduce((acc, mark) => {
69
+ var _a2;
70
+ const markType = typeof mark.type === "string" ? mark.type : mark.type.name;
71
+ const MarkHandler = (_a2 = markMapping[markType]) != null ? _a2 : unhandledMark;
72
+ if (!MarkHandler) {
73
+ throw new Error(`missing handler for mark type ${markType}`);
74
+ }
75
+ return renderComponent({
76
+ component: MarkHandler,
77
+ props: {
78
+ mark,
79
+ parent,
80
+ node: content,
81
+ children: acc
82
+ }
83
+ });
84
+ }, nodeContent) : nodeContent;
85
+ return markedContent;
86
+ };
87
+ }
88
+
89
+ // src/json/html-string/string.ts
90
+ function renderJSONContentToString(options) {
91
+ return TiptapStaticRenderer((ctx) => {
92
+ return ctx.component(ctx.props);
93
+ }, options);
94
+ }
95
+ // Annotate the CommonJS export names for ESM import in node:
96
+ 0 && (module.exports = {
97
+ TiptapStaticRenderer,
98
+ renderJSONContentToString
99
+ });
100
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/json/html-string/index.ts","../../../src/json/renderer.ts","../../../src/json/html-string/string.ts"],"sourcesContent":["export * from '../renderer.js'\nexport * from './string.js'\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { MarkType, NodeType } from '../types'\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, TNodeRender>;\n /**\n * Mapping of mark types to react components\n */\n markMapping: Record<string, TMarkRender>;\n /**\n * Component to render if a node type is not handled\n */\n unhandledNode?: TNodeRender;\n /**\n * Component to render if a mark type is not handled\n */\n unhandledMark?: 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:(\ncb: (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 */\nimport { MarkType, NodeType } from '../../types.js'\nimport { TiptapStaticRenderer, TiptapStaticRendererOptions } from '../renderer.js'\n\nexport function renderJSONContentToString<\n/**\n * A mark type is either a JSON representation of a mark or a Prosemirror mark instance\n */\nTMarkType extends { type: any } = MarkType,\n/**\n * A node type is either a JSON representation of a node or a Prosemirror node instance\n */\nTNodeType extends {\n content?: { forEach:(cb: (node: TNodeType) => void) => void };\n marks?: readonly TMarkType[];\n type: string | { name: string };\n} = NodeType,\n>(options: TiptapStaticRendererOptions<string, TMarkType, TNodeType>) {\n return TiptapStaticRenderer(ctx => {\n return ctx.component(ctx.props as any)\n }, options)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmHO,SAAS,qBAkCd,iBAWA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GACA;AAIA,SAAO,SAAS,cAAc;AAAA,IAC5B;AAAA,IACA;AAAA,EACF,GASgB;AAtLlB;AAuLI,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;AA3N5C,UAAAA;AA4NQ,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,IACZ;AAEJ,WAAO;AAAA,EACT;AACF;;;AC7OO,SAAS,0BAad,SAAoE;AACpE,SAAO,qBAAqB,SAAO;AACjC,WAAO,IAAI,UAAU,IAAI,KAAY;AAAA,EACvC,GAAG,OAAO;AACZ;","names":["_a"]}
@@ -0,0 +1,205 @@
1
+ /**
2
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
3
+ */
4
+ type MarkType<Type extends string = any, Attributes extends undefined | Record<string, any> = any> = {
5
+ type: Type;
6
+ attrs: Attributes;
7
+ };
8
+ /**
9
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
10
+ */
11
+ type NodeType<Type extends string = any, Attributes extends undefined | Record<string, any> = any, NodeMarkType extends MarkType = any, Content extends NodeType[] = any> = {
12
+ type: Type;
13
+ attrs: Attributes;
14
+ content?: Content;
15
+ marks?: NodeMarkType[];
16
+ text?: string;
17
+ };
18
+
19
+ /**
20
+ * Props for a node renderer
21
+ */
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;
48
+ };
49
+ /**
50
+ * Props for a mark renderer
51
+ */
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;
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,
81
+ /**
82
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
83
+ */
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,
93
+ /**
94
+ * A node renderer is a function that takes a node and its children and returns the rendered output
95
+ */
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, TNodeRender>;
105
+ /**
106
+ * Mapping of mark types to react components
107
+ */
108
+ markMapping: Record<string, TMarkRender>;
109
+ /**
110
+ * Component to render if a node type is not handled
111
+ */
112
+ unhandledNode?: TNodeRender;
113
+ /**
114
+ * Component to render if a mark type is not handled
115
+ */
116
+ unhandledMark?: TMarkRender;
117
+ };
118
+ /**
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
164
+ */
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;
181
+
182
+ declare function renderJSONContentToString<
183
+ /**
184
+ * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
185
+ */
186
+ TMarkType extends {
187
+ type: any;
188
+ } = MarkType,
189
+ /**
190
+ * A node type is either a JSON representation of a node or a Prosemirror node instance
191
+ */
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
+ export { type MarkProps, type NodeProps, TiptapStaticRenderer, type TiptapStaticRendererOptions, renderJSONContentToString };