@tiptap/static-renderer 3.20.1 → 3.20.3

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 (43) hide show
  1. package/package.json +5 -5
  2. package/dist/index.cjs +0 -647
  3. package/dist/index.cjs.map +0 -1
  4. package/dist/index.d.cts +0 -334
  5. package/dist/index.d.ts +0 -334
  6. package/dist/index.js +0 -601
  7. package/dist/index.js.map +0 -1
  8. package/dist/json/html-string/index.cjs +0 -111
  9. package/dist/json/html-string/index.cjs.map +0 -1
  10. package/dist/json/html-string/index.d.cts +0 -201
  11. package/dist/json/html-string/index.d.ts +0 -201
  12. package/dist/json/html-string/index.js +0 -81
  13. package/dist/json/html-string/index.js.map +0 -1
  14. package/dist/json/react/index.cjs +0 -117
  15. package/dist/json/react/index.cjs.map +0 -1
  16. package/dist/json/react/index.d.cts +0 -190
  17. package/dist/json/react/index.d.ts +0 -190
  18. package/dist/json/react/index.js +0 -79
  19. package/dist/json/react/index.js.map +0 -1
  20. package/dist/json/renderer.cjs +0 -89
  21. package/dist/json/renderer.cjs.map +0 -1
  22. package/dist/json/renderer.d.cts +0 -166
  23. package/dist/json/renderer.d.ts +0 -166
  24. package/dist/json/renderer.js +0 -64
  25. package/dist/json/renderer.js.map +0 -1
  26. package/dist/pm/html-string/index.cjs +0 -348
  27. package/dist/pm/html-string/index.cjs.map +0 -1
  28. package/dist/pm/html-string/index.d.cts +0 -175
  29. package/dist/pm/html-string/index.d.ts +0 -175
  30. package/dist/pm/html-string/index.js +0 -321
  31. package/dist/pm/html-string/index.js.map +0 -1
  32. package/dist/pm/markdown/index.cjs +0 -478
  33. package/dist/pm/markdown/index.cjs.map +0 -1
  34. package/dist/pm/markdown/index.d.cts +0 -153
  35. package/dist/pm/markdown/index.d.ts +0 -153
  36. package/dist/pm/markdown/index.js +0 -454
  37. package/dist/pm/markdown/index.js.map +0 -1
  38. package/dist/pm/react/index.cjs +0 -412
  39. package/dist/pm/react/index.cjs.map +0 -1
  40. package/dist/pm/react/index.d.cts +0 -170
  41. package/dist/pm/react/index.d.ts +0 -170
  42. package/dist/pm/react/index.js +0 -376
  43. package/dist/pm/react/index.js.map +0 -1
package/dist/index.js DELETED
@@ -1,601 +0,0 @@
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 !== (typeof nodeOrMark.type === "string" ? nodeOrMark.type : nodeOrMark.type.name)) {
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
-
31
- // src/json/renderer.ts
32
- function TiptapStaticRenderer(renderComponent, {
33
- nodeMapping,
34
- markMapping,
35
- unhandledNode,
36
- unhandledMark
37
- }) {
38
- return function renderContent({
39
- content,
40
- parent
41
- }) {
42
- var _a;
43
- const nodeType = typeof content.type === "string" ? content.type : content.type.name;
44
- const NodeHandler = (_a = nodeMapping[nodeType]) != null ? _a : unhandledNode;
45
- if (!NodeHandler) {
46
- throw new Error(`missing handler for node type ${nodeType}`);
47
- }
48
- const nodeContent = renderComponent({
49
- component: NodeHandler,
50
- props: {
51
- node: content,
52
- parent,
53
- renderElement: renderContent,
54
- // Lazily compute the children to avoid unnecessary recursion
55
- get children() {
56
- const children = [];
57
- if (content.content) {
58
- content.content.forEach((child) => {
59
- children.push(
60
- renderContent({
61
- content: child,
62
- parent: content
63
- })
64
- );
65
- });
66
- }
67
- return children;
68
- }
69
- }
70
- });
71
- const markedContent = content.marks ? content.marks.reduce((acc, mark) => {
72
- var _a2;
73
- const markType = typeof mark.type === "string" ? mark.type : mark.type.name;
74
- const MarkHandler = (_a2 = markMapping[markType]) != null ? _a2 : unhandledMark;
75
- if (!MarkHandler) {
76
- throw new Error(`missing handler for mark type ${markType}`);
77
- }
78
- return renderComponent({
79
- component: MarkHandler,
80
- props: {
81
- mark,
82
- parent,
83
- node: content,
84
- children: acc
85
- }
86
- });
87
- }, nodeContent) : nodeContent;
88
- return markedContent;
89
- };
90
- }
91
-
92
- // src/json/html-string/string.ts
93
- function renderJSONContentToString(options) {
94
- return TiptapStaticRenderer((ctx) => {
95
- return ctx.component(ctx.props);
96
- }, options);
97
- }
98
- function serializeAttrsToHTMLString(attrs) {
99
- const output = Object.entries(attrs || {}).map(([key, value]) => `${key.split(" ").at(-1)}=${JSON.stringify(value)}`).join(" ");
100
- return output ? ` ${output}` : "";
101
- }
102
- function serializeChildrenToHTMLString(children) {
103
- return [].concat(children || "").filter(Boolean).join("");
104
- }
105
-
106
- // src/json/react/react.ts
107
- import React from "react";
108
- function renderJSONContentToReactElement(options) {
109
- let key = 0;
110
- return TiptapStaticRenderer(({ component, props: { children, ...props } }) => {
111
- return React.createElement(
112
- component,
113
- // eslint-disable-next-line no-plusplus
114
- Object.assign(props, { key: key++ }),
115
- [].concat(children)
116
- );
117
- }, options);
118
- }
119
-
120
- // src/pm/extensionRenderer.ts
121
- import {
122
- getAttributesFromExtensions,
123
- getExtensionField,
124
- getSchemaByResolvedExtensions,
125
- resolveExtensions,
126
- splitExtensions
127
- } from "@tiptap/core";
128
- import { Node } from "@tiptap/pm/model";
129
- function mapNodeExtensionToReactNode(domOutputSpecToElement, extension, extensionAttributes, options) {
130
- const context = {
131
- name: extension.name,
132
- options: extension.options,
133
- storage: extension.storage,
134
- parent: extension.parent
135
- };
136
- const renderToHTML = getExtensionField(extension, "renderHTML", context);
137
- if (!renderToHTML) {
138
- if (options == null ? void 0 : options.unhandledNode) {
139
- return [extension.name, options.unhandledNode];
140
- }
141
- return [
142
- extension.name,
143
- () => {
144
- throw new Error(
145
- `[tiptap error]: Node ${extension.name} cannot be rendered, it is missing a "renderToHTML" method, please implement it or override the corresponding "nodeMapping" method to have a custom rendering`
146
- );
147
- }
148
- ];
149
- }
150
- return [
151
- extension.name,
152
- ({ node, children }) => {
153
- try {
154
- return domOutputSpecToElement(
155
- renderToHTML({
156
- node,
157
- HTMLAttributes: getHTMLAttributes(node, extensionAttributes)
158
- })
159
- )(children);
160
- } catch (e) {
161
- throw new Error(
162
- `[tiptap error]: Node ${extension.name} cannot be rendered, it's "renderToHTML" method threw an error: ${e.message}`,
163
- { cause: e }
164
- );
165
- }
166
- }
167
- ];
168
- }
169
- function mapMarkExtensionToReactNode(domOutputSpecToElement, extension, extensionAttributes, options) {
170
- const context = {
171
- name: extension.name,
172
- options: extension.options,
173
- storage: extension.storage,
174
- parent: extension.parent
175
- };
176
- const renderToHTML = getExtensionField(extension, "renderHTML", context);
177
- if (!renderToHTML) {
178
- if (options == null ? void 0 : options.unhandledMark) {
179
- return [extension.name, options.unhandledMark];
180
- }
181
- return [
182
- extension.name,
183
- () => {
184
- throw new Error(`Node ${extension.name} cannot be rendered, it is missing a "renderToHTML" method`);
185
- }
186
- ];
187
- }
188
- return [
189
- extension.name,
190
- ({ mark, children }) => {
191
- try {
192
- return domOutputSpecToElement(
193
- renderToHTML({
194
- mark,
195
- HTMLAttributes: getHTMLAttributes(mark, extensionAttributes)
196
- })
197
- )(children);
198
- } catch (e) {
199
- throw new Error(
200
- `[tiptap error]: Mark ${extension.name} cannot be rendered, it's "renderToHTML" method threw an error: ${e.message}`,
201
- { cause: e }
202
- );
203
- }
204
- }
205
- ];
206
- }
207
- function renderToElement({
208
- renderer,
209
- domOutputSpecToElement,
210
- mapDefinedTypes,
211
- content,
212
- extensions,
213
- options
214
- }) {
215
- extensions = resolveExtensions(extensions);
216
- const extensionAttributes = getAttributesFromExtensions(extensions);
217
- const { nodeExtensions, markExtensions } = splitExtensions(extensions);
218
- if (!(content instanceof Node)) {
219
- content = Node.fromJSON(getSchemaByResolvedExtensions(extensions), content);
220
- }
221
- return renderer({
222
- ...options,
223
- nodeMapping: {
224
- ...Object.fromEntries(
225
- nodeExtensions.filter((e) => {
226
- if (e.name in mapDefinedTypes) {
227
- return false;
228
- }
229
- if (options == null ? void 0 : options.nodeMapping) {
230
- return !(e.name in options.nodeMapping);
231
- }
232
- return true;
233
- }).map(
234
- (nodeExtension) => mapNodeExtensionToReactNode(domOutputSpecToElement, nodeExtension, extensionAttributes, options)
235
- )
236
- ),
237
- ...mapDefinedTypes,
238
- ...options == null ? void 0 : options.nodeMapping
239
- },
240
- markMapping: {
241
- ...Object.fromEntries(
242
- markExtensions.filter((e) => {
243
- if (options == null ? void 0 : options.markMapping) {
244
- return !(e.name in options.markMapping);
245
- }
246
- return true;
247
- }).map((mark) => mapMarkExtensionToReactNode(domOutputSpecToElement, mark, extensionAttributes, options))
248
- ),
249
- ...options == null ? void 0 : options.markMapping
250
- }
251
- })({ content });
252
- }
253
-
254
- // src/pm/html-string/html-string.ts
255
- var NON_SELF_CLOSING_TAGS = /* @__PURE__ */ new Set(["iframe", "script", "style", "title", "textarea", "div", "span", "a", "button"]);
256
- function domOutputSpecToHTMLString(content) {
257
- if (typeof content === "string") {
258
- return () => content;
259
- }
260
- if (typeof content === "object" && "length" in content) {
261
- const [_tag, attrs, children, ...rest] = content;
262
- let tag = _tag;
263
- const parts = tag.split(" ");
264
- if (parts.length > 1) {
265
- tag = `${parts[1]} xmlns="${parts[0]}"`;
266
- }
267
- if (attrs === void 0) {
268
- return () => `<${tag}/>`;
269
- }
270
- if (attrs === 0) {
271
- return (child) => `<${tag}>${serializeChildrenToHTMLString(child)}</${tag}>`;
272
- }
273
- if (typeof attrs === "object") {
274
- if (Array.isArray(attrs)) {
275
- if (children === void 0) {
276
- return (child) => `<${tag}>${domOutputSpecToHTMLString(attrs)(child)}</${tag}>`;
277
- }
278
- if (children === 0) {
279
- return (child) => `<${tag}>${domOutputSpecToHTMLString(attrs)(child)}</${tag}>`;
280
- }
281
- return (child) => `<${tag}>${domOutputSpecToHTMLString(attrs)(child)}${[children].concat(rest).map((a) => domOutputSpecToHTMLString(a)(child))}</${tag}>`;
282
- }
283
- if (children === void 0) {
284
- if (NON_SELF_CLOSING_TAGS.has(tag)) {
285
- return () => `<${tag}${serializeAttrsToHTMLString(attrs)}></${tag}>`;
286
- }
287
- return () => `<${tag}${serializeAttrsToHTMLString(attrs)}/>`;
288
- }
289
- if (children === 0) {
290
- return (child) => `<${tag}${serializeAttrsToHTMLString(attrs)}>${serializeChildrenToHTMLString(child)}</${tag}>`;
291
- }
292
- return (child) => `<${tag}${serializeAttrsToHTMLString(attrs)}>${[children].concat(rest).map((a) => domOutputSpecToHTMLString(a)(child)).join("")}</${tag}>`;
293
- }
294
- }
295
- throw new Error(
296
- "[tiptap error]: Unsupported DomOutputSpec type, check the `renderHTML` method output or implement a node mapping",
297
- {
298
- cause: content
299
- }
300
- );
301
- }
302
- function renderToHTMLString({
303
- content,
304
- extensions,
305
- options
306
- }) {
307
- return renderToElement({
308
- renderer: renderJSONContentToString,
309
- domOutputSpecToElement: domOutputSpecToHTMLString,
310
- mapDefinedTypes: {
311
- // Map a doc node to concatenated children
312
- doc: ({ children }) => serializeChildrenToHTMLString(children),
313
- // Map a text node to its text content
314
- text: ({ node }) => {
315
- var _a;
316
- return (_a = node.text) != null ? _a : "";
317
- }
318
- },
319
- content,
320
- extensions,
321
- options
322
- });
323
- }
324
-
325
- // src/pm/markdown/markdown.ts
326
- function renderToMarkdown({
327
- content,
328
- extensions,
329
- options
330
- }) {
331
- return renderToHTMLString({
332
- content,
333
- extensions,
334
- options: {
335
- ...options,
336
- nodeMapping: {
337
- bulletList({ children }) {
338
- return `
339
- ${serializeChildrenToHTMLString(children)}`;
340
- },
341
- orderedList({ children }) {
342
- return `
343
- ${serializeChildrenToHTMLString(children)}`;
344
- },
345
- listItem({ node, children, parent }) {
346
- if ((parent == null ? void 0 : parent.type.name) === "bulletList") {
347
- return `- ${serializeChildrenToHTMLString(children).trim()}
348
- `;
349
- }
350
- if ((parent == null ? void 0 : parent.type.name) === "orderedList") {
351
- let number = parent.attrs.start || 1;
352
- parent.forEach((parentChild, _offset, index) => {
353
- if (node === parentChild) {
354
- number = index + 1;
355
- }
356
- });
357
- return `${number}. ${serializeChildrenToHTMLString(children).trim()}
358
- `;
359
- }
360
- return serializeChildrenToHTMLString(children);
361
- },
362
- paragraph({ children }) {
363
- return `
364
- ${serializeChildrenToHTMLString(children)}
365
- `;
366
- },
367
- heading({ node, children }) {
368
- const level = node.attrs.level;
369
- return `${new Array(level).fill("#").join("")} ${children}
370
- `;
371
- },
372
- codeBlock({ node, children }) {
373
- return `
374
- \`\`\`${node.attrs.language}
375
- ${serializeChildrenToHTMLString(children)}
376
- \`\`\`
377
- `;
378
- },
379
- blockquote({ children }) {
380
- return `
381
- ${serializeChildrenToHTMLString(children).trim().split("\n").map((a) => `> ${a}`).join("\n")}`;
382
- },
383
- image({ node }) {
384
- return `![${node.attrs.alt}](${node.attrs.src})`;
385
- },
386
- hardBreak() {
387
- return "\n";
388
- },
389
- horizontalRule() {
390
- return "\n---\n";
391
- },
392
- table({ children, node }) {
393
- if (!Array.isArray(children)) {
394
- return `
395
- ${serializeChildrenToHTMLString(children)}
396
- `;
397
- }
398
- const columnCount = node.children[0].childCount;
399
- return `
400
- ${serializeChildrenToHTMLString(children[0])}| ${new Array(columnCount).fill("---").join(" | ")} |
401
- ${serializeChildrenToHTMLString(children.slice(1))}
402
- `;
403
- },
404
- tableRow({ children }) {
405
- if (Array.isArray(children)) {
406
- return `| ${children.join(" | ")} |
407
- `;
408
- }
409
- return `${serializeChildrenToHTMLString(children)}
410
- `;
411
- },
412
- tableHeader({ children }) {
413
- return serializeChildrenToHTMLString(children).trim();
414
- },
415
- tableCell({ children }) {
416
- return serializeChildrenToHTMLString(children).trim();
417
- },
418
- ...options == null ? void 0 : options.nodeMapping
419
- },
420
- markMapping: {
421
- bold({ children }) {
422
- return `**${serializeChildrenToHTMLString(children)}**`;
423
- },
424
- italic({ children, node }) {
425
- let isBoldToo = false;
426
- if (node == null ? void 0 : node.marks.some((m) => m.type.name === "bold")) {
427
- isBoldToo = true;
428
- }
429
- if (isBoldToo) {
430
- return `*${serializeChildrenToHTMLString(children)}*`;
431
- }
432
- return `_${serializeChildrenToHTMLString(children)}_`;
433
- },
434
- code({ children }) {
435
- return `\`${serializeChildrenToHTMLString(children)}\``;
436
- },
437
- strike({ children }) {
438
- return `~~${serializeChildrenToHTMLString(children)}~~`;
439
- },
440
- underline({ children }) {
441
- return `<u>${serializeChildrenToHTMLString(children)}</u>`;
442
- },
443
- subscript({ children }) {
444
- return `<sub>${serializeChildrenToHTMLString(children)}</sub>`;
445
- },
446
- superscript({ children }) {
447
- return `<sup>${serializeChildrenToHTMLString(children)}</sup>`;
448
- },
449
- link({ mark, children }) {
450
- return `[${serializeChildrenToHTMLString(children)}](${mark.attrs.href})`;
451
- },
452
- highlight({ children }) {
453
- return `==${serializeChildrenToHTMLString(children)}==`;
454
- },
455
- ...options == null ? void 0 : options.markMapping
456
- }
457
- }
458
- });
459
- }
460
-
461
- // src/pm/react/react.ts
462
- import React2 from "react";
463
- function mapAttrsToHTMLAttributes(attrs, key) {
464
- if (!attrs) {
465
- return { key };
466
- }
467
- return Object.entries(attrs).reduce(
468
- (acc, [name, value]) => {
469
- if (name === "class") {
470
- return Object.assign(acc, { className: value });
471
- }
472
- if (name === "style" && typeof value === "string") {
473
- const styleObject = {};
474
- value.split(";").forEach((style) => {
475
- const [styleKey, val] = style.split(":");
476
- if (styleKey && val) {
477
- const camelCaseKey = styleKey.trim().replace(/-([a-z])/g, (g) => g[1].toUpperCase());
478
- styleObject[camelCaseKey] = val.trim();
479
- }
480
- });
481
- return Object.assign(acc, { style: styleObject });
482
- }
483
- return Object.assign(acc, { [name]: value });
484
- },
485
- { key }
486
- );
487
- }
488
- function domOutputSpecToReactElement(content, key = 0) {
489
- if (typeof content === "string") {
490
- return () => content;
491
- }
492
- if (typeof content === "object" && "length" in content) {
493
- let [tag, attrs, children, ...rest] = content;
494
- const parts = tag.split(" ");
495
- if (parts.length > 1) {
496
- tag = parts[1];
497
- if (attrs === void 0) {
498
- attrs = {
499
- xmlns: parts[0]
500
- };
501
- }
502
- if (attrs === 0) {
503
- attrs = {
504
- xmlns: parts[0]
505
- };
506
- children = 0;
507
- }
508
- if (typeof attrs === "object") {
509
- attrs = Object.assign(attrs, { xmlns: parts[0] });
510
- }
511
- }
512
- if (attrs === void 0) {
513
- return () => React2.createElement(tag, mapAttrsToHTMLAttributes(void 0, key.toString()));
514
- }
515
- if (attrs === 0) {
516
- return (child) => React2.createElement(tag, mapAttrsToHTMLAttributes(void 0, key.toString()), child);
517
- }
518
- if (typeof attrs === "object") {
519
- if (Array.isArray(attrs)) {
520
- if (children === void 0) {
521
- return (child) => React2.createElement(
522
- tag,
523
- mapAttrsToHTMLAttributes(void 0, key.toString()),
524
- domOutputSpecToReactElement(attrs, key++)(child)
525
- );
526
- }
527
- if (children === 0) {
528
- return (child) => React2.createElement(
529
- tag,
530
- mapAttrsToHTMLAttributes(void 0, key.toString()),
531
- domOutputSpecToReactElement(attrs, key++)(child)
532
- );
533
- }
534
- return (child) => React2.createElement(
535
- tag,
536
- mapAttrsToHTMLAttributes(void 0, key.toString()),
537
- domOutputSpecToReactElement(attrs)(child),
538
- [children].concat(rest).map((outputSpec) => domOutputSpecToReactElement(outputSpec, key++)(child))
539
- );
540
- }
541
- if (children === void 0) {
542
- return () => React2.createElement(tag, mapAttrsToHTMLAttributes(attrs, key.toString()));
543
- }
544
- if (children === 0) {
545
- return (child) => React2.createElement(tag, mapAttrsToHTMLAttributes(attrs, key.toString()), child);
546
- }
547
- return (child) => React2.createElement(
548
- tag,
549
- mapAttrsToHTMLAttributes(attrs, key.toString()),
550
- [children].concat(rest).map((outputSpec) => domOutputSpecToReactElement(outputSpec, key++)(child))
551
- );
552
- }
553
- }
554
- throw new Error(
555
- "[tiptap error]: Unsupported DomOutputSpec type, check the `renderHTML` method output or implement a node mapping",
556
- {
557
- cause: content
558
- }
559
- );
560
- }
561
- function renderToReactElement({
562
- content,
563
- extensions,
564
- options
565
- }) {
566
- return renderToElement({
567
- renderer: renderJSONContentToReactElement,
568
- domOutputSpecToElement: domOutputSpecToReactElement,
569
- mapDefinedTypes: {
570
- // Map a doc node to concatenated children
571
- doc: ({ children }) => React2.createElement(React2.Fragment, {}, children),
572
- // Map a text node to its text content
573
- text: ({ node }) => {
574
- var _a;
575
- return (_a = node.text) != null ? _a : "";
576
- }
577
- },
578
- content,
579
- extensions,
580
- options
581
- });
582
- }
583
- export {
584
- TiptapStaticRenderer,
585
- domOutputSpecToHTMLString,
586
- domOutputSpecToReactElement,
587
- getAttributes,
588
- getHTMLAttributes,
589
- mapAttrsToHTMLAttributes,
590
- mapMarkExtensionToReactNode,
591
- mapNodeExtensionToReactNode,
592
- renderJSONContentToReactElement,
593
- renderJSONContentToString,
594
- renderToElement,
595
- renderToHTMLString,
596
- renderToMarkdown,
597
- renderToReactElement,
598
- serializeAttrsToHTMLString,
599
- serializeChildrenToHTMLString
600
- };
601
- //# sourceMappingURL=index.js.map