magic-editor-x 2.2.0 → 2.3.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 (94) hide show
  1. package/CHANGELOG.md +72 -0
  2. package/README.md +493 -168
  3. package/dist/admin/{ConfigurationPage-3Dir3dAf.mjs → ConfigurationPage-BxLHIQdo.mjs} +450 -21
  4. package/dist/admin/{ConfigurationPage-YvnPnna6.js → ConfigurationPage-C-UktL1x.js} +479 -50
  5. package/dist/admin/ConfirmAction-DuQwvRYi.mjs +125 -0
  6. package/dist/admin/ConfirmAction-zLuIFj8t.js +124 -0
  7. package/dist/admin/{CustomBlocksPage-BiiL2ORG.js → CustomBlocksPage-CxcTfdOT.js} +135 -84
  8. package/dist/admin/{CustomBlocksPage-CR2BaP9O.mjs → CustomBlocksPage-D8aMbVXc.mjs} +92 -41
  9. package/dist/admin/DraftSync-BIA8cZgA.js +11 -0
  10. package/dist/admin/DraftSync-CUxOMG-E.mjs +12 -0
  11. package/dist/admin/TemplatesPage-Hn6gyu2l.js +993 -0
  12. package/dist/admin/TemplatesPage-jhYi6Zdf.mjs +993 -0
  13. package/dist/admin/{de-BmmLBJli.mjs → de-1cFMs-4c.mjs} +2 -0
  14. package/dist/admin/{de-BteRx2iA.js → de-2Ie5jy7j.js} +79 -1
  15. package/dist/admin/{de-62Vxb2iw.js → de-B1X46JJZ.js} +205 -2
  16. package/dist/admin/{de-CFX5atOk.mjs → de-BMY2LBaa.mjs} +79 -1
  17. package/dist/admin/{de-Bt6pYIWi.mjs → de-BUS1hUw0.mjs} +145 -0
  18. package/dist/admin/{de-CVbi685U.js → de-C3AsGauQ.js} +145 -0
  19. package/dist/admin/{de-BVvwSYYO.mjs → de-DOiGaTNX.mjs} +205 -2
  20. package/dist/admin/{de-DaJwN6nk.js → de-Dn8athM8.js} +2 -0
  21. package/dist/admin/{en-CyjAcccB.js → en-Ah-Cw0Wd.js} +79 -1
  22. package/dist/admin/{en-BX362fTp.js → en-BkEjpC3i.js} +145 -0
  23. package/dist/admin/{en-VlkaIzrM.js → en-CDg_18Bt.js} +205 -2
  24. package/dist/admin/{en-Cn78RZs-.js → en-CIZ5Ggod.js} +2 -0
  25. package/dist/admin/{en-Cg2Ak5Cr.mjs → en-CU6UzV4g.mjs} +205 -2
  26. package/dist/admin/{en-ea0tfA5i.mjs → en-CvFiqti2.mjs} +79 -1
  27. package/dist/admin/{en-D7mZWLGp.mjs → en-Dc9y-6n9.mjs} +2 -0
  28. package/dist/admin/{en-B_HltUxZ.mjs → en-GCzgS9Aw.mjs} +145 -0
  29. package/dist/admin/{es-BT9oAcHf.js → es-BCpJmUEM.js} +205 -2
  30. package/dist/admin/{es-C9-cXdb6.mjs → es-BmfffNM2.mjs} +205 -2
  31. package/dist/admin/{es-BmGtLkJD.js → es-C4IRHV5d.js} +2 -0
  32. package/dist/admin/{es-CHHEZi44.js → es-C4pEnDRv.js} +145 -0
  33. package/dist/admin/{es-C8GZFGjv.js → es-Cr8JH85N.js} +79 -1
  34. package/dist/admin/{es-joykG5I9.mjs → es-D2Kw6E4N.mjs} +2 -0
  35. package/dist/admin/{es-CrhwOq-5.mjs → es-DHBBZKl2.mjs} +145 -0
  36. package/dist/admin/{es-CEi_ycX8.mjs → es-DbKHZz5l.mjs} +79 -1
  37. package/dist/admin/{fr-CrzxGe5e.mjs → fr-BOvvfEH7.mjs} +2 -0
  38. package/dist/admin/{fr-B3SL0TBV.js → fr-C2aLSwEu.js} +145 -0
  39. package/dist/admin/{fr-BAMSvQbR.mjs → fr-DQJ_7aq5.mjs} +79 -1
  40. package/dist/admin/{fr-DpU_qiXR.mjs → fr-DZd2XJP6.mjs} +205 -2
  41. package/dist/admin/{fr-5djKfQ3s.mjs → fr-Dvq4xk2e.mjs} +145 -0
  42. package/dist/admin/{fr-oRCJNdeJ.js → fr-OJ4VW1kh.js} +79 -1
  43. package/dist/admin/{fr-D6KJqPWi.js → fr-ZcN0yM_2.js} +205 -2
  44. package/dist/admin/{fr-Ct_uPqxV.js → fr-Zkb8vfjN.js} +2 -0
  45. package/dist/admin/index-BHBRr562.js +1940 -0
  46. package/dist/admin/index-CQk1XeNb.mjs +1939 -0
  47. package/dist/admin/{EditorField-Diq1j8kc.js → index-Cm-fFUhE.js} +13704 -6766
  48. package/dist/admin/{EditorField-B8pyAxPE.mjs → index-XgfwqLfV.mjs} +10203 -3265
  49. package/dist/admin/index.js +1 -1
  50. package/dist/admin/index.mjs +2 -2
  51. package/dist/admin/{pt-chN2jrXl.js → pt-1IUyx6ae.js} +2 -0
  52. package/dist/admin/{pt-CQrfqsCN.mjs → pt-BSLzA17U.mjs} +79 -1
  53. package/dist/admin/{pt-jUqOR7Xh.mjs → pt-C3UdeIpd.mjs} +205 -2
  54. package/dist/admin/{pt-BOrl6x-r.js → pt-CImqDnLB.js} +205 -2
  55. package/dist/admin/{pt-BdnY2Cth.mjs → pt-CilDggGF.mjs} +145 -0
  56. package/dist/admin/{pt-tUeJ2Ywd.js → pt-D0I0_JYs.js} +79 -1
  57. package/dist/admin/{pt-EVwCuBUK.js → pt-DX0OAIoL.js} +145 -0
  58. package/dist/admin/{pt-Ga-4Hsv0.mjs → pt-etwsg3G4.mjs} +2 -0
  59. package/dist/admin/useBreakpoint-D30eNTkF.js +37 -0
  60. package/dist/admin/useBreakpoint-DCfn6pDG.mjs +38 -0
  61. package/dist/admin/useT-CmaeZbCr.mjs +65 -0
  62. package/dist/admin/{useT-BWZ_-Hel.js → useT-D3sM9Zf7.js} +4 -70
  63. package/dist/blocks/index.d.mts +167 -0
  64. package/dist/blocks/index.d.ts +167 -0
  65. package/dist/blocks/index.js +631 -0
  66. package/dist/blocks/index.mjs +609 -0
  67. package/dist/react/index.d.mts +39 -0
  68. package/dist/react/index.d.ts +39 -0
  69. package/dist/react/index.js +2687 -0
  70. package/dist/react/index.mjs +2657 -0
  71. package/dist/render/index.d.mts +131 -5
  72. package/dist/render/index.d.ts +131 -5
  73. package/dist/render/index.js +1679 -181
  74. package/dist/render/index.mjs +1679 -181
  75. package/dist/server/index.js +10645 -3716
  76. package/dist/server/index.mjs +10645 -3716
  77. package/dist/vue/index.d.mts +41 -0
  78. package/dist/vue/index.d.ts +41 -0
  79. package/dist/vue/index.js +2630 -0
  80. package/dist/vue/index.mjs +2610 -0
  81. package/package.json +30 -6
  82. package/schema/magic-editor-x.schema.json +540 -1
  83. package/types/blocks.d.ts +167 -0
  84. package/types/index.d.ts +127 -0
  85. package/types/react.d.ts +39 -0
  86. package/types/render.d.ts +131 -5
  87. package/types/vue.d.ts +41 -0
  88. package/dist/admin/api-Dsq53giv.js +0 -37
  89. package/dist/admin/api-RZ0Fz0BD.mjs +0 -38
  90. package/dist/admin/index-C-lyON1B.mjs +0 -303
  91. package/dist/admin/index-iA0lued_.js +0 -302
  92. package/dist/admin/sanitize-Cb4jvBhj.js +0 -628
  93. package/dist/admin/sanitize-ClkX823p.mjs +0 -627
  94. package/dist/admin/useT-pvHmgBFj.mjs +0 -131
@@ -0,0 +1,609 @@
1
+ // shared/built-in-marker.mjs
2
+ var MARKED_BLOCKS = Object.freeze({
3
+ toc: Object.freeze({ keys: Object.freeze(["title", "minLevel", "maxLevel", "style"]), anchor: null }),
4
+ gallery: Object.freeze({
5
+ keys: Object.freeze(["images", "layout", "columns", "caption"]),
6
+ anchor: Object.freeze(["images", "array"])
7
+ }),
8
+ video: Object.freeze({
9
+ keys: Object.freeze(["file", "poster", "title", "caption", "loop", "muted", "tracks"]),
10
+ anchor: Object.freeze(["file", "object"])
11
+ }),
12
+ audio: Object.freeze({
13
+ keys: Object.freeze(["file", "title", "caption", "loop", "tracks"]),
14
+ anchor: Object.freeze(["file", "object"])
15
+ }),
16
+ synced: Object.freeze({ keys: Object.freeze(["ref"]), anchor: Object.freeze(["ref", "string"]) })
17
+ });
18
+
19
+ // shared/editor-data.mjs
20
+ var EDITOR_DATA_LIMITS = Object.freeze({
21
+ maxBlocks: 5e3,
22
+ maxBytes: 5 * 1024 * 1024,
23
+ maxDepth: 24,
24
+ maxBlockIdLength: 64,
25
+ maxTypeLength: 64
26
+ });
27
+
28
+ // shared/sanitize-policy.mjs
29
+ var INLINE_TAGS = Object.freeze([
30
+ "b",
31
+ "strong",
32
+ "i",
33
+ "em",
34
+ "u",
35
+ "s",
36
+ "mark",
37
+ "code",
38
+ "a",
39
+ "br",
40
+ "span",
41
+ "sub",
42
+ "sup"
43
+ ]);
44
+ var INTERNAL_LINK_ATTRIBUTES = Object.freeze([
45
+ "data-mex-uid",
46
+ "data-mex-document-id",
47
+ "data-mex-locale",
48
+ "data-mex-mention"
49
+ ]);
50
+ var INTERNAL_LINK_RULES = Object.freeze({
51
+ "data-mex-uid": /^api::[a-z0-9-]{1,60}\.[a-z0-9-]{1,60}$/,
52
+ "data-mex-document-id": /^[a-z0-9]{1,64}$/,
53
+ "data-mex-locale": /^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8}){0,3}$/,
54
+ // Boolean flag: written as "true"; an empty value (`<a data-mex-mention>`) is accepted and normalised.
55
+ "data-mex-mention": /^(?:|true)$/
56
+ });
57
+ var INLINE_ATTRIBUTES = Object.freeze({
58
+ a: ["href", "target", "rel", "title", ...INTERNAL_LINK_ATTRIBUTES],
59
+ span: ["class", "data-tooltip"],
60
+ mark: ["class"],
61
+ u: ["class"],
62
+ s: ["class"],
63
+ code: ["class"]
64
+ });
65
+ var INLINE_CLASSES = Object.freeze({
66
+ span: ["cdx-tooltip"],
67
+ mark: ["cdx-marker"],
68
+ u: ["cdx-underline"],
69
+ s: ["cdx-strikethrough", "cdx-strikethroughs"],
70
+ code: ["inline-code"]
71
+ });
72
+ var LINK_SCHEMES = Object.freeze(["http", "https", "mailto", "tel"]);
73
+ var MAX_URL_LENGTH = 2048;
74
+ var sanitizeUrl = (value, options = {}) => {
75
+ const schemes = options.schemes || LINK_SCHEMES;
76
+ const allowRelative = options.allowRelative !== false;
77
+ if (typeof value !== "string") return "";
78
+ const url = value.trim();
79
+ if (!url || url.length > MAX_URL_LENGTH) return "";
80
+ if (/[\u0000-\u001f\u007f\s]/.test(url) && !/^(mailto|tel):/i.test(url)) return "";
81
+ if (url.startsWith("//") || url.startsWith("\\")) return "";
82
+ const scheme = /^([a-zA-Z][a-zA-Z0-9+.-]{0,20}):/.exec(url);
83
+ if (!scheme) {
84
+ return allowRelative && /^[/#?.]/.test(url) ? url : "";
85
+ }
86
+ if (!schemes.includes(scheme[1].toLowerCase())) return "";
87
+ if (scheme[1].toLowerCase().startsWith("http")) {
88
+ try {
89
+ const parsed = new URL(url);
90
+ if (parsed.username || parsed.password) return "";
91
+ } catch {
92
+ return "";
93
+ }
94
+ }
95
+ return url;
96
+ };
97
+ var TRACK_KINDS = Object.freeze(["captions", "subtitles", "descriptions", "chapters"]);
98
+
99
+ // shared/blocks/define.mjs
100
+ var BLOCK_FIELD_TYPES = Object.freeze([
101
+ "text",
102
+ "inline",
103
+ "url",
104
+ "enum",
105
+ "number",
106
+ "boolean",
107
+ "array",
108
+ "object"
109
+ ]);
110
+ var BLOCK_LIMITS = Object.freeze({
111
+ /** Fields per object (also the schema root). */
112
+ maxFields: 50,
113
+ /** Nesting of arrays and objects below the root. */
114
+ maxDepth: 4,
115
+ /** Largest `max` of a text or inline field. */
116
+ maxText: 1e5,
117
+ /** Default `max` of a text field. */
118
+ defaultText: 1e3,
119
+ /** Default `max` of an inline HTML field. */
120
+ defaultInline: 1e4,
121
+ /** Largest `max` of an array field. */
122
+ maxItems: 500,
123
+ /** Default `max` of an array field. */
124
+ defaultItems: 50,
125
+ /** Values of an enum field. */
126
+ maxEnumValues: 100,
127
+ /** Values cleaned per block; later array items are dropped. */
128
+ maxValues: 2e4,
129
+ /** Length of a URL value. */
130
+ maxUrl: 2048
131
+ });
132
+ var BLOCK_PRESETS = Object.freeze(["article", "basic", "minimal"]);
133
+ var RESERVED_BLOCK_NAMES = Object.freeze([
134
+ "paragraph",
135
+ "header",
136
+ "list",
137
+ "checklist",
138
+ "quote",
139
+ "warning",
140
+ "alert",
141
+ "toggle",
142
+ "code",
143
+ "codeFlask",
144
+ "delimiter",
145
+ "toc",
146
+ "synced",
147
+ "table",
148
+ "embed",
149
+ "raw",
150
+ "image",
151
+ "simpleImage",
152
+ "gallery",
153
+ "video",
154
+ "audio",
155
+ "linkTool",
156
+ "attaches",
157
+ "personality",
158
+ "button",
159
+ "mediaLib",
160
+ "embeddedEntry",
161
+ "bold",
162
+ "italic",
163
+ "hyperlink",
164
+ "mention",
165
+ "marker",
166
+ "underline",
167
+ "strikethrough",
168
+ "inlineCode",
169
+ "tooltip",
170
+ "aiAssistant",
171
+ "alignmentTune",
172
+ "textVariant",
173
+ "indentTune"
174
+ ]);
175
+ var RESERVED = new Set(RESERVED_BLOCK_NAMES.map((name) => name.toLowerCase()));
176
+ var BLOCK_NAME_PATTERN = /^[A-Za-z][A-Za-z0-9_-]{1,49}$/;
177
+ var KEY_PATTERN = /^[A-Za-z][A-Za-z0-9_]{0,63}$/;
178
+ var FORBIDDEN_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
179
+ var DEFINITION_KEYS = /* @__PURE__ */ new Set([
180
+ "name",
181
+ "version",
182
+ "title",
183
+ "description",
184
+ "schema",
185
+ "sanitize",
186
+ "text",
187
+ "markdown",
188
+ "render",
189
+ "presets"
190
+ ]);
191
+ var FIELD_KEYS = {
192
+ text: ["max", "multiline", "default"],
193
+ inline: ["max", "default"],
194
+ url: ["schemes", "relative"],
195
+ enum: ["values", "default"],
196
+ number: ["min", "max", "integer", "default"],
197
+ boolean: ["default"],
198
+ array: ["items", "max"],
199
+ object: ["fields"]
200
+ };
201
+ var BlockDefinitionError = class extends Error {
202
+ /**
203
+ * @param {string} message - Message naming the definition and the problem.
204
+ * @param {string} [code] - `INVALID_DEFINITION` | `RESERVED_NAME` | `NAME_TAKEN` | `REGISTRY_SEALED`.
205
+ */
206
+ constructor(message, code = "INVALID_DEFINITION") {
207
+ super(message);
208
+ this.name = "BlockDefinitionError";
209
+ this.code = code;
210
+ }
211
+ };
212
+ var isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
213
+ var fail = (where, message) => {
214
+ throw new BlockDefinitionError(`Magic Editor X block ${where}: ${message}`);
215
+ };
216
+ var boundedInt = (value, min, max, fallback, where, key) => {
217
+ if (value === void 0) return fallback;
218
+ if (!Number.isInteger(value) || value < min || value > max)
219
+ fail(where, `"${key}" must be an integer from ${min} to ${max}`);
220
+ return value;
221
+ };
222
+ var normalizeField = (input, where, depth) => {
223
+ if (!isObject(input)) fail(where, 'a field must be an object with a "type"');
224
+ const type = input.type;
225
+ if (!BLOCK_FIELD_TYPES.includes(type)) fail(where, `unknown field type ${JSON.stringify(type)}`);
226
+ for (const key of Object.keys(input))
227
+ if (key !== "type" && key !== "label" && !FIELD_KEYS[type].includes(key))
228
+ fail(where, `unknown option "${key}" for a ${type} field`);
229
+ const out = { type };
230
+ if (input.label !== void 0) {
231
+ if (typeof input.label !== "string" || input.label.length > 100)
232
+ fail(where, '"label" must be a string of at most 100 characters');
233
+ out.label = input.label;
234
+ }
235
+ switch (type) {
236
+ case "text":
237
+ case "inline": {
238
+ out.max = boundedInt(
239
+ input.max,
240
+ 1,
241
+ BLOCK_LIMITS.maxText,
242
+ type === "text" ? BLOCK_LIMITS.defaultText : BLOCK_LIMITS.defaultInline,
243
+ where,
244
+ "max"
245
+ );
246
+ if (input.default !== void 0 && typeof input.default !== "string")
247
+ fail(where, '"default" must be a string');
248
+ out.default = (input.default || "").slice(0, out.max);
249
+ if (type === "text") {
250
+ if (input.multiline !== void 0 && typeof input.multiline !== "boolean")
251
+ fail(where, '"multiline" must be a boolean');
252
+ out.multiline = input.multiline === true;
253
+ }
254
+ break;
255
+ }
256
+ case "url": {
257
+ const schemes = input.schemes === void 0 ? ["http", "https"] : input.schemes;
258
+ if (!Array.isArray(schemes) || !schemes.length || schemes.some((scheme) => !LINK_SCHEMES.includes(scheme)))
259
+ fail(where, `"schemes" must be a non-empty list of ${LINK_SCHEMES.join(", ")}`);
260
+ if (input.relative !== void 0 && typeof input.relative !== "boolean")
261
+ fail(where, '"relative" must be a boolean');
262
+ out.schemes = Object.freeze([...new Set(schemes)]);
263
+ out.relative = input.relative !== false;
264
+ break;
265
+ }
266
+ case "enum": {
267
+ const values = input.values;
268
+ if (!Array.isArray(values) || values.length < 1 || values.length > BLOCK_LIMITS.maxEnumValues || values.some((value) => typeof value !== "string" || value.length < 1 || value.length > 100) || new Set(values).size !== values.length)
269
+ fail(
270
+ where,
271
+ `"values" must be 1 to ${BLOCK_LIMITS.maxEnumValues} distinct strings (1-100 characters)`
272
+ );
273
+ out.values = Object.freeze([...values]);
274
+ if (input.default !== void 0 && !values.includes(input.default))
275
+ fail(where, '"default" must be one of "values"');
276
+ out.default = input.default === void 0 ? values[0] : input.default;
277
+ break;
278
+ }
279
+ case "number": {
280
+ for (const key of ["min", "max", "default"])
281
+ if (input[key] !== void 0 && (typeof input[key] !== "number" || !Number.isFinite(input[key])))
282
+ fail(where, `"${key}" must be a finite number`);
283
+ if (input.integer !== void 0 && typeof input.integer !== "boolean")
284
+ fail(where, '"integer" must be a boolean');
285
+ out.integer = input.integer === true;
286
+ out.min = input.min === void 0 ? -Number.MAX_SAFE_INTEGER : input.min;
287
+ out.max = input.max === void 0 ? Number.MAX_SAFE_INTEGER : input.max;
288
+ if (out.min > out.max) fail(where, '"min" must not be greater than "max"');
289
+ if (input.default !== void 0) {
290
+ if (input.default < out.min || input.default > out.max || out.integer && !Number.isInteger(input.default))
291
+ fail(where, '"default" must be a valid value of the field');
292
+ out.default = input.default;
293
+ }
294
+ break;
295
+ }
296
+ case "boolean":
297
+ if (input.default !== void 0 && typeof input.default !== "boolean")
298
+ fail(where, '"default" must be a boolean');
299
+ out.default = input.default === true;
300
+ break;
301
+ case "array":
302
+ if (depth >= BLOCK_LIMITS.maxDepth) fail(where, `fields nest at most ${BLOCK_LIMITS.maxDepth} levels`);
303
+ out.max = boundedInt(input.max, 1, BLOCK_LIMITS.maxItems, BLOCK_LIMITS.defaultItems, where, "max");
304
+ out.items = normalizeField(input.items, `${where}[]`, depth + 1);
305
+ break;
306
+ case "object":
307
+ if (depth >= BLOCK_LIMITS.maxDepth) fail(where, `fields nest at most ${BLOCK_LIMITS.maxDepth} levels`);
308
+ out.fields = normalizeFields(input.fields, where, depth + 1);
309
+ break;
310
+ default:
311
+ break;
312
+ }
313
+ return Object.freeze(out);
314
+ };
315
+ var normalizeFields = (input, where, depth) => {
316
+ if (!isObject(input)) fail(where, '"fields" must be an object of field descriptors');
317
+ const keys = Object.keys(input);
318
+ if (keys.length < 1 || keys.length > BLOCK_LIMITS.maxFields)
319
+ fail(where, `an object needs 1 to ${BLOCK_LIMITS.maxFields} fields`);
320
+ const out = /* @__PURE__ */ Object.create(null);
321
+ for (const key of keys) {
322
+ if (!KEY_PATTERN.test(key) || FORBIDDEN_KEYS.has(key))
323
+ fail(where, `field name ${JSON.stringify(key)} must match ${KEY_PATTERN}`);
324
+ out[key] = normalizeField(input[key], `${where}.${key}`, depth);
325
+ }
326
+ return Object.freeze(out);
327
+ };
328
+ var blockNameProblem = (name) => {
329
+ if (typeof name !== "string" || !BLOCK_NAME_PATTERN.test(name))
330
+ return `the name must match ${BLOCK_NAME_PATTERN}`;
331
+ if (RESERVED.has(name.toLowerCase())) return `"${name}" is a built-in tool name`;
332
+ return null;
333
+ };
334
+ var defineBlock = (definition) => {
335
+ if (!isObject(definition))
336
+ throw new BlockDefinitionError("Magic Editor X block: the definition must be an object");
337
+ const label = typeof definition.name === "string" ? `"${definition.name.slice(0, 50)}"` : "(no name)";
338
+ for (const key of Object.keys(definition))
339
+ if (!DEFINITION_KEYS.has(key)) fail(label, `unknown key "${key}"`);
340
+ const problem = blockNameProblem(definition.name);
341
+ if (problem)
342
+ throw new BlockDefinitionError(
343
+ `Magic Editor X block ${label}: ${problem}`,
344
+ problem.includes("built-in") ? "RESERVED_NAME" : "INVALID_DEFINITION"
345
+ );
346
+ const out = {
347
+ name: definition.name,
348
+ version: boundedInt(definition.version, 1, 1e6, 1, label, "version")
349
+ };
350
+ for (const [key, max] of [
351
+ ["title", 100],
352
+ ["description", 500]
353
+ ]) {
354
+ if (definition[key] === void 0) continue;
355
+ if (typeof definition[key] !== "string" || definition[key].length > max)
356
+ fail(label, `"${key}" must be a string of at most ${max} characters`);
357
+ out[key] = definition[key];
358
+ }
359
+ out.schema = normalizeFields(definition.schema, `${label} schema`, 0);
360
+ for (const key of ["sanitize", "text", "markdown", "render"]) {
361
+ if (definition[key] === void 0) continue;
362
+ if (typeof definition[key] !== "function") fail(label, `"${key}" must be a function`);
363
+ out[key] = definition[key];
364
+ }
365
+ const presets = definition.presets === void 0 ? [] : definition.presets;
366
+ if (!Array.isArray(presets) || presets.some((preset) => !BLOCK_PRESETS.includes(preset)))
367
+ fail(label, `"presets" must list some of ${BLOCK_PRESETS.join(", ")}`);
368
+ out.presets = Object.freeze([...new Set(presets)]);
369
+ return Object.freeze(out);
370
+ };
371
+ var cleanValue = (field, value, inline, budget) => {
372
+ budget.left -= 1;
373
+ switch (field.type) {
374
+ case "text":
375
+ return typeof value === "string" ? value.slice(0, field.max) : field.default;
376
+ case "inline":
377
+ return inline(typeof value === "string" ? value.slice(0, field.max) : field.default);
378
+ case "url":
379
+ return typeof value === "string" ? sanitizeUrl(value.slice(0, BLOCK_LIMITS.maxUrl), {
380
+ schemes: field.schemes,
381
+ allowRelative: field.relative
382
+ }) : "";
383
+ case "enum":
384
+ return typeof value === "string" && field.values.includes(value) ? value : field.default;
385
+ case "number":
386
+ return typeof value === "number" && Number.isFinite(value) && value >= field.min && value <= field.max && (!field.integer || Number.isInteger(value)) ? value : field.default;
387
+ case "boolean":
388
+ return typeof value === "boolean" ? value : field.default;
389
+ case "array": {
390
+ const out = [];
391
+ if (!Array.isArray(value)) return out;
392
+ for (const item of value) {
393
+ if (out.length >= field.max || budget.left <= 0) break;
394
+ if (!matchesType(field.items, item)) continue;
395
+ const clean = cleanValue(field.items, item, inline, budget);
396
+ if (clean !== void 0) out.push(clean);
397
+ }
398
+ return out;
399
+ }
400
+ case "object":
401
+ return cleanObject(field.fields, isObject(value) ? value : {}, inline, budget);
402
+ default:
403
+ return void 0;
404
+ }
405
+ };
406
+ var matchesType = (field, item) => {
407
+ switch (field.type) {
408
+ case "text":
409
+ case "inline":
410
+ case "url":
411
+ case "enum":
412
+ return typeof item === "string";
413
+ case "number":
414
+ return typeof item === "number";
415
+ case "boolean":
416
+ return typeof item === "boolean";
417
+ case "array":
418
+ return Array.isArray(item);
419
+ default:
420
+ return isObject(item);
421
+ }
422
+ };
423
+ var cleanObject = (fields, value, inline, budget) => {
424
+ const out = {};
425
+ for (const key of Object.keys(fields)) {
426
+ const own = Object.prototype.hasOwnProperty.call(value, key) ? value[key] : void 0;
427
+ const clean = cleanValue(fields[key], own, inline, budget);
428
+ if (clean !== void 0) out[key] = clean;
429
+ }
430
+ return out;
431
+ };
432
+ var eachLeaf = (fields, data, visit) => {
433
+ const walk = (field, value, path, depth) => {
434
+ if (depth > BLOCK_LIMITS.maxDepth + 1) return;
435
+ if (field.type === "array") {
436
+ if (!Array.isArray(value)) return;
437
+ value.slice(0, field.max).forEach((item, index) => walk(field.items, item, [...path, index], depth + 1));
438
+ } else if (field.type === "object") {
439
+ if (!isObject(value)) return;
440
+ for (const key of Object.keys(field.fields))
441
+ if (Object.prototype.hasOwnProperty.call(value, key))
442
+ walk(field.fields[key], value[key], [...path, key], depth + 1);
443
+ } else visit(field, value, path);
444
+ };
445
+ if (!isObject(data)) return;
446
+ for (const key of Object.keys(fields))
447
+ if (Object.prototype.hasOwnProperty.call(data, key)) walk(fields[key], data[key], [key], 0);
448
+ };
449
+ var fieldAt = (fields, path) => {
450
+ let current = { type: "object", fields };
451
+ for (const part of String(path).split(".")) {
452
+ if (!part) continue;
453
+ if (current.type === "array") {
454
+ if (/^\d+$/.test(part)) {
455
+ current = current.items;
456
+ continue;
457
+ }
458
+ return null;
459
+ }
460
+ if (current.type !== "object" || !Object.prototype.hasOwnProperty.call(current.fields, part)) return null;
461
+ current = current.fields[part];
462
+ }
463
+ return current;
464
+ };
465
+ var fieldSchema = (field) => {
466
+ const base = field.label ? { title: field.label } : {};
467
+ switch (field.type) {
468
+ case "text":
469
+ return { ...base, type: "string", maxLength: field.max };
470
+ case "inline":
471
+ return {
472
+ ...base,
473
+ type: "string",
474
+ description: "Inline HTML (shared inline policy; sanitising may lengthen the text by escaping)"
475
+ };
476
+ case "url":
477
+ return {
478
+ ...base,
479
+ type: "string",
480
+ maxLength: BLOCK_LIMITS.maxUrl,
481
+ description: `URL (${field.schemes.join(", ")}${field.relative ? ", site-relative" : ""}) or ''`
482
+ };
483
+ case "enum":
484
+ return { ...base, type: "string", enum: [...field.values], default: field.default };
485
+ case "number": {
486
+ const out = { ...base, type: field.integer ? "integer" : "number" };
487
+ if (field.min !== -Number.MAX_SAFE_INTEGER) out.minimum = field.min;
488
+ if (field.max !== Number.MAX_SAFE_INTEGER) out.maximum = field.max;
489
+ if (field.default !== void 0) out.default = field.default;
490
+ return out;
491
+ }
492
+ case "boolean":
493
+ return { ...base, type: "boolean", default: field.default };
494
+ case "array":
495
+ return { ...base, type: "array", maxItems: field.max, items: fieldSchema(field.items) };
496
+ default:
497
+ return { ...base, ...objectSchema(field.fields) };
498
+ }
499
+ };
500
+ var objectSchema = (fields) => {
501
+ const properties = {};
502
+ const required = [];
503
+ for (const [key, field] of Object.entries(fields)) {
504
+ properties[key] = fieldSchema(field);
505
+ if (!(field.type === "number" && field.default === void 0)) required.push(key);
506
+ }
507
+ return { type: "object", properties, required, additionalProperties: false };
508
+ };
509
+ var blockJsonSchema = (definition) => {
510
+ const def = compileBlock(definition);
511
+ return {
512
+ $schema: "https://json-schema.org/draft/2020-12/schema",
513
+ title: def.title || def.name,
514
+ "x-magic-editor-x-block": { name: def.name, version: def.version },
515
+ ...objectSchema(def.schema)
516
+ };
517
+ };
518
+ var compiled = /* @__PURE__ */ new WeakMap();
519
+ var compileBlock = (definition) => {
520
+ if (definition && typeof definition === "object" && compiled.has(definition))
521
+ return compiled.get(definition);
522
+ const def = defineBlock(definition && typeof definition === "object" ? { ...definition } : definition);
523
+ const schema = def.schema;
524
+ const run = (data, inline) => cleanObject(schema, isObject(data) ? data : {}, inline, { left: BLOCK_LIMITS.maxValues });
525
+ const result = Object.freeze({
526
+ ...def,
527
+ /**
528
+ * Cleans untrusted block data (unknown keys dropped, values bounded).
529
+ * The optional `sanitize` hook runs on the clean data; its result is
530
+ * cleaned again, so it cannot add keys or unsafe values. A hook that
531
+ * throws is ignored (the clean data is used).
532
+ * @param {unknown} data - Block data.
533
+ * @param {(html: string) => string} inline - Inline HTML sanitiser of the caller.
534
+ * @returns {object} Clean data.
535
+ */
536
+ clean: (data, inline) => {
537
+ const first = run(data, inline);
538
+ if (!def.sanitize) return first;
539
+ let next;
540
+ try {
541
+ next = def.sanitize(copyData(first), { sanitizeUrl });
542
+ } catch {
543
+ return first;
544
+ }
545
+ return isObject(next) ? run(next, inline) : first;
546
+ },
547
+ /**
548
+ * Text fields of the data in schema order: `{ path, kind, value }`
549
+ * (`html` for inline fields, `plain` for text fields).
550
+ * @param {unknown} data - Block data.
551
+ * @returns {Array<{ path: Array<string|number>, kind: 'html'|'plain', value: string }>} Fields.
552
+ */
553
+ fields: (data) => {
554
+ const out = [];
555
+ eachLeaf(schema, data, (field, value, path) => {
556
+ if (typeof value !== "string") return;
557
+ if (field.type === "inline") out.push({ path, kind: "html", value });
558
+ else if (field.type === "text") out.push({ path, kind: "plain", value });
559
+ });
560
+ return out;
561
+ },
562
+ /**
563
+ * Non-empty URL values: `{ path, value }`.
564
+ * @param {unknown} data - Block data.
565
+ * @returns {Array<{ path: Array<string|number>, value: string }>} URLs.
566
+ */
567
+ urls: (data) => {
568
+ const out = [];
569
+ eachLeaf(schema, data, (field, value, path) => {
570
+ if (field.type === "url" && typeof value === "string" && value) out.push({ path, value });
571
+ });
572
+ return out;
573
+ },
574
+ /**
575
+ * Kind of the value at a dotted path, for the version diff.
576
+ * @param {string} path - Dotted path.
577
+ * @returns {'inline'|'plain'|'value'} Kind.
578
+ */
579
+ kindOf: (path) => {
580
+ const field = fieldAt(schema, path);
581
+ if (!field) return "value";
582
+ if (field.type === "inline") return "inline";
583
+ if (field.type === "text") return "plain";
584
+ return "value";
585
+ }
586
+ });
587
+ compiled.set(result, result);
588
+ if (definition && typeof definition === "object") compiled.set(definition, result);
589
+ return result;
590
+ };
591
+ var copyData = (value) => {
592
+ if (Array.isArray(value)) return value.map(copyData);
593
+ if (isObject(value)) {
594
+ const out = {};
595
+ for (const [key, child] of Object.entries(value)) out[key] = copyData(child);
596
+ return out;
597
+ }
598
+ return value;
599
+ };
600
+ export {
601
+ BLOCK_FIELD_TYPES,
602
+ BLOCK_LIMITS,
603
+ BLOCK_PRESETS,
604
+ BlockDefinitionError,
605
+ RESERVED_BLOCK_NAMES,
606
+ blockJsonSchema,
607
+ blockNameProblem,
608
+ defineBlock
609
+ };
@@ -0,0 +1,39 @@
1
+ /** Types of `magic-editor-x/react` (React 18 and 19; needs `@types/react` for TypeScript). */
2
+
3
+ import type { ComponentType, ReactElement, ReactNode } from 'react';
4
+ import type { RenderBlock, RenderNode, RenderOptions } from '../render/index.js';
5
+
6
+ /** Helpers handed to a block component. */
7
+ export interface ReactBlockHelpers {
8
+ /** Plain text of stored inline HTML. */
9
+ text(html: unknown): string;
10
+ /** Validated URL or ''. */
11
+ url(value: unknown, schemes?: string[]): string;
12
+ /** React nodes of stored inline HTML (strict parser, links resolved). */
13
+ inline(html: unknown): ReactNode[];
14
+ /** React nodes of child blocks (toggle content), with your components applied. */
15
+ renderBlocks(blocks: RenderBlock[]): ReactNode[];
16
+ }
17
+
18
+ /** Props of a block component in `components`. */
19
+ export interface BlockComponentProps {
20
+ block: RenderBlock;
21
+ helpers: ReactBlockHelpers;
22
+ /** The default output of the block. */
23
+ children: ReactNode[];
24
+ }
25
+
26
+ export interface MagicEditorXContentProps {
27
+ /** Stored field value (object, JSON string, 1.x shapes). null renders nothing. */
28
+ value: unknown;
29
+ /** Render options (links, media, embedHosts, imageSizes, code, rawHtml + sanitizeRaw, overrides). */
30
+ options?: RenderOptions;
31
+ /** Per-type components; return null to hide a block. */
32
+ components?: Record<string, ComponentType<BlockComponentProps>>;
33
+ }
34
+
35
+ /** Renders stored content as React elements (a fragment, no wrapper element). */
36
+ export declare function MagicEditorXContent(props: MagicEditorXContentProps): ReactElement;
37
+ /** React nodes of a `renderTree` result. */
38
+ export declare function treeToReact(nodes: RenderNode[]): ReactNode[];
39
+ export default MagicEditorXContent;
@@ -0,0 +1,39 @@
1
+ /** Types of `magic-editor-x/react` (React 18 and 19; needs `@types/react` for TypeScript). */
2
+
3
+ import type { ComponentType, ReactElement, ReactNode } from 'react';
4
+ import type { RenderBlock, RenderNode, RenderOptions } from '../render/index.js';
5
+
6
+ /** Helpers handed to a block component. */
7
+ export interface ReactBlockHelpers {
8
+ /** Plain text of stored inline HTML. */
9
+ text(html: unknown): string;
10
+ /** Validated URL or ''. */
11
+ url(value: unknown, schemes?: string[]): string;
12
+ /** React nodes of stored inline HTML (strict parser, links resolved). */
13
+ inline(html: unknown): ReactNode[];
14
+ /** React nodes of child blocks (toggle content), with your components applied. */
15
+ renderBlocks(blocks: RenderBlock[]): ReactNode[];
16
+ }
17
+
18
+ /** Props of a block component in `components`. */
19
+ export interface BlockComponentProps {
20
+ block: RenderBlock;
21
+ helpers: ReactBlockHelpers;
22
+ /** The default output of the block. */
23
+ children: ReactNode[];
24
+ }
25
+
26
+ export interface MagicEditorXContentProps {
27
+ /** Stored field value (object, JSON string, 1.x shapes). null renders nothing. */
28
+ value: unknown;
29
+ /** Render options (links, media, embedHosts, imageSizes, code, rawHtml + sanitizeRaw, overrides). */
30
+ options?: RenderOptions;
31
+ /** Per-type components; return null to hide a block. */
32
+ components?: Record<string, ComponentType<BlockComponentProps>>;
33
+ }
34
+
35
+ /** Renders stored content as React elements (a fragment, no wrapper element). */
36
+ export declare function MagicEditorXContent(props: MagicEditorXContentProps): ReactElement;
37
+ /** React nodes of a `renderTree` result. */
38
+ export declare function treeToReact(nodes: RenderNode[]): ReactNode[];
39
+ export default MagicEditorXContent;