@teleporthq/teleport-shared 0.43.61 → 0.43.63

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 (78) hide show
  1. package/__tests__/utils/font-preconnect.ts +62 -0
  2. package/__tests__/utils/rich-text-embeds-runtime-module.ts +124 -0
  3. package/__tests__/utils/rich-text-embeds.ts +254 -0
  4. package/dist/cjs/index.d.ts +6 -1
  5. package/dist/cjs/index.d.ts.map +1 -1
  6. package/dist/cjs/index.js +11 -1
  7. package/dist/cjs/index.js.map +1 -1
  8. package/dist/cjs/page-revalidate/api-route.d.ts +24 -0
  9. package/dist/cjs/page-revalidate/api-route.d.ts.map +1 -0
  10. package/dist/cjs/page-revalidate/api-route.js +30 -0
  11. package/dist/cjs/page-revalidate/api-route.js.map +1 -0
  12. package/dist/cjs/page-revalidate/constants.d.ts +22 -0
  13. package/dist/cjs/page-revalidate/constants.d.ts.map +1 -0
  14. package/dist/cjs/page-revalidate/constants.js +25 -0
  15. package/dist/cjs/page-revalidate/constants.js.map +1 -0
  16. package/dist/cjs/page-revalidate/index.d.ts +3 -0
  17. package/dist/cjs/page-revalidate/index.d.ts.map +1 -0
  18. package/dist/cjs/page-revalidate/index.js +19 -0
  19. package/dist/cjs/page-revalidate/index.js.map +1 -0
  20. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  21. package/dist/cjs/utils/font-preconnect.d.ts +40 -0
  22. package/dist/cjs/utils/font-preconnect.d.ts.map +1 -0
  23. package/dist/cjs/utils/font-preconnect.js +52 -0
  24. package/dist/cjs/utils/font-preconnect.js.map +1 -0
  25. package/dist/cjs/utils/product-ratings.d.ts +26 -0
  26. package/dist/cjs/utils/product-ratings.d.ts.map +1 -0
  27. package/dist/cjs/utils/product-ratings.js +30 -0
  28. package/dist/cjs/utils/product-ratings.js.map +1 -0
  29. package/dist/cjs/utils/rich-text-embeds-codegen.d.ts +9 -0
  30. package/dist/cjs/utils/rich-text-embeds-codegen.d.ts.map +1 -0
  31. package/dist/cjs/utils/rich-text-embeds-codegen.js +139 -0
  32. package/dist/cjs/utils/rich-text-embeds-codegen.js.map +1 -0
  33. package/dist/cjs/utils/rich-text-embeds.d.ts +251 -0
  34. package/dist/cjs/utils/rich-text-embeds.d.ts.map +1 -0
  35. package/dist/cjs/utils/rich-text-embeds.js +852 -0
  36. package/dist/cjs/utils/rich-text-embeds.js.map +1 -0
  37. package/dist/esm/index.d.ts +6 -1
  38. package/dist/esm/index.d.ts.map +1 -1
  39. package/dist/esm/index.js +6 -1
  40. package/dist/esm/index.js.map +1 -1
  41. package/dist/esm/page-revalidate/api-route.d.ts +24 -0
  42. package/dist/esm/page-revalidate/api-route.d.ts.map +1 -0
  43. package/dist/esm/page-revalidate/api-route.js +26 -0
  44. package/dist/esm/page-revalidate/api-route.js.map +1 -0
  45. package/dist/esm/page-revalidate/constants.d.ts +22 -0
  46. package/dist/esm/page-revalidate/constants.d.ts.map +1 -0
  47. package/dist/esm/page-revalidate/constants.js +22 -0
  48. package/dist/esm/page-revalidate/constants.js.map +1 -0
  49. package/dist/esm/page-revalidate/index.d.ts +3 -0
  50. package/dist/esm/page-revalidate/index.d.ts.map +1 -0
  51. package/dist/esm/page-revalidate/index.js +3 -0
  52. package/dist/esm/page-revalidate/index.js.map +1 -0
  53. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  54. package/dist/esm/utils/font-preconnect.d.ts +40 -0
  55. package/dist/esm/utils/font-preconnect.d.ts.map +1 -0
  56. package/dist/esm/utils/font-preconnect.js +48 -0
  57. package/dist/esm/utils/font-preconnect.js.map +1 -0
  58. package/dist/esm/utils/product-ratings.d.ts +26 -0
  59. package/dist/esm/utils/product-ratings.d.ts.map +1 -0
  60. package/dist/esm/utils/product-ratings.js +26 -0
  61. package/dist/esm/utils/product-ratings.js.map +1 -0
  62. package/dist/esm/utils/rich-text-embeds-codegen.d.ts +9 -0
  63. package/dist/esm/utils/rich-text-embeds-codegen.d.ts.map +1 -0
  64. package/dist/esm/utils/rich-text-embeds-codegen.js +134 -0
  65. package/dist/esm/utils/rich-text-embeds-codegen.js.map +1 -0
  66. package/dist/esm/utils/rich-text-embeds.d.ts +251 -0
  67. package/dist/esm/utils/rich-text-embeds.d.ts.map +1 -0
  68. package/dist/esm/utils/rich-text-embeds.js +822 -0
  69. package/dist/esm/utils/rich-text-embeds.js.map +1 -0
  70. package/package.json +3 -3
  71. package/src/index.ts +10 -0
  72. package/src/page-revalidate/api-route.ts +77 -0
  73. package/src/page-revalidate/constants.ts +24 -0
  74. package/src/page-revalidate/index.ts +2 -0
  75. package/src/utils/font-preconnect.ts +61 -0
  76. package/src/utils/product-ratings.ts +98 -0
  77. package/src/utils/rich-text-embeds-codegen.ts +216 -0
  78. package/src/utils/rich-text-embeds.ts +966 -0
@@ -0,0 +1,822 @@
1
+ /**
2
+ * The "code embed" contract for rich-text CONTENT — the blog post body, and any
3
+ * other column authored through a rich-text editor.
4
+ *
5
+ * A code embed is a block a post author drops into the body to pull in content
6
+ * from another site (a video, a social post, a design frame, a form, or a raw
7
+ * snippet they pasted). It is stored INSIDE the content HTML, so the same shape
8
+ * has to be produced by the studio's Quill editor, produced by the generated
9
+ * admin panel's Quill editor, previewed by the studio canvas and rendered by
10
+ * the exported Next.js app. This module is that single shape.
11
+ *
12
+ * Two rendering modes, decided by one rule (`resolveEmbedMode`):
13
+ *
14
+ * - `inline` — the markup is provably passive (no `<script>`, no `on*`
15
+ * handler, no `javascript:` URL). It is stored as the element's
16
+ * own children, so it renders server-side, needs no JavaScript
17
+ * and is crawlable. Most provider embeds are a bare `<iframe>`
18
+ * and land here.
19
+ * - `sandbox` — anything else. The markup is base64-encoded into
20
+ * `data-tq-embed-code` and the runtime materializes it inside a
21
+ * sandboxed `srcdoc` iframe, so author-supplied JavaScript can
22
+ * never touch the hosting page. The children hold only a
23
+ * `<noscript>` link fallback.
24
+ *
25
+ * The provider registry is deliberately DECLARATIVE — patterns plus a
26
+ * replacement template, never functions — so `rich-text-embeds-codegen` can
27
+ * serialize it verbatim into the module the generated project gets, and the
28
+ * editor and the published site can never drift.
29
+ */
30
+ export var EMBED_CLASS_NAME = 'tq-embed';
31
+ export var EMBED_FRAME_CLASS_NAME = 'tq-embed-frame';
32
+ export var EMBED_CAPTION_CLASS_NAME = 'tq-embed-caption';
33
+ /** The Quill blot name, shared by the studio editor and the generated one. */
34
+ export var EMBED_BLOT_NAME = 'tq-embed';
35
+ export var EMBED_ATTR = {
36
+ MARKER: 'data-tq-embed',
37
+ PROVIDER: 'data-tq-embed-provider',
38
+ URL: 'data-tq-embed-url',
39
+ MODE: 'data-tq-embed-mode',
40
+ CODE: 'data-tq-embed-code',
41
+ RATIO: 'data-tq-embed-ratio',
42
+ WIDTH: 'data-tq-embed-width',
43
+ ALIGN: 'data-tq-embed-align',
44
+ CAPTION: 'data-tq-embed-caption',
45
+ SANDBOX: 'data-tq-embed-sandbox',
46
+ /** Written by a runtime once a sandbox embed has been mounted. */
47
+ ACTIVATED: 'data-tq-embed-activated',
48
+ /**
49
+ * Marks an element an EDITOR added purely to preview the embed. Editors read
50
+ * their value back out of the live DOM, so a preview must be recognizable in
51
+ * order to be stripped again — see `normalizeEmbedsForStorage`.
52
+ */
53
+ PREVIEW: 'data-tq-embed-preview',
54
+ };
55
+ export var EMBED_ALIGNMENTS = ['center', 'float-left', 'float-right'];
56
+ export var CUSTOM_EMBED_PROVIDER_ID = 'custom';
57
+ export var GENERIC_EMBED_PROVIDER_ID = 'iframe-url';
58
+ export var DEFAULT_EMBED_WIDTH = 100;
59
+ export var DEFAULT_EMBED_ALIGN = 'center';
60
+ export var DEFAULT_EMBED_SANDBOX_LEVEL = 'strict';
61
+ /** 16:9, as a padding-top percentage. */
62
+ export var WIDESCREEN_EMBED_RATIO = 56.25;
63
+ var FILL_FRAME_STYLE = 'position:absolute;top:0;left:0;width:100%;height:100%;border:0;border-radius:inherit';
64
+ var BLOCK_FRAME_STYLE = 'display:block;width:100%;border:0;border-radius:inherit';
65
+ var iframeTemplate = function (src, options) {
66
+ var _a;
67
+ var style = options.fill
68
+ ? FILL_FRAME_STYLE
69
+ : "".concat(BLOCK_FRAME_STYLE, ";height:").concat((_a = options.height) !== null && _a !== void 0 ? _a : 400, "px");
70
+ return ("<iframe src=\"".concat(src, "\" title=\"").concat(options.title, "\" loading=\"lazy\" frameborder=\"0\" ") +
71
+ "referrerpolicy=\"strict-origin-when-cross-origin\" " +
72
+ "allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; web-share\" " +
73
+ "allowfullscreen style=\"".concat(style, "\"></iframe>"));
74
+ };
75
+ /**
76
+ * Ordered most-specific first: `resolveEmbedProvider` returns the first match,
77
+ * and the generic `iframe-url` fallback is deliberately last.
78
+ */
79
+ export var EMBED_PROVIDERS = [
80
+ {
81
+ id: 'youtube',
82
+ label: 'YouTube',
83
+ patterns: [
84
+ /^https?:\/\/(?:www\.|m\.)?youtube\.com\/watch\?(?:[^#]*&)?v=([A-Za-z0-9_-]{6,})/i,
85
+ /^https?:\/\/youtu\.be\/([A-Za-z0-9_-]{6,})/i,
86
+ /^https?:\/\/(?:www\.)?youtube\.com\/shorts\/([A-Za-z0-9_-]{6,})/i,
87
+ /^https?:\/\/(?:www\.)?youtube(?:-nocookie)?\.com\/embed\/([A-Za-z0-9_-]{6,})/i,
88
+ ],
89
+ template: iframeTemplate('https://www.youtube.com/embed/$1', {
90
+ title: 'YouTube video',
91
+ fill: true,
92
+ }),
93
+ ratio: WIDESCREEN_EMBED_RATIO,
94
+ },
95
+ {
96
+ id: 'vimeo',
97
+ label: 'Vimeo',
98
+ patterns: [
99
+ /^https?:\/\/(?:www\.)?vimeo\.com\/(\d+)/i,
100
+ /^https?:\/\/player\.vimeo\.com\/video\/(\d+)/i,
101
+ ],
102
+ template: iframeTemplate('https://player.vimeo.com/video/$1', {
103
+ title: 'Vimeo video',
104
+ fill: true,
105
+ }),
106
+ ratio: WIDESCREEN_EMBED_RATIO,
107
+ },
108
+ {
109
+ id: 'loom',
110
+ label: 'Loom',
111
+ patterns: [/^https?:\/\/(?:www\.)?loom\.com\/(?:share|embed)\/([A-Za-z0-9]+)/i],
112
+ template: iframeTemplate('https://www.loom.com/embed/$1', {
113
+ title: 'Loom video',
114
+ fill: true,
115
+ }),
116
+ ratio: WIDESCREEN_EMBED_RATIO,
117
+ },
118
+ {
119
+ id: 'dailymotion',
120
+ label: 'Dailymotion',
121
+ patterns: [
122
+ /^https?:\/\/(?:www\.)?dailymotion\.com\/video\/([A-Za-z0-9]+)/i,
123
+ /^https?:\/\/dai\.ly\/([A-Za-z0-9]+)/i,
124
+ ],
125
+ template: iframeTemplate('https://www.dailymotion.com/embed/video/$1', {
126
+ title: 'Dailymotion video',
127
+ fill: true,
128
+ }),
129
+ ratio: WIDESCREEN_EMBED_RATIO,
130
+ },
131
+ {
132
+ id: 'spotify',
133
+ label: 'Spotify',
134
+ patterns: [
135
+ /^https?:\/\/open\.spotify\.com\/(?:embed\/)?(track|album|playlist|episode|show|artist)\/([A-Za-z0-9]+)/i,
136
+ ],
137
+ template: iframeTemplate('https://open.spotify.com/embed/$1/$2', {
138
+ title: 'Spotify player',
139
+ fill: false,
140
+ height: 352,
141
+ }),
142
+ ratio: null,
143
+ },
144
+ {
145
+ id: 'soundcloud',
146
+ label: 'SoundCloud',
147
+ patterns: [/^https?:\/\/soundcloud\.com\/[^?#]+/i],
148
+ template: iframeTemplate('https://w.soundcloud.com/player/?url=$E&&color=%23ff5500&auto_play=false&show_teaser=false', { title: 'SoundCloud player', fill: false, height: 166 }),
149
+ ratio: null,
150
+ },
151
+ {
152
+ id: 'apple-podcasts',
153
+ label: 'Apple Podcasts',
154
+ patterns: [/^https?:\/\/podcasts\.apple\.com\/(.+)$/i],
155
+ template: iframeTemplate('https://embed.podcasts.apple.com/$1', {
156
+ title: 'Apple Podcasts player',
157
+ fill: false,
158
+ height: 175,
159
+ }),
160
+ ratio: null,
161
+ },
162
+ {
163
+ id: 'twitter',
164
+ label: 'X (Twitter)',
165
+ patterns: [/^https?:\/\/(?:www\.)?(?:twitter|x)\.com\/[^/]+\/status\/(\d+)/i],
166
+ template: '<blockquote class="twitter-tweet"><a href="$&"></a></blockquote>' +
167
+ '<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>',
168
+ ratio: null,
169
+ },
170
+ {
171
+ id: 'instagram',
172
+ label: 'Instagram',
173
+ patterns: [/^https?:\/\/(?:www\.)?instagram\.com\/(?:p|reel|tv)\/([A-Za-z0-9_-]+)/i],
174
+ template: '<blockquote class="instagram-media" data-instgrm-permalink="https://www.instagram.com/p/$1/" data-instgrm-version="14"></blockquote>' +
175
+ '<script async src="https://www.instagram.com/embed.js"></script>',
176
+ ratio: null,
177
+ },
178
+ {
179
+ id: 'tiktok',
180
+ label: 'TikTok',
181
+ patterns: [/^https?:\/\/(?:www\.)?tiktok\.com\/@[^/]+\/video\/(\d+)/i],
182
+ template: '<blockquote class="tiktok-embed" cite="$&" data-video-id="$1"><section></section></blockquote>' +
183
+ '<script async src="https://www.tiktok.com/embed.js"></script>',
184
+ ratio: null,
185
+ },
186
+ {
187
+ id: 'reddit',
188
+ label: 'Reddit',
189
+ patterns: [/^https?:\/\/(?:www\.)?reddit\.com\/r\/[^/]+\/comments\/.+/i],
190
+ template: '<blockquote class="reddit-embed-bq"><a href="$&"></a></blockquote>' +
191
+ '<script async src="https://embed.reddit.com/widgets.js" charset="UTF-8"></script>',
192
+ ratio: null,
193
+ },
194
+ {
195
+ id: 'pinterest',
196
+ label: 'Pinterest',
197
+ patterns: [/^https?:\/\/(?:[a-z]{2}\.)?pinterest\.[a-z.]+\/pin\/[0-9]+/i],
198
+ template: '<a data-pin-do="embedPin" href="$&"></a>' +
199
+ '<script async defer src="https://assets.pinterest.com/js/pinit.js"></script>',
200
+ ratio: null,
201
+ },
202
+ {
203
+ id: 'facebook',
204
+ label: 'Facebook',
205
+ patterns: [/^https?:\/\/(?:www\.|web\.)?facebook\.com\/.+\/(?:posts|videos)\/.+/i],
206
+ template: iframeTemplate('https://www.facebook.com/plugins/post.php?href=$E&&show_text=true', {
207
+ title: 'Facebook post',
208
+ fill: false,
209
+ height: 500,
210
+ }),
211
+ ratio: null,
212
+ },
213
+ {
214
+ id: 'github-gist',
215
+ label: 'GitHub Gist',
216
+ patterns: [/^https?:\/\/gist\.github\.com\/([^/]+\/[a-f0-9]+)/i],
217
+ template: '<script src="https://gist.github.com/$1.js"></script>',
218
+ ratio: null,
219
+ },
220
+ {
221
+ id: 'codepen',
222
+ label: 'CodePen',
223
+ patterns: [/^https?:\/\/codepen\.io\/([^/]+)\/(?:pen|embed|details|full)\/([A-Za-z0-9]+)/i],
224
+ template: iframeTemplate('https://codepen.io/$1/embed/$2?default-tab=result', {
225
+ title: 'CodePen',
226
+ fill: false,
227
+ height: 400,
228
+ }),
229
+ ratio: null,
230
+ },
231
+ {
232
+ id: 'codesandbox',
233
+ label: 'CodeSandbox',
234
+ patterns: [
235
+ /^https?:\/\/codesandbox\.io\/(?:s|embed)\/([A-Za-z0-9_-]+)/i,
236
+ /^https?:\/\/codesandbox\.io\/p\/(?:sandbox|devbox)\/([A-Za-z0-9_-]+)/i,
237
+ ],
238
+ template: iframeTemplate('https://codesandbox.io/embed/$1', {
239
+ title: 'CodeSandbox',
240
+ fill: false,
241
+ height: 500,
242
+ }),
243
+ ratio: null,
244
+ },
245
+ {
246
+ id: 'jsfiddle',
247
+ label: 'JSFiddle',
248
+ patterns: [/^https?:\/\/(?:www\.)?jsfiddle\.net\/((?:[^/]+\/)?[A-Za-z0-9]+)\/?/i],
249
+ template: iframeTemplate('https://jsfiddle.net/$1/embedded/', {
250
+ title: 'JSFiddle',
251
+ fill: false,
252
+ height: 400,
253
+ }),
254
+ ratio: null,
255
+ },
256
+ {
257
+ id: 'figma',
258
+ label: 'Figma',
259
+ patterns: [/^https?:\/\/(?:www\.)?figma\.com\/(?:file|design|proto|board|slides)\/.+/i],
260
+ template: iframeTemplate('https://www.figma.com/embed?embed_host=share&url=$E&', {
261
+ title: 'Figma',
262
+ fill: true,
263
+ }),
264
+ ratio: WIDESCREEN_EMBED_RATIO,
265
+ },
266
+ {
267
+ id: 'canva',
268
+ label: 'Canva',
269
+ patterns: [
270
+ /^https?:\/\/(?:www\.)?canva\.com\/design\/([A-Za-z0-9_-]+)\/([A-Za-z0-9_-]+)\/(?:view|watch)/i,
271
+ ],
272
+ template: iframeTemplate('https://www.canva.com/design/$1/$2/view?embed', {
273
+ title: 'Canva design',
274
+ fill: true,
275
+ }),
276
+ ratio: WIDESCREEN_EMBED_RATIO,
277
+ },
278
+ {
279
+ id: 'miro',
280
+ label: 'Miro',
281
+ patterns: [/^https?:\/\/miro\.com\/app\/(?:board|live-embed)\/([A-Za-z0-9_=-]+)/i],
282
+ template: iframeTemplate('https://miro.com/app/live-embed/$1/', {
283
+ title: 'Miro board',
284
+ fill: true,
285
+ }),
286
+ ratio: WIDESCREEN_EMBED_RATIO,
287
+ },
288
+ {
289
+ id: 'google-maps',
290
+ label: 'Google Maps',
291
+ patterns: [/^https?:\/\/(?:www\.)?google\.[a-z.]+\/maps\/embed\?(.+)$/i],
292
+ template: iframeTemplate('https://www.google.com/maps/embed?$1', {
293
+ title: 'Google Maps',
294
+ fill: true,
295
+ }),
296
+ ratio: WIDESCREEN_EMBED_RATIO,
297
+ },
298
+ {
299
+ id: 'google-maps-place',
300
+ label: 'Google Maps',
301
+ patterns: [/^https?:\/\/(?:(?:www\.)?google\.[a-z.]+\/maps|maps\.app\.goo\.gl)\/.+/i],
302
+ template: iframeTemplate('https://maps.google.com/maps?q=$E&&output=embed', {
303
+ title: 'Google Maps',
304
+ fill: true,
305
+ }),
306
+ ratio: WIDESCREEN_EMBED_RATIO,
307
+ },
308
+ {
309
+ id: 'google-forms',
310
+ label: 'Google Forms',
311
+ patterns: [/^https?:\/\/docs\.google\.com\/forms\/d\/(?:e\/)?([A-Za-z0-9_-]+)/i],
312
+ template: iframeTemplate('https://docs.google.com/forms/d/e/$1/viewform?embedded=true', {
313
+ title: 'Google Form',
314
+ fill: false,
315
+ height: 700,
316
+ }),
317
+ ratio: null,
318
+ },
319
+ {
320
+ id: 'google-docs',
321
+ label: 'Google Docs',
322
+ patterns: [
323
+ /^https?:\/\/docs\.google\.com\/(document|spreadsheets|presentation)\/d\/([A-Za-z0-9_-]+)/i,
324
+ ],
325
+ template: iframeTemplate('https://docs.google.com/$1/d/$2/preview', {
326
+ title: 'Google Docs',
327
+ fill: false,
328
+ height: 600,
329
+ }),
330
+ ratio: null,
331
+ },
332
+ {
333
+ id: 'airtable',
334
+ label: 'Airtable',
335
+ patterns: [/^https?:\/\/airtable\.com\/(?:embed\/)?(app[A-Za-z0-9/?=&_-]+)/i],
336
+ template: iframeTemplate('https://airtable.com/embed/$1', {
337
+ title: 'Airtable',
338
+ fill: false,
339
+ height: 533,
340
+ }),
341
+ ratio: null,
342
+ },
343
+ {
344
+ id: 'typeform',
345
+ label: 'Typeform',
346
+ patterns: [/^https?:\/\/(?:[A-Za-z0-9-]+\.)?typeform\.com\/to\/([A-Za-z0-9]+)/i],
347
+ template: iframeTemplate('https://form.typeform.com/to/$1', {
348
+ title: 'Typeform',
349
+ fill: false,
350
+ height: 500,
351
+ }),
352
+ ratio: null,
353
+ },
354
+ {
355
+ id: 'calendly',
356
+ label: 'Calendly',
357
+ patterns: [/^https?:\/\/calendly\.com\/(.+)$/i],
358
+ template: iframeTemplate('https://calendly.com/$1', {
359
+ title: 'Calendly',
360
+ fill: false,
361
+ height: 700,
362
+ }),
363
+ ratio: null,
364
+ },
365
+ {
366
+ id: GENERIC_EMBED_PROVIDER_ID,
367
+ label: 'Web page',
368
+ patterns: [/^https:\/\/[^\s"'<>]+$/i],
369
+ template: iframeTemplate('$&', { title: 'Embedded page', fill: true }),
370
+ ratio: WIDESCREEN_EMBED_RATIO,
371
+ },
372
+ ];
373
+ /** `javascript:`, `data:` and protocol-relative URLs never become an embed. */
374
+ export function isSafeEmbedUrl(url) {
375
+ return /^https?:\/\/[^\s"'<>]+$/i.test((url || '').trim());
376
+ }
377
+ /** How an embed names itself in the editor: the provider, or "Custom code". */
378
+ export function embedProviderLabel(providerId) {
379
+ var provider = EMBED_PROVIDERS.filter(function (entry) { return entry.id === providerId; })[0];
380
+ return provider ? provider.label : 'Custom code';
381
+ }
382
+ export function resolveEmbedProvider(url) {
383
+ var trimmed = (url || '').trim();
384
+ if (!isSafeEmbedUrl(trimmed)) {
385
+ return null;
386
+ }
387
+ for (var _i = 0, EMBED_PROVIDERS_1 = EMBED_PROVIDERS; _i < EMBED_PROVIDERS_1.length; _i++) {
388
+ var provider = EMBED_PROVIDERS_1[_i];
389
+ for (var _a = 0, _b = provider.patterns; _a < _b.length; _a++) {
390
+ var pattern = _b[_a];
391
+ var match = trimmed.match(pattern);
392
+ if (match) {
393
+ return { provider: provider, match: match };
394
+ }
395
+ }
396
+ }
397
+ return null;
398
+ }
399
+ /**
400
+ * Substitutes `$1`-`$9` / `$E1`-`$E9` / `$&` / `$E&` in a provider template.
401
+ * Written by hand rather than through `String.replace` so the replacement
402
+ * patterns `String.replace` reserves (`$'`, "$`", `$<name>`) stay literal.
403
+ */
404
+ export function applyEmbedTemplate(template, match, url) {
405
+ return template.replace(/\$(\$|E?(?:[1-9]|&))/g, function (_token, spec) {
406
+ var _a;
407
+ if (spec === '$') {
408
+ return '$';
409
+ }
410
+ var encoded = spec.charAt(0) === 'E';
411
+ var key = encoded ? spec.slice(1) : spec;
412
+ var raw = key === '&' ? url : (_a = match[Number(key)]) !== null && _a !== void 0 ? _a : '';
413
+ return encoded ? encodeURIComponent(raw) : raw;
414
+ });
415
+ }
416
+ /** The embed markup for a pasted URL, or `null` when nothing recognizes it. */
417
+ export function buildEmbedHtmlFromUrl(url) {
418
+ var resolved = resolveEmbedProvider(url);
419
+ if (!resolved) {
420
+ return null;
421
+ }
422
+ return {
423
+ provider: resolved.provider,
424
+ html: applyEmbedTemplate(resolved.provider.template, resolved.match, url.trim()),
425
+ };
426
+ }
427
+ /**
428
+ * Markup that can execute anything — a `<script>`, an inline `on*` handler or a
429
+ * `javascript:` URL — is quarantined in a sandboxed iframe. Everything else is
430
+ * passive and can be rendered straight into the page.
431
+ */
432
+ export function resolveEmbedMode(html) {
433
+ var markup = html || '';
434
+ var executes = /<script[\s/>]/i.test(markup) ||
435
+ /\son[a-z]+\s*=/i.test(markup) ||
436
+ /(?:href|src|action|formaction)\s*=\s*["']?\s*javascript:/i.test(markup);
437
+ return executes ? 'sandbox' : 'inline';
438
+ }
439
+ export function clampEmbedWidth(width) {
440
+ if (!isFinite(width)) {
441
+ return DEFAULT_EMBED_WIDTH;
442
+ }
443
+ return Math.max(10, Math.min(100, Math.round(width)));
444
+ }
445
+ export function normalizeEmbedAlign(align) {
446
+ return EMBED_ALIGNMENTS.indexOf(align) !== -1
447
+ ? align
448
+ : DEFAULT_EMBED_ALIGN;
449
+ }
450
+ export function escapeEmbedAttribute(value) {
451
+ return String(value !== null && value !== void 0 ? value : '')
452
+ .replace(/&/g, '&amp;')
453
+ .replace(/"/g, '&quot;')
454
+ .replace(/</g, '&lt;')
455
+ .replace(/>/g, '&gt;');
456
+ }
457
+ export function escapeEmbedText(value) {
458
+ return String(value !== null && value !== void 0 ? value : '')
459
+ .replace(/&/g, '&amp;')
460
+ .replace(/</g, '&lt;')
461
+ .replace(/>/g, '&gt;');
462
+ }
463
+ /**
464
+ * UTF-8 safe base64, working in both the browser (`btoa`) and Node (`Buffer`).
465
+ * The intermediate `%xx` round-trip is what makes non-ASCII captions and
466
+ * markup survive — `btoa` alone throws on anything above U+00FF.
467
+ */
468
+ export function encodeEmbedCode(code) {
469
+ var binary = encodeURIComponent(code).replace(/%([0-9A-F]{2})/gi, function (_m, hex) {
470
+ return String.fromCharCode(parseInt(hex, 16));
471
+ });
472
+ if (typeof btoa === 'function') {
473
+ return btoa(binary);
474
+ }
475
+ if (typeof Buffer !== 'undefined') {
476
+ return Buffer.from(binary, 'binary').toString('base64');
477
+ }
478
+ throw new Error('No base64 encoder available');
479
+ }
480
+ export function decodeEmbedCode(encoded) {
481
+ var binary;
482
+ if (typeof atob === 'function') {
483
+ binary = atob(encoded);
484
+ }
485
+ else if (typeof Buffer !== 'undefined') {
486
+ binary = Buffer.from(encoded, 'base64').toString('binary');
487
+ }
488
+ else {
489
+ throw new Error('No base64 decoder available');
490
+ }
491
+ var percent = '';
492
+ for (var index = 0; index < binary.length; index += 1) {
493
+ percent += "%".concat("00".concat(binary.charCodeAt(index).toString(16)).slice(-2));
494
+ }
495
+ return decodeURIComponent(percent);
496
+ }
497
+ export function createEmbedValue(params) {
498
+ var _a, _b, _c;
499
+ var code = params.code || '';
500
+ return {
501
+ provider: params.provider || CUSTOM_EMBED_PROVIDER_ID,
502
+ url: params.url || '',
503
+ code: code,
504
+ mode: resolveEmbedMode(code),
505
+ ratio: (_a = params.ratio) !== null && _a !== void 0 ? _a : null,
506
+ width: clampEmbedWidth((_b = params.width) !== null && _b !== void 0 ? _b : DEFAULT_EMBED_WIDTH),
507
+ align: normalizeEmbedAlign((_c = params.align) !== null && _c !== void 0 ? _c : DEFAULT_EMBED_ALIGN),
508
+ caption: params.caption || '',
509
+ sandboxLevel: params.sandboxLevel === 'trusted' ? 'trusted' : DEFAULT_EMBED_SANDBOX_LEVEL,
510
+ };
511
+ }
512
+ /**
513
+ * The outer block's own layout — width, float and the margins each align needs.
514
+ *
515
+ * `position:relative` is deliberate: an editor overlays a click catcher on the
516
+ * block (an embed's own iframe swallows the click that should select it), and
517
+ * that overlay needs the block as its positioning context.
518
+ */
519
+ export function embedBlockStyle(value) {
520
+ var declarations = [
521
+ 'position:relative',
522
+ "width:".concat(clampEmbedWidth(value.width), "%"),
523
+ 'max-width:100%',
524
+ 'margin:16px 0',
525
+ ];
526
+ if (value.align === 'center') {
527
+ declarations.push('margin-left:auto', 'margin-right:auto');
528
+ }
529
+ else if (value.align === 'float-left') {
530
+ declarations.push('float:left', 'margin-right:16px', 'margin-bottom:8px');
531
+ }
532
+ else if (value.align === 'float-right') {
533
+ declarations.push('float:right', 'margin-left:16px', 'margin-bottom:8px');
534
+ }
535
+ return declarations.join(';');
536
+ }
537
+ /**
538
+ * The frame is what holds the embed itself. With a `ratio` it becomes the
539
+ * responsive padding-top box the provider templates fill absolutely; without
540
+ * one the template carries its own height.
541
+ */
542
+ export function embedFrameStyle(ratio) {
543
+ if (ratio === null || !isFinite(ratio) || ratio <= 0) {
544
+ return 'position:relative;width:100%';
545
+ }
546
+ return "position:relative;width:100%;padding-top:".concat(ratio, "%");
547
+ }
548
+ export var EMBED_CAPTION_STYLE = 'margin-top:8px;font-size:0.875em;line-height:1.5;opacity:0.75;text-align:center';
549
+ /**
550
+ * The stored markup for one embed. This is the exact string both editors write
551
+ * into the content column and both runtimes read back.
552
+ */
553
+ export function buildEmbedElementHtml(value) {
554
+ var attrs = [
555
+ "class=\"".concat(EMBED_CLASS_NAME, "\""),
556
+ "".concat(EMBED_ATTR.MARKER, "=\"1\""),
557
+ "".concat(EMBED_ATTR.PROVIDER, "=\"").concat(escapeEmbedAttribute(value.provider), "\""),
558
+ "".concat(EMBED_ATTR.MODE, "=\"").concat(value.mode, "\""),
559
+ "".concat(EMBED_ATTR.WIDTH, "=\"").concat(clampEmbedWidth(value.width), "\""),
560
+ "".concat(EMBED_ATTR.ALIGN, "=\"").concat(value.align, "\""),
561
+ "".concat(EMBED_ATTR.SANDBOX, "=\"").concat(value.sandboxLevel, "\""),
562
+ ];
563
+ if (value.url) {
564
+ attrs.push("".concat(EMBED_ATTR.URL, "=\"").concat(escapeEmbedAttribute(value.url), "\""));
565
+ }
566
+ if (value.ratio !== null) {
567
+ attrs.push("".concat(EMBED_ATTR.RATIO, "=\"").concat(value.ratio, "\""));
568
+ }
569
+ if (value.caption) {
570
+ attrs.push("".concat(EMBED_ATTR.CAPTION, "=\"").concat(escapeEmbedAttribute(value.caption), "\""));
571
+ }
572
+ if (value.mode === 'sandbox') {
573
+ attrs.push("".concat(EMBED_ATTR.CODE, "=\"").concat(escapeEmbedAttribute(encodeEmbedCode(value.code)), "\""));
574
+ }
575
+ attrs.push('contenteditable="false"');
576
+ attrs.push("style=\"".concat(embedBlockStyle(value), "\""));
577
+ var caption = value.caption
578
+ ? "<figcaption class=\"".concat(EMBED_CAPTION_CLASS_NAME, "\" style=\"").concat(EMBED_CAPTION_STYLE, "\">").concat(escapeEmbedText(value.caption), "</figcaption>")
579
+ : '';
580
+ return ("<figure ".concat(attrs.join(' '), ">") +
581
+ "<div class=\"".concat(EMBED_FRAME_CLASS_NAME, "\" style=\"").concat(embedFrameStyle(value.ratio), "\">") +
582
+ "".concat(buildEmbedFrameChildren(value), "</div>").concat(caption, "</figure>"));
583
+ }
584
+ /**
585
+ * `inline` keeps the markup as real children so it renders with no JavaScript.
586
+ * `sandbox` keeps only a `<noscript>` link — the markup itself lives, encoded,
587
+ * on `data-tq-embed-code` and the runtime mounts it in a sandboxed iframe.
588
+ */
589
+ export function buildEmbedFrameChildren(value) {
590
+ if (value.mode === 'inline') {
591
+ return value.code;
592
+ }
593
+ if (!value.url) {
594
+ return '';
595
+ }
596
+ return ("<noscript><a href=\"".concat(escapeEmbedAttribute(value.url), "\" rel=\"noopener noreferrer\" ") +
597
+ "target=\"_blank\">".concat(escapeEmbedText(value.url), "</a></noscript>"));
598
+ }
599
+ /* ── Reading a stored embed back out of the DOM ─────────────────────────────
600
+ *
601
+ * Used by the studio blot (`static value(node)`), by the selection overlay and
602
+ * by the runtime activator. The frame element is the single place the markup
603
+ * lives, so both directions agree on where to look.
604
+ */
605
+ export function findEmbedFrame(element) {
606
+ return element.querySelector(".".concat(EMBED_FRAME_CLASS_NAME));
607
+ }
608
+ export function readEmbedValueFromElement(element) {
609
+ var mode = element.getAttribute(EMBED_ATTR.MODE) === 'sandbox' ? 'sandbox' : 'inline';
610
+ var frame = findEmbedFrame(element);
611
+ var rawRatio = element.getAttribute(EMBED_ATTR.RATIO);
612
+ var parsedRatio = rawRatio === null ? NaN : parseFloat(rawRatio);
613
+ var code = '';
614
+ if (mode === 'sandbox') {
615
+ code = readEncodedEmbedCode(element);
616
+ }
617
+ else if (frame) {
618
+ code = frame.innerHTML;
619
+ }
620
+ return {
621
+ provider: element.getAttribute(EMBED_ATTR.PROVIDER) || CUSTOM_EMBED_PROVIDER_ID,
622
+ url: element.getAttribute(EMBED_ATTR.URL) || '',
623
+ code: code,
624
+ mode: mode,
625
+ ratio: isFinite(parsedRatio) ? parsedRatio : null,
626
+ width: clampEmbedWidth(parseFloat(element.getAttribute(EMBED_ATTR.WIDTH) || '')),
627
+ align: normalizeEmbedAlign(element.getAttribute(EMBED_ATTR.ALIGN) || ''),
628
+ caption: element.getAttribute(EMBED_ATTR.CAPTION) || '',
629
+ sandboxLevel: element.getAttribute(EMBED_ATTR.SANDBOX) === 'trusted'
630
+ ? 'trusted'
631
+ : DEFAULT_EMBED_SANDBOX_LEVEL,
632
+ };
633
+ }
634
+ /**
635
+ * A corrupted `data-tq-embed-code` (truncated by an export round-trip, mangled
636
+ * by a content rewrite) must degrade to "render nothing", never to a thrown
637
+ * exception inside a render pass.
638
+ */
639
+ export function readEncodedEmbedCode(element) {
640
+ var encoded = element.getAttribute(EMBED_ATTR.CODE);
641
+ if (!encoded) {
642
+ return '';
643
+ }
644
+ try {
645
+ return decodeEmbedCode(encoded);
646
+ }
647
+ catch (_a) {
648
+ return '';
649
+ }
650
+ }
651
+ /** Writes one `style` declaration block, replacing whatever was there. */
652
+ function writeStyle(element, style) {
653
+ element.setAttribute('style', style);
654
+ }
655
+ export function applyEmbedElementLayout(element, updates) {
656
+ if (updates.width !== undefined) {
657
+ element.setAttribute(EMBED_ATTR.WIDTH, String(clampEmbedWidth(updates.width)));
658
+ }
659
+ if (updates.align !== undefined) {
660
+ element.setAttribute(EMBED_ATTR.ALIGN, normalizeEmbedAlign(updates.align));
661
+ }
662
+ writeStyle(element, embedBlockStyle({
663
+ width: clampEmbedWidth(parseFloat(element.getAttribute(EMBED_ATTR.WIDTH) || '')),
664
+ align: normalizeEmbedAlign(element.getAttribute(EMBED_ATTR.ALIGN) || ''),
665
+ }));
666
+ }
667
+ /**
668
+ * Adds, updates or removes the caption element in place. The caption lives both
669
+ * as an attribute (so it survives a sandbox re-render) and as visible text.
670
+ */
671
+ export function applyEmbedElementCaption(element, caption) {
672
+ var trimmed = caption || '';
673
+ var node = element.querySelector(".".concat(EMBED_CAPTION_CLASS_NAME));
674
+ if (!trimmed) {
675
+ element.removeAttribute(EMBED_ATTR.CAPTION);
676
+ if (node && node.parentNode) {
677
+ node.parentNode.removeChild(node);
678
+ }
679
+ return;
680
+ }
681
+ element.setAttribute(EMBED_ATTR.CAPTION, trimmed);
682
+ if (!node) {
683
+ node = element.ownerDocument.createElement('figcaption');
684
+ node.setAttribute('class', EMBED_CAPTION_CLASS_NAME);
685
+ writeStyle(node, EMBED_CAPTION_STYLE);
686
+ element.appendChild(node);
687
+ }
688
+ node.textContent = trimmed;
689
+ }
690
+ /* ── The sandbox payload ────────────────────────────────────────────────────
691
+ *
692
+ * Three surfaces mount a sandboxed embed: the studio's Quill editor preview,
693
+ * the studio canvas (Solid) and the exported Next.js app (React). The WIRING
694
+ * has to differ — each framework owns its own effect and cleanup — but the
695
+ * payload must not, or an embed would behave differently depending on where it
696
+ * is looked at. Everything that decides behaviour lives here as data: the
697
+ * sandbox flags, the document the iframe is given, the height protocol and its
698
+ * bounds. Each surface only concatenates and mounts.
699
+ */
700
+ /**
701
+ * `allow-same-origin` is deliberately absent: combined with `allow-scripts` it
702
+ * lets a frame reach into the host page and even remove its own sandbox, which
703
+ * is the whole thing this mode exists to prevent.
704
+ */
705
+ export var EMBED_SANDBOX_FLAGS_STRICT = 'allow-scripts allow-popups allow-popups-to-escape-sandbox allow-forms allow-presentation';
706
+ /** The opt-in for widgets that refuse to render without storage access. */
707
+ export var EMBED_SANDBOX_FLAGS_TRUSTED = "".concat(EMBED_SANDBOX_FLAGS_STRICT, " allow-same-origin");
708
+ export function resolveEmbedSandboxFlags(level) {
709
+ return level === 'trusted' ? EMBED_SANDBOX_FLAGS_TRUSTED : EMBED_SANDBOX_FLAGS_STRICT;
710
+ }
711
+ export var EMBED_HEIGHT_MESSAGE_TYPE = 'tq-embed-height';
712
+ export var EMBED_TOKEN_PLACEHOLDER = '__TQ_EMBED_TOKEN__';
713
+ export var EMBED_MIN_HEIGHT = 80;
714
+ export var EMBED_MAX_HEIGHT = 6000;
715
+ /** Until the frame reports back, this is what it occupies. */
716
+ export var EMBED_INITIAL_HEIGHT = 320;
717
+ export var EMBED_SANDBOX_DOC_HEAD = '<!doctype html><html><head><meta charset="utf-8">' +
718
+ '<meta name="viewport" content="width=device-width, initial-scale=1">' +
719
+ '<base target="_blank">' +
720
+ '<style>html,body{margin:0;padding:0;background:transparent}' +
721
+ 'img,iframe,video,blockquote{max-width:100%}</style>' +
722
+ '<script>(function(){' +
723
+ "var token=".concat(JSON.stringify(EMBED_TOKEN_PLACEHOLDER), ";") +
724
+ 'var last=0;' +
725
+ 'function report(){try{' +
726
+ 'var doc=document.documentElement;var body=document.body;' +
727
+ 'var height=Math.max(doc?doc.scrollHeight:0,doc?doc.offsetHeight:0,' +
728
+ 'body?body.scrollHeight:0,body?body.offsetHeight:0);' +
729
+ 'if(!height||height===last){return;}last=height;' +
730
+ "parent.postMessage({type:".concat(JSON.stringify(EMBED_HEIGHT_MESSAGE_TYPE), ",token:token,height:height},\"*\");") +
731
+ '}catch(e){}}' +
732
+ 'function watch(){report();' +
733
+ 'if(typeof ResizeObserver==="function"&&document.documentElement){' +
734
+ 'new ResizeObserver(report).observe(document.documentElement);}' +
735
+ 'if(typeof MutationObserver==="function"&&document.body){' +
736
+ 'new MutationObserver(report).observe(document.body,' +
737
+ '{childList:true,subtree:true,attributes:true});}' +
738
+ // Several providers paint well after `load` (they fetch, then swap in their
739
+ // own iframe), so a few late reports are what stops a widget from sitting in
740
+ // a box sized for its placeholder.
741
+ 'var delays=[150,400,900,1800,3500];' +
742
+ 'for(var i=0;i<delays.length;i++){setTimeout(report,delays[i]);}}' +
743
+ 'if(document.readyState==="loading"){document.addEventListener("DOMContentLoaded",watch);}' +
744
+ 'else{watch();}window.addEventListener("load",report);' +
745
+ '})();</script></head><body>';
746
+ export var EMBED_SANDBOX_DOC_TAIL = '</body></html>';
747
+ /**
748
+ * The document a sandboxed embed is given. Assign it to `iframe.srcdoc` as a
749
+ * property — writing it into the attribute would need HTML escaping.
750
+ */
751
+ export function buildEmbedSandboxDocument(code, token) {
752
+ return (EMBED_SANDBOX_DOC_HEAD.replace(EMBED_TOKEN_PLACEHOLDER, token) +
753
+ (code || '') +
754
+ EMBED_SANDBOX_DOC_TAIL);
755
+ }
756
+ /**
757
+ * A sandboxed frame has an opaque origin, so `event.origin` is `"null"` and
758
+ * cannot identify it. The per-frame token baked into its document is the guard
759
+ * instead: a message is only ever matched against tokens this page minted.
760
+ */
761
+ export function parseEmbedHeightMessage(data) {
762
+ if (!data || typeof data !== 'object') {
763
+ return null;
764
+ }
765
+ var message = data;
766
+ if (message.type !== EMBED_HEIGHT_MESSAGE_TYPE || typeof message.token !== 'string') {
767
+ return null;
768
+ }
769
+ var height = Number(message.height);
770
+ if (!isFinite(height) || height <= 0) {
771
+ return null;
772
+ }
773
+ return {
774
+ token: message.token,
775
+ height: Math.min(Math.max(Math.round(height), EMBED_MIN_HEIGHT), EMBED_MAX_HEIGHT),
776
+ };
777
+ }
778
+ var embedTokenCounter = 0;
779
+ export function createEmbedToken() {
780
+ embedTokenCounter += 1;
781
+ return "tqe-".concat(Date.now().toString(36), "-").concat(embedTokenCounter.toString(36), "-").concat(Math.random()
782
+ .toString(36)
783
+ .slice(2, 8));
784
+ }
785
+ /**
786
+ * Restores every embed inside `root` to its canonical STORED form, in place.
787
+ *
788
+ * An editor reads its value back out of the live DOM (Quill hands back
789
+ * `root.innerHTML`), and by then the DOM carries things the content column must
790
+ * never keep: the preview an editor mounted next to a sandboxed embed, and the
791
+ * marker a runtime writes once it has handled a block. The editor normalizes a
792
+ * detached CLONE of its root through this before emitting a value, so what is
793
+ * stored is exactly what `buildEmbedElementHtml` would have produced — whatever
794
+ * the preview did to the live DOM.
795
+ *
796
+ * The frame restore is a self-heal for the other mounting strategy: the canvas
797
+ * and the published page mount INTO the frame, because they never read back.
798
+ */
799
+ export function normalizeEmbedsForStorage(root) {
800
+ var previews = root.querySelectorAll("[".concat(EMBED_ATTR.PREVIEW, "]"));
801
+ for (var index = previews.length - 1; index >= 0; index -= 1) {
802
+ var preview = previews[index];
803
+ if (preview.parentNode) {
804
+ preview.parentNode.removeChild(preview);
805
+ }
806
+ }
807
+ var blocks = root.querySelectorAll("[".concat(EMBED_ATTR.MARKER, "]"));
808
+ for (var index = 0; index < blocks.length; index += 1) {
809
+ var block = blocks[index];
810
+ block.removeAttribute(EMBED_ATTR.ACTIVATED);
811
+ var frame = findEmbedFrame(block);
812
+ if (!frame) {
813
+ continue;
814
+ }
815
+ var value = readEmbedValueFromElement(block);
816
+ if (value.mode !== 'sandbox') {
817
+ continue;
818
+ }
819
+ frame.innerHTML = buildEmbedFrameChildren(value);
820
+ }
821
+ }
822
+ //# sourceMappingURL=rich-text-embeds.js.map