@timbenniks/contentstack-platform-sdk 0.1.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 (244) hide show
  1. package/dist/auth/index.cjs +607 -0
  2. package/dist/auth/index.cjs.map +1 -0
  3. package/dist/auth/index.d.cts +108 -0
  4. package/dist/auth/index.d.ts +108 -0
  5. package/dist/auth/index.mjs +25 -0
  6. package/dist/auth/index.mjs.map +1 -0
  7. package/dist/brandkit/index.cjs +525 -0
  8. package/dist/brandkit/index.cjs.map +1 -0
  9. package/dist/brandkit/index.d.cts +95 -0
  10. package/dist/brandkit/index.d.ts +95 -0
  11. package/dist/brandkit/index.mjs +11 -0
  12. package/dist/brandkit/index.mjs.map +1 -0
  13. package/dist/chunk-3C6J2BDB.mjs +84 -0
  14. package/dist/chunk-3C6J2BDB.mjs.map +1 -0
  15. package/dist/chunk-3KE63N3I.mjs +64 -0
  16. package/dist/chunk-3KE63N3I.mjs.map +1 -0
  17. package/dist/chunk-4CJ4IVPJ.mjs +212 -0
  18. package/dist/chunk-4CJ4IVPJ.mjs.map +1 -0
  19. package/dist/chunk-4JFUI7MJ.mjs +368 -0
  20. package/dist/chunk-4JFUI7MJ.mjs.map +1 -0
  21. package/dist/chunk-7VFGD32I.mjs +26 -0
  22. package/dist/chunk-7VFGD32I.mjs.map +1 -0
  23. package/dist/chunk-ARPJDW3A.mjs +44 -0
  24. package/dist/chunk-ARPJDW3A.mjs.map +1 -0
  25. package/dist/chunk-AVJHCFRK.mjs +52 -0
  26. package/dist/chunk-AVJHCFRK.mjs.map +1 -0
  27. package/dist/chunk-BK2IBTQS.mjs +131 -0
  28. package/dist/chunk-BK2IBTQS.mjs.map +1 -0
  29. package/dist/chunk-BUZ6CQHE.mjs +75 -0
  30. package/dist/chunk-BUZ6CQHE.mjs.map +1 -0
  31. package/dist/chunk-CKMAOWBQ.mjs +379 -0
  32. package/dist/chunk-CKMAOWBQ.mjs.map +1 -0
  33. package/dist/chunk-DJQLN4TR.mjs +1 -0
  34. package/dist/chunk-DJQLN4TR.mjs.map +1 -0
  35. package/dist/chunk-DMERADWM.mjs +67 -0
  36. package/dist/chunk-DMERADWM.mjs.map +1 -0
  37. package/dist/chunk-EREPKWTW.mjs +926 -0
  38. package/dist/chunk-EREPKWTW.mjs.map +1 -0
  39. package/dist/chunk-FQP4PB5X.mjs +88 -0
  40. package/dist/chunk-FQP4PB5X.mjs.map +1 -0
  41. package/dist/chunk-GNPQJBFX.mjs +144 -0
  42. package/dist/chunk-GNPQJBFX.mjs.map +1 -0
  43. package/dist/chunk-GOSB24M6.mjs +87 -0
  44. package/dist/chunk-GOSB24M6.mjs.map +1 -0
  45. package/dist/chunk-JL2E3EOT.mjs +255 -0
  46. package/dist/chunk-JL2E3EOT.mjs.map +1 -0
  47. package/dist/chunk-JYGZBKTH.mjs +222 -0
  48. package/dist/chunk-JYGZBKTH.mjs.map +1 -0
  49. package/dist/chunk-JZE2W7VW.mjs +52 -0
  50. package/dist/chunk-JZE2W7VW.mjs.map +1 -0
  51. package/dist/chunk-K76DKSHJ.mjs +18 -0
  52. package/dist/chunk-K76DKSHJ.mjs.map +1 -0
  53. package/dist/chunk-KLVIROVU.mjs +232 -0
  54. package/dist/chunk-KLVIROVU.mjs.map +1 -0
  55. package/dist/chunk-LPVVA5J3.mjs +18 -0
  56. package/dist/chunk-LPVVA5J3.mjs.map +1 -0
  57. package/dist/chunk-NKLOZ5VI.mjs +112 -0
  58. package/dist/chunk-NKLOZ5VI.mjs.map +1 -0
  59. package/dist/chunk-QGA4WBDC.mjs +7 -0
  60. package/dist/chunk-QGA4WBDC.mjs.map +1 -0
  61. package/dist/chunk-QW7TVYOA.mjs +56 -0
  62. package/dist/chunk-QW7TVYOA.mjs.map +1 -0
  63. package/dist/chunk-SU5QEKYW.mjs +83 -0
  64. package/dist/chunk-SU5QEKYW.mjs.map +1 -0
  65. package/dist/chunk-T5A2E2RI.mjs +654 -0
  66. package/dist/chunk-T5A2E2RI.mjs.map +1 -0
  67. package/dist/chunk-T5OIJQK7.mjs +116 -0
  68. package/dist/chunk-T5OIJQK7.mjs.map +1 -0
  69. package/dist/chunk-VW7DD6HV.mjs +253 -0
  70. package/dist/chunk-VW7DD6HV.mjs.map +1 -0
  71. package/dist/chunk-XH7NLHGW.mjs +133 -0
  72. package/dist/chunk-XH7NLHGW.mjs.map +1 -0
  73. package/dist/client-DJ5haQGd.d.cts +22 -0
  74. package/dist/client-DwVGVSQz.d.ts +22 -0
  75. package/dist/cma/index.cjs +1349 -0
  76. package/dist/cma/index.cjs.map +1 -0
  77. package/dist/cma/index.d.cts +22 -0
  78. package/dist/cma/index.d.ts +22 -0
  79. package/dist/cma/index.mjs +70 -0
  80. package/dist/cma/index.mjs.map +1 -0
  81. package/dist/errors-CAw-IRCP.d.cts +65 -0
  82. package/dist/errors-CAw-IRCP.d.ts +65 -0
  83. package/dist/generative-ai/index.cjs +401 -0
  84. package/dist/generative-ai/index.cjs.map +1 -0
  85. package/dist/generative-ai/index.d.cts +31 -0
  86. package/dist/generative-ai/index.d.ts +31 -0
  87. package/dist/generative-ai/index.mjs +10 -0
  88. package/dist/generative-ai/index.mjs.map +1 -0
  89. package/dist/images/index.cjs +185 -0
  90. package/dist/images/index.cjs.map +1 -0
  91. package/dist/images/index.d.cts +27 -0
  92. package/dist/images/index.d.ts +27 -0
  93. package/dist/images/index.mjs +8 -0
  94. package/dist/images/index.mjs.map +1 -0
  95. package/dist/index.cjs +2909 -0
  96. package/dist/index.cjs.map +1 -0
  97. package/dist/index.d.cts +30 -0
  98. package/dist/index.d.ts +30 -0
  99. package/dist/index.mjs +153 -0
  100. package/dist/index.mjs.map +1 -0
  101. package/dist/knowledge-vault/index.cjs +413 -0
  102. package/dist/knowledge-vault/index.cjs.map +1 -0
  103. package/dist/knowledge-vault/index.d.cts +49 -0
  104. package/dist/knowledge-vault/index.d.ts +49 -0
  105. package/dist/knowledge-vault/index.mjs +10 -0
  106. package/dist/knowledge-vault/index.mjs.map +1 -0
  107. package/dist/launch/index.cjs +624 -0
  108. package/dist/launch/index.cjs.map +1 -0
  109. package/dist/launch/index.d.cts +169 -0
  110. package/dist/launch/index.d.ts +169 -0
  111. package/dist/launch/index.mjs +11 -0
  112. package/dist/launch/index.mjs.map +1 -0
  113. package/dist/react/auth/index.cjs +113 -0
  114. package/dist/react/auth/index.cjs.map +1 -0
  115. package/dist/react/auth/index.d.cts +33 -0
  116. package/dist/react/auth/index.d.ts +33 -0
  117. package/dist/react/auth/index.mjs +13 -0
  118. package/dist/react/auth/index.mjs.map +1 -0
  119. package/dist/react/content/index.cjs +113 -0
  120. package/dist/react/content/index.cjs.map +1 -0
  121. package/dist/react/content/index.d.cts +25 -0
  122. package/dist/react/content/index.d.ts +25 -0
  123. package/dist/react/content/index.mjs +7 -0
  124. package/dist/react/content/index.mjs.map +1 -0
  125. package/dist/react/hooks/index.cjs +2097 -0
  126. package/dist/react/hooks/index.cjs.map +1 -0
  127. package/dist/react/hooks/index.d.cts +77 -0
  128. package/dist/react/hooks/index.d.ts +77 -0
  129. package/dist/react/hooks/index.mjs +46 -0
  130. package/dist/react/hooks/index.mjs.map +1 -0
  131. package/dist/react/index.cjs +2307 -0
  132. package/dist/react/index.cjs.map +1 -0
  133. package/dist/react/index.d.cts +16 -0
  134. package/dist/react/index.d.ts +16 -0
  135. package/dist/react/index.mjs +67 -0
  136. package/dist/react/index.mjs.map +1 -0
  137. package/dist/react/provider/index.cjs +83 -0
  138. package/dist/react/provider/index.cjs.map +1 -0
  139. package/dist/react/provider/index.d.cts +30 -0
  140. package/dist/react/provider/index.d.ts +30 -0
  141. package/dist/react/provider/index.mjs +11 -0
  142. package/dist/react/provider/index.mjs.map +1 -0
  143. package/dist/regions/index.cjs +171 -0
  144. package/dist/regions/index.cjs.map +1 -0
  145. package/dist/regions/index.d.cts +55 -0
  146. package/dist/regions/index.d.ts +55 -0
  147. package/dist/regions/index.mjs +13 -0
  148. package/dist/regions/index.mjs.map +1 -0
  149. package/dist/request-builders-BxeolQIw.d.ts +735 -0
  150. package/dist/request-builders-C2IG1LUo.d.cts +735 -0
  151. package/dist/rte/index.cjs +683 -0
  152. package/dist/rte/index.cjs.map +1 -0
  153. package/dist/rte/index.d.cts +33 -0
  154. package/dist/rte/index.d.ts +33 -0
  155. package/dist/rte/index.mjs +17 -0
  156. package/dist/rte/index.mjs.map +1 -0
  157. package/dist/server/index.cjs +917 -0
  158. package/dist/server/index.cjs.map +1 -0
  159. package/dist/server/index.d.cts +6 -0
  160. package/dist/server/index.d.ts +6 -0
  161. package/dist/server/index.mjs +45 -0
  162. package/dist/server/index.mjs.map +1 -0
  163. package/dist/server/middleware/index.cjs +614 -0
  164. package/dist/server/middleware/index.cjs.map +1 -0
  165. package/dist/server/middleware/index.d.cts +77 -0
  166. package/dist/server/middleware/index.d.ts +77 -0
  167. package/dist/server/middleware/index.mjs +27 -0
  168. package/dist/server/middleware/index.mjs.map +1 -0
  169. package/dist/server/proxy/index.cjs +329 -0
  170. package/dist/server/proxy/index.cjs.map +1 -0
  171. package/dist/server/proxy/index.d.cts +143 -0
  172. package/dist/server/proxy/index.d.ts +143 -0
  173. package/dist/server/proxy/index.mjs +29 -0
  174. package/dist/server/proxy/index.mjs.map +1 -0
  175. package/dist/server/webhooks/index.cjs +131 -0
  176. package/dist/server/webhooks/index.cjs.map +1 -0
  177. package/dist/server/webhooks/index.d.cts +230 -0
  178. package/dist/server/webhooks/index.d.ts +230 -0
  179. package/dist/server/webhooks/index.mjs +23 -0
  180. package/dist/server/webhooks/index.mjs.map +1 -0
  181. package/dist/types-6D9VR7pT.d.cts +26 -0
  182. package/dist/types-AelT0rFJ.d.cts +21 -0
  183. package/dist/types-AelT0rFJ.d.ts +21 -0
  184. package/dist/types-Bu5yCgmw.d.ts +26 -0
  185. package/dist/types-DgixK-ll.d.cts +23 -0
  186. package/dist/types-DgixK-ll.d.ts +23 -0
  187. package/dist/types-DrMwdlH9.d.cts +245 -0
  188. package/dist/types-DrMwdlH9.d.ts +245 -0
  189. package/dist/ui/css/index.cjs +31 -0
  190. package/dist/ui/css/index.cjs.map +1 -0
  191. package/dist/ui/css/index.d.cts +15 -0
  192. package/dist/ui/css/index.d.ts +15 -0
  193. package/dist/ui/css/index.mjs +7 -0
  194. package/dist/ui/css/index.mjs.map +1 -0
  195. package/dist/ui/index.cjs +368 -0
  196. package/dist/ui/index.cjs.map +1 -0
  197. package/dist/ui/index.d.cts +4 -0
  198. package/dist/ui/index.d.ts +4 -0
  199. package/dist/ui/index.mjs +33 -0
  200. package/dist/ui/index.mjs.map +1 -0
  201. package/dist/ui/theme/index.cjs +105 -0
  202. package/dist/ui/theme/index.cjs.map +1 -0
  203. package/dist/ui/theme/index.d.cts +33 -0
  204. package/dist/ui/theme/index.d.ts +33 -0
  205. package/dist/ui/theme/index.mjs +15 -0
  206. package/dist/ui/theme/index.mjs.map +1 -0
  207. package/dist/ui/tokens/index.cjs +286 -0
  208. package/dist/ui/tokens/index.cjs.map +1 -0
  209. package/dist/ui/tokens/index.d.cts +54 -0
  210. package/dist/ui/tokens/index.d.ts +54 -0
  211. package/dist/ui/tokens/index.mjs +17 -0
  212. package/dist/ui/tokens/index.mjs.map +1 -0
  213. package/dist/ui/tokens.css +408 -0
  214. package/dist/vue/auth/index.cjs +141 -0
  215. package/dist/vue/auth/index.cjs.map +1 -0
  216. package/dist/vue/auth/index.d.cts +78 -0
  217. package/dist/vue/auth/index.d.ts +78 -0
  218. package/dist/vue/auth/index.mjs +13 -0
  219. package/dist/vue/auth/index.mjs.map +1 -0
  220. package/dist/vue/composables/index.cjs +2108 -0
  221. package/dist/vue/composables/index.cjs.map +1 -0
  222. package/dist/vue/composables/index.d.cts +78 -0
  223. package/dist/vue/composables/index.d.ts +78 -0
  224. package/dist/vue/composables/index.mjs +46 -0
  225. package/dist/vue/composables/index.mjs.map +1 -0
  226. package/dist/vue/content/index.cjs +142 -0
  227. package/dist/vue/content/index.cjs.map +1 -0
  228. package/dist/vue/content/index.d.cts +47 -0
  229. package/dist/vue/content/index.d.ts +47 -0
  230. package/dist/vue/content/index.mjs +7 -0
  231. package/dist/vue/content/index.mjs.map +1 -0
  232. package/dist/vue/index.cjs +2389 -0
  233. package/dist/vue/index.cjs.map +1 -0
  234. package/dist/vue/index.d.cts +16 -0
  235. package/dist/vue/index.d.ts +16 -0
  236. package/dist/vue/index.mjs +69 -0
  237. package/dist/vue/index.mjs.map +1 -0
  238. package/dist/vue/provider/index.cjs +97 -0
  239. package/dist/vue/provider/index.cjs.map +1 -0
  240. package/dist/vue/provider/index.d.cts +90 -0
  241. package/dist/vue/provider/index.d.ts +90 -0
  242. package/dist/vue/provider/index.mjs +13 -0
  243. package/dist/vue/provider/index.mjs.map +1 -0
  244. package/package.json +195 -0
@@ -0,0 +1,654 @@
1
+ // src/rte/to-html.ts
2
+ import {
3
+ jsonToHtml
4
+ } from "@contentstack/json-rte-serializer";
5
+
6
+ // src/rte/utils.ts
7
+ var KNOWN_RTE_TYPES = /* @__PURE__ */ new Set([
8
+ "doc",
9
+ "p",
10
+ "h1",
11
+ "h2",
12
+ "h3",
13
+ "h4",
14
+ "h5",
15
+ "h6",
16
+ "ul",
17
+ "ol",
18
+ "li",
19
+ "blockquote",
20
+ "code",
21
+ "img",
22
+ "a",
23
+ "table",
24
+ "tr",
25
+ "td",
26
+ "th",
27
+ "hr",
28
+ "reference"
29
+ ]);
30
+ function isRecord(value) {
31
+ return typeof value === "object" && value !== null;
32
+ }
33
+ function isTextNode(value) {
34
+ return isRecord(value) && typeof value.text === "string" && typeof value.type !== "string";
35
+ }
36
+ function isElementNode(value) {
37
+ return isRecord(value) && typeof value.type === "string" && typeof value.uid === "string";
38
+ }
39
+ function decodeHTML(value) {
40
+ return value.replace(/&nbsp;/g, " ").replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
41
+ }
42
+ function createUid() {
43
+ const uuid = globalThis.crypto?.randomUUID;
44
+ if (typeof uuid === "function") {
45
+ return uuid.call(globalThis.crypto);
46
+ }
47
+ return `rte-${Math.random().toString(36).slice(2, 10)}`;
48
+ }
49
+
50
+ // src/rte/serializer.ts
51
+ var DEFAULT_TABLE_COLUMN_WIDTH = 250;
52
+ function normalizeForSerializer(doc) {
53
+ const normalized = normalizeNode(doc);
54
+ if (!isTextNode(normalized) && normalized.type === "doc") {
55
+ return normalized;
56
+ }
57
+ return {
58
+ ...doc,
59
+ children: []
60
+ };
61
+ }
62
+ function normalizeNode(node) {
63
+ if (isTextNode(node)) {
64
+ return { ...node };
65
+ }
66
+ const children = node.children?.map((child) => normalizeNode(child));
67
+ const attrs = isRecord(node.attrs) ? { ...node.attrs } : {};
68
+ if (node.type === "a" && typeof attrs.url !== "string" && typeof attrs.href === "string") {
69
+ attrs.url = attrs.href;
70
+ }
71
+ if (node.type === "img" && typeof attrs.url !== "string" && typeof attrs.src === "string") {
72
+ attrs.url = attrs.src;
73
+ }
74
+ if (node.type === "table") {
75
+ const tableRows = (children ?? []).filter(
76
+ (row) => !isTextNode(row) && row.type === "tr"
77
+ );
78
+ const inferredCols = tableRows.reduce((max, row) => {
79
+ const columnCount = (row.children ?? []).filter(
80
+ (cell) => !isTextNode(cell) && (cell.type === "td" || cell.type === "th")
81
+ ).length;
82
+ return Math.max(max, columnCount);
83
+ }, 0);
84
+ const normalizedCols = typeof attrs.cols === "number" && attrs.cols > 0 ? attrs.cols : inferredCols;
85
+ const normalizedRows = typeof attrs.rows === "number" && attrs.rows > 0 ? attrs.rows : tableRows.length;
86
+ const normalizedWidths = Array.isArray(attrs.colWidths) && attrs.colWidths.length > 0 && attrs.colWidths.every((width) => typeof width === "number") ? attrs.colWidths : Array.from({ length: Math.max(normalizedCols, 1) }, () => DEFAULT_TABLE_COLUMN_WIDTH);
87
+ return {
88
+ ...node,
89
+ attrs: {
90
+ ...attrs,
91
+ rows: normalizedRows,
92
+ cols: Math.max(normalizedCols, 1),
93
+ colWidths: normalizedWidths
94
+ },
95
+ ...children ? { children } : {}
96
+ };
97
+ }
98
+ return {
99
+ ...node,
100
+ attrs,
101
+ ...children ? { children } : {}
102
+ };
103
+ }
104
+
105
+ // src/rte/to-html.ts
106
+ function toHTML(doc, options = {}) {
107
+ return jsonToHtml(
108
+ normalizeForSerializer(doc),
109
+ buildSerializerOptions(options)
110
+ );
111
+ }
112
+ function buildSerializerOptions(options) {
113
+ const { renderEntry, renderAsset, serializerOptions } = options;
114
+ if (!renderEntry && !renderAsset) {
115
+ return serializerOptions;
116
+ }
117
+ const customElementTypes = {
118
+ ...serializerOptions?.customElementTypes ?? {}
119
+ };
120
+ const existingReferenceRenderer = customElementTypes.reference;
121
+ customElementTypes.reference = (attrs, child, jsonBlock, extraProps) => {
122
+ const block = asRTENode(jsonBlock);
123
+ const referenceAttrs = isRecord(block.attrs) ? block.attrs : {};
124
+ if (referenceAttrs.type === "entry" && renderEntry) {
125
+ return renderEntry(referenceAttrs, block);
126
+ }
127
+ if (referenceAttrs.type === "asset" && renderAsset) {
128
+ return renderAsset(referenceAttrs, block);
129
+ }
130
+ return typeof existingReferenceRenderer === "function" ? existingReferenceRenderer(attrs, child, jsonBlock, extraProps) : "";
131
+ };
132
+ return {
133
+ ...serializerOptions,
134
+ customElementTypes
135
+ };
136
+ }
137
+ function asRTENode(value) {
138
+ if (isRecord(value) && typeof value.type === "string" && typeof value.uid === "string") {
139
+ return value;
140
+ }
141
+ return {
142
+ type: "reference",
143
+ uid: "reference"
144
+ };
145
+ }
146
+
147
+ // src/rte/to-markdown.ts
148
+ import { jsonToMarkdown } from "@contentstack/json-rte-serializer";
149
+ function toMarkdown(doc, options = {}) {
150
+ const mappedDoc = options.renderEntry || options.renderAsset ? mapReferenceNodes(doc, options) : doc;
151
+ const markdown = jsonToMarkdown(normalizeForSerializer(mappedDoc)).trim();
152
+ return normalizeMarkdownOutput(markdown);
153
+ }
154
+ function mapReferenceNodes(doc, options) {
155
+ const mapped = mapNode(doc, options);
156
+ if (isTextNode(mapped)) {
157
+ return {
158
+ ...doc,
159
+ children: []
160
+ };
161
+ }
162
+ return {
163
+ ...doc,
164
+ ...mapped,
165
+ type: "doc",
166
+ uid: mapped.uid ?? doc.uid
167
+ };
168
+ }
169
+ function mapNode(node, options) {
170
+ if (isTextNode(node)) {
171
+ return { ...node };
172
+ }
173
+ if (node.type === "reference") {
174
+ const attrs = node.attrs ?? {};
175
+ if (attrs.type === "entry" && options.renderEntry) {
176
+ return { text: options.renderEntry(attrs, node) };
177
+ }
178
+ if (attrs.type === "asset" && options.renderAsset) {
179
+ return { text: options.renderAsset(attrs, node) };
180
+ }
181
+ return { text: "" };
182
+ }
183
+ return {
184
+ ...node,
185
+ ...node.children ? { children: node.children.map((child) => mapNode(child, options)) } : {}
186
+ };
187
+ }
188
+ function normalizeMarkdownOutput(markdown) {
189
+ return markdown.replace(/!\[([^\]]*)\]\s*\n\s*\(([^)\n]+)\)/g, "![$1]($2)");
190
+ }
191
+
192
+ // src/rte/to-plaintext.ts
193
+ var DOUBLE_BREAK_TYPES = /* @__PURE__ */ new Set([
194
+ "p",
195
+ "h1",
196
+ "h2",
197
+ "h3",
198
+ "h4",
199
+ "h5",
200
+ "h6",
201
+ "blockquote",
202
+ "code",
203
+ "ul",
204
+ "ol",
205
+ "table"
206
+ ]);
207
+ var SINGLE_BREAK_TYPES = /* @__PURE__ */ new Set(["li", "tr", "td", "th"]);
208
+ function toPlainText(doc) {
209
+ return renderNode(doc).replace(/\n{3,}/g, "\n\n").trim();
210
+ }
211
+ function renderNode(node) {
212
+ if (isTextNode(node)) {
213
+ return node.text;
214
+ }
215
+ const children = (node.children ?? []).map((child) => renderNode(child)).join("");
216
+ if (node.type === "doc") {
217
+ return children;
218
+ }
219
+ if (node.type === "img") {
220
+ const alt = node.attrs?.alt;
221
+ if (typeof alt === "string" && alt.trim()) {
222
+ return `${alt}
223
+
224
+ `;
225
+ }
226
+ return "\n";
227
+ }
228
+ if (node.type === "a") {
229
+ const href = node.attrs?.href;
230
+ const text = children || (typeof href === "string" ? href : "");
231
+ return text;
232
+ }
233
+ if (node.type === "hr") {
234
+ return "\n";
235
+ }
236
+ if (node.type === "reference") {
237
+ return "";
238
+ }
239
+ if (DOUBLE_BREAK_TYPES.has(node.type)) {
240
+ return `${children}
241
+
242
+ `;
243
+ }
244
+ if (SINGLE_BREAK_TYPES.has(node.type)) {
245
+ return `${children}
246
+ `;
247
+ }
248
+ return children;
249
+ }
250
+
251
+ // src/rte/from-html.ts
252
+ import { htmlToJson } from "@contentstack/json-rte-serializer";
253
+ var VOID_TAGS = /* @__PURE__ */ new Set(["img", "hr", "br", "meta", "link", "input"]);
254
+ function fromHTML(html, options = {}) {
255
+ const parsed = parseWithSerializer(html, options);
256
+ if (parsed) {
257
+ return parsed;
258
+ }
259
+ return parseWithFallback(html);
260
+ }
261
+ function parseWithSerializer(html, options) {
262
+ try {
263
+ const parsed = htmlToJson(html, options.serializerOptions);
264
+ return normalizeDoc(parsed);
265
+ } catch {
266
+ return null;
267
+ }
268
+ }
269
+ function normalizeDoc(value) {
270
+ if (isRecord(value) && value.type === "doc" && Array.isArray(value.children)) {
271
+ return {
272
+ ...value,
273
+ type: "doc",
274
+ uid: typeof value.uid === "string" && value.uid.length > 0 ? value.uid : createUid(),
275
+ children: value.children
276
+ };
277
+ }
278
+ return null;
279
+ }
280
+ function parseWithFallback(html) {
281
+ const root = parseHTML(html);
282
+ const children = convertNodes(root.children);
283
+ return {
284
+ type: "doc",
285
+ uid: createUid(),
286
+ children
287
+ };
288
+ }
289
+ function parseHTML(html) {
290
+ const root = {
291
+ kind: "element",
292
+ tag: "root",
293
+ attrs: {},
294
+ children: []
295
+ };
296
+ const stack = [root];
297
+ const tokenPattern = /<!--[\s\S]*?-->|<\/?[^>]+>|[^<]+/g;
298
+ const tokens = html.match(tokenPattern) ?? [];
299
+ for (const token of tokens) {
300
+ if (!token) {
301
+ continue;
302
+ }
303
+ if (token.startsWith("<!--")) {
304
+ continue;
305
+ }
306
+ if (!token.startsWith("<")) {
307
+ const text = decodeHTML(token);
308
+ if (text.length === 0) {
309
+ continue;
310
+ }
311
+ const parent2 = stack[stack.length - 1];
312
+ if (!parent2) {
313
+ continue;
314
+ }
315
+ parent2.children.push({
316
+ kind: "text",
317
+ value: text
318
+ });
319
+ continue;
320
+ }
321
+ if (token.startsWith("</")) {
322
+ const closingTag = token.slice(2, -1).trim().toLowerCase();
323
+ while (stack.length > 1) {
324
+ const current = stack.pop();
325
+ if (current?.tag === closingTag) {
326
+ break;
327
+ }
328
+ }
329
+ continue;
330
+ }
331
+ const parsed = parseOpeningTag(token);
332
+ if (!parsed) {
333
+ continue;
334
+ }
335
+ const parent = stack[stack.length - 1];
336
+ if (!parent) {
337
+ continue;
338
+ }
339
+ parent.children.push(parsed.element);
340
+ const isVoid = parsed.selfClosing || VOID_TAGS.has(parsed.element.tag);
341
+ if (!isVoid) {
342
+ stack.push(parsed.element);
343
+ }
344
+ }
345
+ return root;
346
+ }
347
+ function parseOpeningTag(token) {
348
+ const isSelfClosing = token.endsWith("/>");
349
+ const content = token.slice(1, token.length - (isSelfClosing ? 2 : 1)).trim();
350
+ if (!content) {
351
+ return null;
352
+ }
353
+ const tagMatch = content.match(/^([a-zA-Z0-9-]+)/);
354
+ if (!tagMatch?.[1]) {
355
+ return null;
356
+ }
357
+ const tag = tagMatch[1].toLowerCase();
358
+ const attrs = parseAttributes(content.slice(tag.length).trim());
359
+ return {
360
+ element: {
361
+ kind: "element",
362
+ tag,
363
+ attrs,
364
+ children: []
365
+ },
366
+ selfClosing: isSelfClosing
367
+ };
368
+ }
369
+ function parseAttributes(input) {
370
+ const attrs = {};
371
+ const attrPattern = /([:@a-zA-Z0-9_-]+)(?:\s*=\s*("([^"]*)"|'([^']*)'|([^\s"'>/]+)))?/g;
372
+ let match = attrPattern.exec(input);
373
+ while (match) {
374
+ const key = match[1];
375
+ if (key) {
376
+ const value = decodeHTML(match[3] ?? match[4] ?? match[5] ?? "");
377
+ attrs[key] = value;
378
+ }
379
+ match = attrPattern.exec(input);
380
+ }
381
+ return attrs;
382
+ }
383
+ function convertNodes(nodes, marks = {}) {
384
+ const output = [];
385
+ for (const node of nodes) {
386
+ if (node.kind === "text") {
387
+ const cleaned = normalizeText(node.value);
388
+ if (!cleaned) {
389
+ continue;
390
+ }
391
+ output.push({
392
+ text: cleaned,
393
+ ...marks.bold ? { bold: true } : {},
394
+ ...marks.italic ? { italic: true } : {},
395
+ ...marks.underline ? { underline: true } : {},
396
+ ...marks.strikethrough ? { strikethrough: true } : {},
397
+ ...marks.code ? { code: true } : {},
398
+ ...marks.superscript ? { superscript: true } : {},
399
+ ...marks.subscript ? { subscript: true } : {}
400
+ });
401
+ continue;
402
+ }
403
+ output.push(...convertElement(node, marks));
404
+ }
405
+ return output;
406
+ }
407
+ function convertElement(element, marks) {
408
+ switch (element.tag) {
409
+ case "strong":
410
+ case "b":
411
+ return convertNodes(element.children, { ...marks, bold: true });
412
+ case "em":
413
+ case "i":
414
+ return convertNodes(element.children, { ...marks, italic: true });
415
+ case "u":
416
+ return convertNodes(element.children, { ...marks, underline: true });
417
+ case "s":
418
+ case "strike":
419
+ case "del":
420
+ return convertNodes(element.children, { ...marks, strikethrough: true });
421
+ case "sup":
422
+ return convertNodes(element.children, { ...marks, superscript: true });
423
+ case "sub":
424
+ return convertNodes(element.children, { ...marks, subscript: true });
425
+ case "code":
426
+ return convertNodes(element.children, { ...marks, code: true });
427
+ case "br":
428
+ return [{ text: "\n" }];
429
+ case "img":
430
+ return [
431
+ {
432
+ type: "img",
433
+ uid: createUid(),
434
+ attrs: {
435
+ src: element.attrs.src ?? "",
436
+ alt: element.attrs.alt ?? ""
437
+ }
438
+ }
439
+ ];
440
+ case "hr":
441
+ return [
442
+ {
443
+ type: "hr",
444
+ uid: createUid()
445
+ }
446
+ ];
447
+ case "a":
448
+ return [
449
+ {
450
+ type: "a",
451
+ uid: createUid(),
452
+ attrs: {
453
+ href: element.attrs.href ?? "",
454
+ ...element.attrs.target ? { target: element.attrs.target } : {}
455
+ },
456
+ children: convertNodes(element.children)
457
+ }
458
+ ];
459
+ case "reference":
460
+ return [
461
+ {
462
+ type: "reference",
463
+ uid: createUid(),
464
+ attrs: {
465
+ ...element.attrs
466
+ }
467
+ }
468
+ ];
469
+ case "pre": {
470
+ const codeText = extractTextContent(element);
471
+ return [
472
+ {
473
+ type: "code",
474
+ uid: createUid(),
475
+ children: codeText ? [{ text: codeText }] : []
476
+ }
477
+ ];
478
+ }
479
+ case "p":
480
+ case "h1":
481
+ case "h2":
482
+ case "h3":
483
+ case "h4":
484
+ case "h5":
485
+ case "h6":
486
+ case "ul":
487
+ case "ol":
488
+ case "li":
489
+ case "blockquote":
490
+ case "table":
491
+ case "tr":
492
+ case "td":
493
+ case "th":
494
+ return [
495
+ {
496
+ type: element.tag,
497
+ uid: createUid(),
498
+ children: convertNodes(element.children)
499
+ }
500
+ ];
501
+ default:
502
+ return convertNodes(element.children, marks);
503
+ }
504
+ }
505
+ function extractTextContent(node) {
506
+ if (node.kind === "text") {
507
+ return decodeHTML(node.value);
508
+ }
509
+ return node.children.map((child) => extractTextContent(child)).join("");
510
+ }
511
+ function normalizeText(value) {
512
+ const collapsed = value.replace(/\s+/g, " ");
513
+ if (collapsed.trim().length === 0) {
514
+ return " ";
515
+ }
516
+ return collapsed;
517
+ }
518
+
519
+ // src/rte/transform.ts
520
+ function transform(doc, visitors) {
521
+ const transformed = visitNode(doc, visitors);
522
+ if (!transformed || isTextNode(transformed) || transformed.type !== "doc") {
523
+ return {
524
+ ...doc,
525
+ children: []
526
+ };
527
+ }
528
+ return transformed;
529
+ }
530
+ function visitNode(node, visitors) {
531
+ if (isTextNode(node)) {
532
+ return { ...node };
533
+ }
534
+ const nextChildren = (node.children ?? []).map((child) => visitNode(child, visitors)).filter((child) => Boolean(child));
535
+ const withChildren = {
536
+ ...node,
537
+ ...node.children ? { children: nextChildren } : {}
538
+ };
539
+ const visitor = visitors[withChildren.type];
540
+ if (!visitor) {
541
+ return withChildren;
542
+ }
543
+ return visitor(withChildren);
544
+ }
545
+
546
+ // src/rte/validate.ts
547
+ function validate(doc) {
548
+ const errors = [];
549
+ if (!isElementNode(doc)) {
550
+ errors.push("Document must be an object node with type and uid");
551
+ return { valid: false, errors };
552
+ }
553
+ if (doc.type !== "doc") {
554
+ errors.push('Document root node must have type "doc"');
555
+ }
556
+ if (!Array.isArray(doc.children)) {
557
+ errors.push('Document root node must include a "children" array');
558
+ } else {
559
+ validateChildren(doc.children, "doc.children", errors);
560
+ }
561
+ return { valid: errors.length === 0, errors };
562
+ }
563
+ function validateChildren(children, path, errors) {
564
+ children.forEach((child, index) => {
565
+ validateNode(child, `${path}[${index}]`, errors);
566
+ });
567
+ }
568
+ function validateNode(node, path, errors) {
569
+ if (isTextNode(node)) {
570
+ if (typeof node.text !== "string") {
571
+ errors.push(`${path} must have a string text value`);
572
+ }
573
+ return;
574
+ }
575
+ if (!isElementNode(node)) {
576
+ errors.push(`${path} must be a valid element node or text node`);
577
+ return;
578
+ }
579
+ validateElementNode(node, path, errors);
580
+ }
581
+ function validateElementNode(node, path, errors) {
582
+ if (!KNOWN_RTE_TYPES.has(node.type)) {
583
+ errors.push(`${path}.type "${node.type}" is not a supported RTE node type`);
584
+ }
585
+ if (typeof node.uid !== "string" || node.uid.trim().length === 0) {
586
+ errors.push(`${path}.uid must be a non-empty string`);
587
+ }
588
+ if (node.children !== void 0) {
589
+ if (!Array.isArray(node.children)) {
590
+ errors.push(`${path}.children must be an array when provided`);
591
+ } else {
592
+ validateChildren(node.children, `${path}.children`, errors);
593
+ }
594
+ }
595
+ switch (node.type) {
596
+ case "img":
597
+ validateImgNode(node, path, errors);
598
+ break;
599
+ case "a":
600
+ validateAnchorNode(node, path, errors);
601
+ break;
602
+ case "reference":
603
+ validateReferenceNode(node, path, errors);
604
+ break;
605
+ case "hr":
606
+ if (Array.isArray(node.children) && node.children.length > 0) {
607
+ errors.push(`${path}.children must be empty for hr nodes`);
608
+ }
609
+ break;
610
+ }
611
+ }
612
+ function validateImgNode(node, path, errors) {
613
+ if (!isRecord(node.attrs) || typeof node.attrs.src !== "string" || node.attrs.src.length === 0) {
614
+ errors.push(`${path}.attrs.src must be a non-empty string for img nodes`);
615
+ }
616
+ if (isRecord(node.attrs) && node.attrs.alt !== void 0 && typeof node.attrs.alt !== "string") {
617
+ errors.push(`${path}.attrs.alt must be a string when provided`);
618
+ }
619
+ }
620
+ function validateAnchorNode(node, path, errors) {
621
+ if (!isRecord(node.attrs) || typeof node.attrs.href !== "string" || node.attrs.href.length === 0) {
622
+ errors.push(`${path}.attrs.href must be a non-empty string for a nodes`);
623
+ }
624
+ if (isRecord(node.attrs) && node.attrs.target !== void 0 && typeof node.attrs.target !== "string") {
625
+ errors.push(`${path}.attrs.target must be a string when provided`);
626
+ }
627
+ }
628
+ function validateReferenceNode(node, path, errors) {
629
+ if (!isRecord(node.attrs)) {
630
+ errors.push(`${path}.attrs must be present for reference nodes`);
631
+ return;
632
+ }
633
+ const refType = node.attrs.type;
634
+ if (refType !== "entry" && refType !== "asset") {
635
+ errors.push(`${path}.attrs.type must be "entry" or "asset" for reference nodes`);
636
+ return;
637
+ }
638
+ if (refType === "entry" && typeof node.attrs["entry-uid"] !== "string") {
639
+ errors.push(`${path}.attrs["entry-uid"] must be provided for entry references`);
640
+ }
641
+ if (refType === "asset" && typeof node.attrs["asset-uid"] !== "string") {
642
+ errors.push(`${path}.attrs["asset-uid"] must be provided for asset references`);
643
+ }
644
+ }
645
+
646
+ export {
647
+ toHTML,
648
+ toMarkdown,
649
+ toPlainText,
650
+ fromHTML,
651
+ transform,
652
+ validate
653
+ };
654
+ //# sourceMappingURL=chunk-T5A2E2RI.mjs.map