@tiptap/static-renderer 3.24.0 → 3.26.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 (47) hide show
  1. package/dist/index.cjs +5 -4
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +42 -14
  4. package/dist/index.d.ts +42 -14
  5. package/dist/index.js +5 -4
  6. package/dist/index.js.map +1 -1
  7. package/dist/json/html-string/index.cjs +3 -3
  8. package/dist/json/html-string/index.cjs.map +1 -1
  9. package/dist/json/html-string/index.d.cts +39 -11
  10. package/dist/json/html-string/index.d.ts +39 -11
  11. package/dist/json/html-string/index.js +3 -3
  12. package/dist/json/html-string/index.js.map +1 -1
  13. package/dist/json/react/index.cjs +3 -3
  14. package/dist/json/react/index.cjs.map +1 -1
  15. package/dist/json/react/index.d.cts +39 -11
  16. package/dist/json/react/index.d.ts +39 -11
  17. package/dist/json/react/index.js +3 -3
  18. package/dist/json/react/index.js.map +1 -1
  19. package/dist/json/renderer.cjs +3 -3
  20. package/dist/json/renderer.cjs.map +1 -1
  21. package/dist/json/renderer.d.cts +36 -8
  22. package/dist/json/renderer.d.ts +36 -8
  23. package/dist/json/renderer.js +3 -3
  24. package/dist/json/renderer.js.map +1 -1
  25. package/dist/pm/html-string/index.cjs +3 -3
  26. package/dist/pm/html-string/index.cjs.map +1 -1
  27. package/dist/pm/html-string/index.d.cts +32 -4
  28. package/dist/pm/html-string/index.d.ts +32 -4
  29. package/dist/pm/html-string/index.js +3 -3
  30. package/dist/pm/html-string/index.js.map +1 -1
  31. package/dist/pm/markdown/index.cjs +5 -4
  32. package/dist/pm/markdown/index.cjs.map +1 -1
  33. package/dist/pm/markdown/index.d.cts +32 -4
  34. package/dist/pm/markdown/index.d.ts +32 -4
  35. package/dist/pm/markdown/index.js +5 -4
  36. package/dist/pm/markdown/index.js.map +1 -1
  37. package/dist/pm/react/index.cjs +3 -3
  38. package/dist/pm/react/index.cjs.map +1 -1
  39. package/dist/pm/react/index.d.cts +32 -4
  40. package/dist/pm/react/index.d.ts +32 -4
  41. package/dist/pm/react/index.js +3 -3
  42. package/dist/pm/react/index.js.map +1 -1
  43. package/package.json +5 -5
  44. package/src/json/html-string/string.ts +4 -6
  45. package/src/json/react/react.ts +4 -5
  46. package/src/json/renderer.ts +34 -8
  47. package/src/pm/markdown/markdown.ts +1 -1
@@ -1,5 +1,31 @@
1
1
  /* oslint-disableno-explicit-any */
2
- import type { MarkType, NodeType } from '@tiptap/core'
2
+ import type { JSONContent } from '@tiptap/core'
3
+
4
+ /**
5
+ * A JSON representation of a mark (a Tiptap/ProseMirror mark serialized to JSON).
6
+ */
7
+ export type JSONMarkType = NonNullable<JSONContent['marks']>[number]
8
+
9
+ /**
10
+ * A JSON representation of a node (a Tiptap/ProseMirror node serialized to JSON).
11
+ *
12
+ * `marks` is tied to the `TMark` type parameter so the node<->mark relationship
13
+ * stays sound. This is also why we cannot simply default `TNodeType` to
14
+ * `JSONContent`: the generic constraint references `TMarkType` via
15
+ * `marks?: readonly TMarkType[]`, and `JSONContent.marks` is a *concrete* array,
16
+ * which TypeScript rejects ("TMarkType could be instantiated with a different
17
+ * subtype of constraint"). Parameterizing the node type by the same `TMark`
18
+ * avoids that bivariance error. Please don't "simplify" this back to
19
+ * `JSONContent` — it won't type-check.
20
+ */
21
+ export type JSONNodeType<TMark extends { type: string | { name: string } } = JSONMarkType> = {
22
+ type?: string
23
+ attrs?: Record<string, any>
24
+ content?: JSONNodeType<TMark>[]
25
+ marks?: readonly TMark[]
26
+ text?: string
27
+ [key: string]: any
28
+ }
3
29
 
4
30
  /**
5
31
  * Props for a node renderer
@@ -62,15 +88,15 @@ export type TiptapStaticRendererOptions<
62
88
  /**
63
89
  * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
64
90
  */
65
- TMarkType extends { type: any } = MarkType,
91
+ TMarkType extends { type: any } = JSONMarkType,
66
92
  /**
67
93
  * A node type is either a JSON representation of a node or a Prosemirror node instance
68
94
  */
69
95
  TNodeType extends {
70
96
  content?: { forEach: (cb: (node: TNodeType) => void) => void }
71
97
  marks?: readonly TMarkType[]
72
- type: string | { name: string }
73
- } = NodeType,
98
+ type?: string | { name: string }
99
+ } = JSONNodeType<TMarkType>,
74
100
  /**
75
101
  * A node renderer is a function that takes a node and its children and returns the rendered output
76
102
  */
@@ -123,15 +149,15 @@ export function TiptapStaticRenderer<
123
149
  /**
124
150
  * A mark type is either a JSON representation of a mark or a Prosemirror mark instance
125
151
  */
126
- TMarkType extends { type: string | { name: string } } = MarkType,
152
+ TMarkType extends { type: string | { name: string } } = JSONMarkType,
127
153
  /**
128
154
  * A node type is either a JSON representation of a node or a Prosemirror node instance
129
155
  */
130
156
  TNodeType extends {
131
157
  content?: { forEach: (cb: (node: TNodeType) => void) => void }
132
158
  marks?: readonly TMarkType[]
133
- type: string | { name: string }
134
- } = NodeType,
159
+ type?: string | { name: string }
160
+ } = JSONNodeType<TMarkType>,
135
161
  /**
136
162
  * A node renderer is a function that takes a node and its children and returns the rendered output
137
163
  */
@@ -184,7 +210,7 @@ export function TiptapStaticRenderer<
184
210
  */
185
211
  parent?: TNodeType
186
212
  }): TReturnType {
187
- const nodeType = typeof content.type === 'string' ? content.type : content.type.name
213
+ const nodeType = typeof content.type === 'string' ? content.type : (content.type?.name ?? '')
188
214
  const NodeHandler = nodeMapping[nodeType] ?? unhandledNode
189
215
 
190
216
  if (!NodeHandler) {
@@ -72,7 +72,7 @@ export function renderToMarkdown({
72
72
  .trim()
73
73
  .split('\n')
74
74
  .map(a => `> ${a}`)
75
- .join('\n')}`
75
+ .join('\n')}\n`
76
76
  },
77
77
  image({ node }) {
78
78
  return `![${node.attrs.alt}](${node.attrs.src})`