social-embeds 0.2.0 → 0.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.
- package/README.md +113 -33
- package/dist/{index-CGSxjvGN.d.ts → index-7CfJ3zQZ.d.ts} +4 -9
- package/dist/index.d.ts +53 -31
- package/dist/index.js +354 -109
- package/dist/{parser-B-_erpaf.js → parser-HhmrfoYj.js} +337 -41
- package/dist/parser.d.ts +1 -1
- package/dist/parser.js +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { a as escapeAttribute, i as x$1, n as matchUrl, o as parseHtml, r as platforms, s as rebuild, t as parseEmbed } from "./parser-
|
|
3
|
-
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { a as escapeAttribute, i as x$1, n as matchUrl, o as parseHtml, r as platforms, s as rebuild, t as parseEmbed } from "./parser-HhmrfoYj.js";
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
4
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
5
|
// src/loader/load-script.ts
|
|
6
6
|
const ATTEMPTS$1 = 3;
|
|
@@ -29,8 +29,9 @@ var LoadError = class extends Error {};
|
|
|
29
29
|
async function add(script) {
|
|
30
30
|
const ready = script.readyCallback?.();
|
|
31
31
|
for (let attempt = 1;; attempt++) try {
|
|
32
|
-
await insert(script);
|
|
33
|
-
break;
|
|
32
|
+
const element = await insert(script);
|
|
33
|
+
if (attempt >= ATTEMPTS$1 || !script.skipped?.()) break;
|
|
34
|
+
element.remove();
|
|
34
35
|
} catch (error) {
|
|
35
36
|
if (!(error instanceof LoadError) || attempt >= ATTEMPTS$1) throw error;
|
|
36
37
|
await delay(RETRY_DELAY_MS$1 * 2 ** (attempt - 1));
|
|
@@ -39,8 +40,8 @@ async function add(script) {
|
|
|
39
40
|
}
|
|
40
41
|
async function insert(script) {
|
|
41
42
|
const restore = script.prepare?.();
|
|
43
|
+
const element = document.createElement("script");
|
|
42
44
|
const loaded = new Promise((resolve, reject) => {
|
|
43
|
-
const element = document.createElement("script");
|
|
44
45
|
element.src = script.url;
|
|
45
46
|
element.async = true;
|
|
46
47
|
if (script.crossOrigin) element.crossOrigin = script.crossOrigin;
|
|
@@ -58,6 +59,7 @@ async function insert(script) {
|
|
|
58
59
|
} finally {
|
|
59
60
|
restore?.();
|
|
60
61
|
}
|
|
62
|
+
return element;
|
|
61
63
|
}
|
|
62
64
|
function poll(script) {
|
|
63
65
|
return new Promise((resolve, reject) => {
|
|
@@ -89,10 +91,7 @@ function delay(ms) {
|
|
|
89
91
|
}
|
|
90
92
|
// src/loader/platform-scripts.ts
|
|
91
93
|
const globals$1 = () => window;
|
|
92
|
-
/**
|
|
93
|
-
* The SDK refuses to parse without a Graph API version. Social plugins render with old and
|
|
94
|
-
* new versions alike; this is the one Meta's Embedded Posts documentation uses.
|
|
95
|
-
*/
|
|
94
|
+
/** The SDK refuses to parse without a Graph API version (docs/decisions.md D26). */
|
|
96
95
|
const FACEBOOK_SDK_VERSION = "v26.0";
|
|
97
96
|
const LOCALE = /^[a-z]{2,3}_[A-Z]{2}$/;
|
|
98
97
|
/** Instagram and Threads share `instgrm.Embeds`; each registers its name when loaded. */
|
|
@@ -115,12 +114,6 @@ const x = {
|
|
|
115
114
|
else resolve();
|
|
116
115
|
})
|
|
117
116
|
};
|
|
118
|
-
/**
|
|
119
|
-
* Instagram's and Threads' embed.js do nothing when a loaded Facebook SDK is on the page:
|
|
120
|
-
* both start with `(window.FB && !window.FB.__buffer) || …`. The SDK's own stub carries
|
|
121
|
-
* `__buffer` while the SDK loads, so a loaded SDK is marked the same way while the script
|
|
122
|
-
* runs. Neither script reads `FB` anywhere else (docs/decisions.md D38).
|
|
123
|
-
*/
|
|
124
117
|
function besideFacebookSdk() {
|
|
125
118
|
const fb = globals$1().FB;
|
|
126
119
|
if (!fb || fb.__buffer) return () => {};
|
|
@@ -129,24 +122,29 @@ function besideFacebookSdk() {
|
|
|
129
122
|
delete fb.__buffer;
|
|
130
123
|
};
|
|
131
124
|
}
|
|
125
|
+
function skippedBesideFacebookSdk(name) {
|
|
126
|
+
const registered = embedSdkRegistered(name);
|
|
127
|
+
return () => {
|
|
128
|
+
const fb = globals$1().FB;
|
|
129
|
+
return !registered() && fb !== void 0 && !fb.__buffer;
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
132
|
const instagram = {
|
|
133
133
|
key: "instagram",
|
|
134
134
|
url: "https://www.instagram.com/embed.js",
|
|
135
135
|
isOwn: (src) => /^https?:\/\/www\.instagram\.com\/embed\.js(?:[?#]|$)/.test(src),
|
|
136
136
|
isPresent: embedSdkRegistered("instagram"),
|
|
137
|
-
prepare: besideFacebookSdk
|
|
137
|
+
prepare: besideFacebookSdk,
|
|
138
|
+
skipped: skippedBesideFacebookSdk("instagram")
|
|
138
139
|
};
|
|
139
140
|
const threads = {
|
|
140
141
|
key: "threads",
|
|
141
142
|
url: "https://www.threads.com/embed.js",
|
|
142
143
|
isOwn: (src) => /^https?:\/\/www\.threads\.(?:com|net)\/embed\.js(?:[?#]|$)/.test(src),
|
|
143
144
|
isPresent: embedSdkRegistered("threads"),
|
|
144
|
-
prepare: besideFacebookSdk
|
|
145
|
+
prepare: besideFacebookSdk,
|
|
146
|
+
skipped: skippedBesideFacebookSdk("threads")
|
|
145
147
|
};
|
|
146
|
-
/**
|
|
147
|
-
* `xfbml=0` stops the SDK from scanning the whole page; embeds are parsed explicitly. One
|
|
148
|
-
* SDK serves the whole page, so the first locale requested wins.
|
|
149
|
-
*/
|
|
150
148
|
function facebook(locale) {
|
|
151
149
|
return {
|
|
152
150
|
key: "facebook",
|
|
@@ -163,17 +161,23 @@ function facebook(locale) {
|
|
|
163
161
|
})
|
|
164
162
|
};
|
|
165
163
|
}
|
|
164
|
+
const pinterest = {
|
|
165
|
+
key: "pinterest",
|
|
166
|
+
url: "https://assets.pinterest.com/js/pinit_main.js",
|
|
167
|
+
isOwn: (src) => /^https?:\/\/assets\.pinterest\.com\/js\/pinit(?:_main)?\.js(?:[?#]|$)/.test(src),
|
|
168
|
+
isPresent: () => typeof globals$1().PinUtils?.build === "function"
|
|
169
|
+
};
|
|
166
170
|
function loadPlatformScript(platform, { locale = "en_US" } = {}) {
|
|
167
171
|
switch (platform) {
|
|
168
172
|
case "x": return loadScript(x);
|
|
169
173
|
case "instagram": return loadScript(instagram);
|
|
170
174
|
case "threads": return loadScript(threads);
|
|
175
|
+
case "pinterest": return loadScript(pinterest);
|
|
171
176
|
case "facebook": return LOCALE.test(locale) ? loadScript(facebook(locale)) : Promise.reject(/* @__PURE__ */ new Error(`social-embeds: invalid Facebook locale "${locale}"`));
|
|
172
177
|
}
|
|
173
178
|
}
|
|
174
179
|
// src/components/drivers.ts
|
|
175
180
|
const globals = () => window;
|
|
176
|
-
/** Calls `done` once `rendered(container)` holds, checking after every DOM change. */
|
|
177
181
|
function watch(container, rendered, done) {
|
|
178
182
|
if (rendered(container)) {
|
|
179
183
|
done();
|
|
@@ -198,17 +202,10 @@ function watch(container, rendered, done) {
|
|
|
198
202
|
observer.disconnect();
|
|
199
203
|
};
|
|
200
204
|
}
|
|
201
|
-
/**
|
|
202
|
-
* widgets.js and embed.js render every blockquote with their class anywhere in the page, on
|
|
203
|
-
* load and on each call, so the components write the blockquote without it. The class is
|
|
204
|
-
* added once, when this embed renders; a platform that has since replaced it with its own
|
|
205
|
-
* (Threads' `text-post-media-registered`) keeps that (docs/decisions.md D39).
|
|
206
|
-
*/
|
|
207
205
|
function reveal(container, className) {
|
|
208
206
|
const blockquote = container.querySelector("blockquote");
|
|
209
207
|
if (blockquote && !blockquote.hasAttribute("class")) blockquote.classList.add(className);
|
|
210
208
|
}
|
|
211
|
-
/** An iframe in `container` that the platform has given a height. */
|
|
212
209
|
function hasSizedIframe(container) {
|
|
213
210
|
return [...container.querySelectorAll("iframe")].some((iframe) => Number.parseFloat(iframe.style.height) > 0);
|
|
214
211
|
}
|
|
@@ -283,13 +280,21 @@ function facebookDriver(locale) {
|
|
|
283
280
|
}
|
|
284
281
|
return driver;
|
|
285
282
|
}
|
|
283
|
+
const pinterestDriver = {
|
|
284
|
+
load: () => loadPlatformScript("pinterest"),
|
|
285
|
+
render(container, done) {
|
|
286
|
+
const build = globals().PinUtils?.build;
|
|
287
|
+
if (!build) throw new Error("social-embeds: PinUtils.build is missing");
|
|
288
|
+
const stop = watch(container, (element) => element.querySelector("span[data-pin-id]") !== null, done);
|
|
289
|
+
const link = container.querySelector("a");
|
|
290
|
+
if (link && !link.hasAttribute("data-pin-do")) link.setAttribute("data-pin-do", "embedPin");
|
|
291
|
+
build(container);
|
|
292
|
+
return stop;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
286
295
|
// src/components/hooks.ts
|
|
287
296
|
const ROOT_MARGIN = "200px 0px";
|
|
288
|
-
/**
|
|
289
|
-
* Whether the element has come near the viewport. Always false on the server and in the
|
|
290
|
-
* first client render, so server and client markup match; true right after mount when
|
|
291
|
-
* `lazy` is off or IntersectionObserver is unavailable.
|
|
292
|
-
*/
|
|
297
|
+
/** False on the server and in the first client render, so the markup matches on hydration. */
|
|
293
298
|
function useNearViewport(ref, lazy) {
|
|
294
299
|
const [near, setNear] = useState(false);
|
|
295
300
|
useEffect(() => {
|
|
@@ -326,16 +331,65 @@ const FILL = {
|
|
|
326
331
|
position: "absolute",
|
|
327
332
|
inset: 0
|
|
328
333
|
};
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
* never missed before hydration; the placeholder covers the box until then.
|
|
333
|
-
*/
|
|
334
|
-
function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, ratio, size, width = "100%", maxWidth, height, maxHeight, placeholder, lazy = true, onLoad, style, ...rest }) {
|
|
334
|
+
const HEIGHT_TIMEOUT_MS = 15e3;
|
|
335
|
+
const MAX_REPORTED_HEIGHT = 5e4;
|
|
336
|
+
function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, sandbox, ratio, size, messageHeight, startHeight, width = "100%", maxWidth, height, maxHeight, placeholder, lazy = true, onLoad, onError, style, ...rest }) {
|
|
335
337
|
const frame = useRef(null);
|
|
338
|
+
const iframe = useRef(null);
|
|
336
339
|
const near = useNearViewport(frame, lazy);
|
|
337
340
|
const [loaded, setLoaded] = useState(false);
|
|
341
|
+
const [reportedHeight, setReportedHeight] = useState();
|
|
338
342
|
const reported = useRef(false);
|
|
343
|
+
const callbacks = useRef({
|
|
344
|
+
onLoad,
|
|
345
|
+
onError
|
|
346
|
+
});
|
|
347
|
+
useEffect(() => {
|
|
348
|
+
callbacks.current = {
|
|
349
|
+
onLoad,
|
|
350
|
+
onError
|
|
351
|
+
};
|
|
352
|
+
});
|
|
353
|
+
const report = useCallback(() => {
|
|
354
|
+
setLoaded(true);
|
|
355
|
+
if (reported.current) return;
|
|
356
|
+
reported.current = true;
|
|
357
|
+
callbacks.current.onLoad?.();
|
|
358
|
+
}, []);
|
|
359
|
+
useEffect(() => {
|
|
360
|
+
if (!near || !messageHeight) return;
|
|
361
|
+
const { origin, read } = messageHeight;
|
|
362
|
+
let heard = false;
|
|
363
|
+
const listen = (event) => {
|
|
364
|
+
if (event.origin !== origin) return;
|
|
365
|
+
if (!iframe.current || event.source !== iframe.current.contentWindow) return;
|
|
366
|
+
let value;
|
|
367
|
+
try {
|
|
368
|
+
value = read(event.data);
|
|
369
|
+
} catch {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
if (typeof value !== "number" || !(value > 0 && value <= MAX_REPORTED_HEIGHT)) return;
|
|
373
|
+
heard = true;
|
|
374
|
+
setReportedHeight(Math.ceil(value));
|
|
375
|
+
report();
|
|
376
|
+
};
|
|
377
|
+
window.addEventListener("message", listen);
|
|
378
|
+
const timer = setTimeout(() => {
|
|
379
|
+
if (heard) return;
|
|
380
|
+
setLoaded(true);
|
|
381
|
+
callbacks.current.onError?.(/* @__PURE__ */ new Error(`social-embeds: ${title} did not report its height`));
|
|
382
|
+
}, HEIGHT_TIMEOUT_MS);
|
|
383
|
+
return () => {
|
|
384
|
+
window.removeEventListener("message", listen);
|
|
385
|
+
clearTimeout(timer);
|
|
386
|
+
};
|
|
387
|
+
}, [
|
|
388
|
+
near,
|
|
389
|
+
messageHeight,
|
|
390
|
+
title,
|
|
391
|
+
report
|
|
392
|
+
]);
|
|
339
393
|
const aspectWidth = size?.width ?? ratio.width;
|
|
340
394
|
const aspectHeight = size?.height ?? ratio.height;
|
|
341
395
|
const widest = maxHeight === void 0 || height !== void 0 ? void 0 : Math.floor(maxHeight * aspectWidth / aspectHeight);
|
|
@@ -343,7 +397,7 @@ function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, ratio, size
|
|
|
343
397
|
position: "relative",
|
|
344
398
|
width,
|
|
345
399
|
maxWidth: widest === void 0 || maxWidth !== void 0 && maxWidth < widest ? maxWidth : widest,
|
|
346
|
-
...height === void 0 ? { aspectRatio: `${String(aspectWidth)} / ${String(aspectHeight)}` } : {
|
|
400
|
+
...messageHeight ? { height: reportedHeight ?? startHeight } : height === void 0 ? { aspectRatio: `${String(aspectWidth)} / ${String(aspectHeight)}` } : {
|
|
347
401
|
height,
|
|
348
402
|
maxHeight
|
|
349
403
|
},
|
|
@@ -356,12 +410,14 @@ function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, ratio, size
|
|
|
356
410
|
ref: frame,
|
|
357
411
|
style: FILL,
|
|
358
412
|
children: [near && /* @__PURE__ */ jsx("iframe", {
|
|
413
|
+
ref: iframe,
|
|
359
414
|
src,
|
|
360
415
|
title,
|
|
361
416
|
allow,
|
|
362
417
|
allowFullScreen: true,
|
|
363
418
|
referrerPolicy,
|
|
364
419
|
scrolling,
|
|
420
|
+
sandbox,
|
|
365
421
|
style: {
|
|
366
422
|
display: "block",
|
|
367
423
|
width: "100%",
|
|
@@ -369,10 +425,7 @@ function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, ratio, size
|
|
|
369
425
|
border: 0
|
|
370
426
|
},
|
|
371
427
|
onLoad: () => {
|
|
372
|
-
|
|
373
|
-
if (reported.current) return;
|
|
374
|
-
reported.current = true;
|
|
375
|
-
onLoad?.();
|
|
428
|
+
if (!messageHeight) report();
|
|
376
429
|
}
|
|
377
430
|
}), !loaded && /* @__PURE__ */ jsx("div", {
|
|
378
431
|
style: FILL,
|
|
@@ -381,7 +434,6 @@ function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, ratio, size
|
|
|
381
434
|
})
|
|
382
435
|
});
|
|
383
436
|
}
|
|
384
|
-
/** The default placeholder: a neutral box, readable on light and dark pages. */
|
|
385
437
|
function Skeleton({ minHeight }) {
|
|
386
438
|
return /* @__PURE__ */ jsx("div", {
|
|
387
439
|
"aria-hidden": "true",
|
|
@@ -394,7 +446,6 @@ function Skeleton({ minHeight }) {
|
|
|
394
446
|
});
|
|
395
447
|
}
|
|
396
448
|
// src/parser/verify.ts
|
|
397
|
-
/** The kind and variant pairs the parser produces for each platform. */
|
|
398
449
|
const SHAPES = {
|
|
399
450
|
x: ["post sdk"],
|
|
400
451
|
instagram: ["post sdk"],
|
|
@@ -405,23 +456,21 @@ const SHAPES = {
|
|
|
405
456
|
"video iframe"
|
|
406
457
|
],
|
|
407
458
|
tiktok: ["video iframe"],
|
|
408
|
-
youtube: ["video iframe"]
|
|
459
|
+
youtube: ["video iframe"],
|
|
460
|
+
telegram: ["post iframe"],
|
|
461
|
+
snapchat: ["post iframe", "video iframe"],
|
|
462
|
+
linkedin: ["post iframe"],
|
|
463
|
+
reddit: ["post iframe"],
|
|
464
|
+
pinterest: ["post sdk"]
|
|
409
465
|
};
|
|
410
466
|
/**
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
* - Its URL must be one `matchUrl` recognises for the same platform and id, and its kind
|
|
415
|
-
* and variant a pair the parser produces, because components put the URL and id into
|
|
416
|
-
* iframe sources and platform markup.
|
|
417
|
-
* - Its `fallbackHtml` must be exactly what the platform's whitelist rebuilds, because
|
|
418
|
-
* components write it into the page as HTML. For X it must also link to the same post,
|
|
419
|
-
* because widgets.js renders the post the blockquote links to. Otherwise the fallback is
|
|
420
|
-
* dropped.
|
|
467
|
+
* Security: the embed may be host-built, and its url, id and `fallbackHtml` are written
|
|
468
|
+
* into the page: only what this parser itself produces passes (docs/decisions.md D35).
|
|
421
469
|
*/
|
|
422
470
|
function verifyEmbed(embed) {
|
|
423
471
|
const matched = matchUrl(embed.url);
|
|
424
472
|
if (matched?.platform !== embed.platform || matched.id !== embed.id) return null;
|
|
473
|
+
if (matched.url !== embed.url) return null;
|
|
425
474
|
if (!SHAPES[embed.platform].includes(`${embed.kind} ${embed.variant}`)) return null;
|
|
426
475
|
if (embed.fallbackHtml === void 0) return embed;
|
|
427
476
|
if (trustedFallback(embed, embed.fallbackHtml)) return embed;
|
|
@@ -444,9 +493,13 @@ const NAMES = {
|
|
|
444
493
|
facebook: "Facebook",
|
|
445
494
|
threads: "Threads",
|
|
446
495
|
tiktok: "TikTok",
|
|
447
|
-
youtube: "YouTube"
|
|
496
|
+
youtube: "YouTube",
|
|
497
|
+
telegram: "Telegram",
|
|
498
|
+
snapchat: "Snapchat",
|
|
499
|
+
linkedin: "LinkedIn",
|
|
500
|
+
reddit: "Reddit",
|
|
501
|
+
pinterest: "Pinterest"
|
|
448
502
|
};
|
|
449
|
-
/** The embed for `platform`, or a message saying why the input is not one. */
|
|
450
503
|
function resolveEmbed(url, given, platform) {
|
|
451
504
|
const embed = given ?? (url === void 0 ? null : matchUrl(url));
|
|
452
505
|
if (!embed) return { error: `social-embeds: not a ${NAMES[platform]} post URL: ${String(url)}` };
|
|
@@ -455,17 +508,14 @@ function resolveEmbed(url, given, platform) {
|
|
|
455
508
|
if (!verified) return { error: `social-embeds: the ${NAMES[platform]} embed's url and id do not match` };
|
|
456
509
|
return { embed: verified };
|
|
457
510
|
}
|
|
458
|
-
/**
|
|
511
|
+
/** Later entries replace earlier ones with the same name. */
|
|
459
512
|
function query(...sources) {
|
|
460
513
|
const params = /* @__PURE__ */ new Map();
|
|
461
514
|
for (const source of sources) for (const [key, value] of source ?? []) params.set(key, String(value));
|
|
462
515
|
const pairs = [...params].map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
463
516
|
return pairs.length > 0 ? `?${pairs.join("&")}` : "";
|
|
464
517
|
}
|
|
465
|
-
/**
|
|
466
|
-
* Maps option props to the platform's parameter names. Player parameters take 0 and 1,
|
|
467
|
-
* so booleans become numbers; undefined props are left out.
|
|
468
|
-
*/
|
|
518
|
+
/** Player parameters take 0 and 1, so booleans become numbers. */
|
|
469
519
|
function playerParams(props, names) {
|
|
470
520
|
const params = [];
|
|
471
521
|
for (const [prop, name] of Object.entries(names)) {
|
|
@@ -476,19 +526,14 @@ function playerParams(props, names) {
|
|
|
476
526
|
return params;
|
|
477
527
|
}
|
|
478
528
|
// src/components/facebook-video-iframe.tsx
|
|
479
|
-
const ALLOW$
|
|
480
|
-
/**
|
|
481
|
-
* Facebook video from embed code: the iframe that came in (brief §5.4), rebuilt from the
|
|
482
|
-
* parsed href and parameters. Its content fills the iframe at any width, whatever its
|
|
483
|
-
* `width` parameter says (docs/decisions.md D29). `FacebookEmbed` routes here.
|
|
484
|
-
*/
|
|
529
|
+
const ALLOW$4 = "autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share";
|
|
485
530
|
function FacebookVideoIframe({ embed, ...frame }) {
|
|
486
531
|
const params = Object.entries(embed.options).filter(([key]) => key !== "href");
|
|
487
532
|
return /* @__PURE__ */ jsx(IframeEmbed, {
|
|
488
533
|
...frame,
|
|
489
534
|
src: `https://www.facebook.com/plugins/video.php${query([["href", embed.url]], params)}`,
|
|
490
535
|
title: "Facebook video player",
|
|
491
|
-
allow: ALLOW$
|
|
536
|
+
allow: ALLOW$4,
|
|
492
537
|
scrolling: "no",
|
|
493
538
|
ratio: {
|
|
494
539
|
width: 16,
|
|
@@ -498,7 +543,7 @@ function FacebookVideoIframe({ embed, ...frame }) {
|
|
|
498
543
|
});
|
|
499
544
|
}
|
|
500
545
|
// src/components/markup.ts
|
|
501
|
-
/** `
|
|
546
|
+
/** `true` becomes a valueless attribute. */
|
|
502
547
|
function attributes(entries) {
|
|
503
548
|
let html = "";
|
|
504
549
|
for (const [name, value] of Object.entries(entries)) {
|
|
@@ -507,10 +552,7 @@ function attributes(entries) {
|
|
|
507
552
|
}
|
|
508
553
|
return html;
|
|
509
554
|
}
|
|
510
|
-
/**
|
|
511
|
-
* Options written as the platform's `data-*` attributes, in the given order. Booleans are
|
|
512
|
-
* written as `"true"`/`"false"`, as platforms document them.
|
|
513
|
-
*/
|
|
555
|
+
/** Booleans are written as `"true"` and `"false"`, the way the platforms document them. */
|
|
514
556
|
function dataAttributes(names, options) {
|
|
515
557
|
const entries = {};
|
|
516
558
|
for (const [key, name] of Object.entries(names)) {
|
|
@@ -535,11 +577,6 @@ const RETRY_DELAY_MS = 1e3;
|
|
|
535
577
|
function toError(error) {
|
|
536
578
|
return error instanceof Error ? error : new Error(String(error));
|
|
537
579
|
}
|
|
538
|
-
/**
|
|
539
|
-
* Group A: the platform markup is written once into a container React never updates
|
|
540
|
-
* again, so the platform script can replace it in place (brief §6.3). Callers key this
|
|
541
|
-
* component by `markup`, so different markup gets a fresh container.
|
|
542
|
-
*/
|
|
543
580
|
function ScriptEmbed({ driver, markup, hasFallback, minHeight, label, width = "100%", maxWidth, placeholder, lazy = true, onLoad, onError, style, ...rest }) {
|
|
544
581
|
const container = useRef(null);
|
|
545
582
|
const near = useNearViewport(container, lazy);
|
|
@@ -624,7 +661,7 @@ function ScriptEmbed({ driver, markup, hasFallback, minHeight, label, width = "1
|
|
|
624
661
|
});
|
|
625
662
|
}
|
|
626
663
|
// src/components/facebook-embed.tsx
|
|
627
|
-
const ATTRIBUTES$
|
|
664
|
+
const ATTRIBUTES$2 = {
|
|
628
665
|
width: "data-width",
|
|
629
666
|
showText: "data-show-text",
|
|
630
667
|
lazy: "data-lazy",
|
|
@@ -632,7 +669,6 @@ const ATTRIBUTES$1 = {
|
|
|
632
669
|
autoplay: "data-autoplay",
|
|
633
670
|
showCaptions: "data-show-captions"
|
|
634
671
|
};
|
|
635
|
-
/** The video options under the names `plugins/video.php` reads them by. */
|
|
636
672
|
const PLAYER_PARAMETERS = {
|
|
637
673
|
showText: "show_text",
|
|
638
674
|
allowfullscreen: "allowfullscreen",
|
|
@@ -642,16 +678,10 @@ const PLAYER_PARAMETERS = {
|
|
|
642
678
|
/** Embedded Posts: "Max. 750 pixel"; videos have no upper limit (docs/decisions.md D33). */
|
|
643
679
|
const POST_MAX_WIDTH = 750;
|
|
644
680
|
/** docs/decisions.md D34. */
|
|
645
|
-
const MIN_HEIGHT$
|
|
681
|
+
const MIN_HEIGHT$4 = {
|
|
646
682
|
post: 300,
|
|
647
683
|
video: 200
|
|
648
684
|
};
|
|
649
|
-
/**
|
|
650
|
-
* Facebook rendering rules (brief §5.4): posts always through the SDK; video embed code as
|
|
651
|
-
* the iframe that came in; video URLs through the SDK. The parser has set `variant`.
|
|
652
|
-
* Video embed code that shows the post text also goes through the SDK (D44), unless
|
|
653
|
-
* `showText` turns the text off: then it is the plain iframe (D45).
|
|
654
|
-
*/
|
|
655
685
|
function FacebookEmbed(props) {
|
|
656
686
|
const { url, embed: given, onError, locale, showText, allowfullscreen, autoplay, showCaptions, height, maxHeight, ...shared } = props;
|
|
657
687
|
const { embed, error } = resolveEmbed(url, given, "facebook");
|
|
@@ -691,7 +721,7 @@ function FacebookEmbed(props) {
|
|
|
691
721
|
});
|
|
692
722
|
}
|
|
693
723
|
const sdkLocale = locale ?? (typeof options.locale === "string" ? options.locale : "en_US");
|
|
694
|
-
const markup = `<div class="${embed.kind === "video" ? "fb-video" : "fb-post"}" data-href="${escapeAttribute(embed.url)}"${dataAttributes(ATTRIBUTES$
|
|
724
|
+
const markup = `<div class="${embed.kind === "video" ? "fb-video" : "fb-post"}" data-href="${escapeAttribute(embed.url)}"${dataAttributes(ATTRIBUTES$2, options)}>${embed.fallbackHtml ?? ""}</div>`;
|
|
695
725
|
return /* @__PURE__ */ jsx(ScriptEmbed, {
|
|
696
726
|
maxWidth: embed.kind === "post" ? POST_MAX_WIDTH : void 0,
|
|
697
727
|
...shared,
|
|
@@ -699,18 +729,13 @@ function FacebookEmbed(props) {
|
|
|
699
729
|
driver: facebookDriver(sdkLocale),
|
|
700
730
|
markup,
|
|
701
731
|
hasFallback: hasFallback(embed.fallbackHtml),
|
|
702
|
-
minHeight: MIN_HEIGHT$
|
|
732
|
+
minHeight: MIN_HEIGHT$4[embed.kind],
|
|
703
733
|
label: "Facebook"
|
|
704
734
|
}, `${sdkLocale} ${markup}`);
|
|
705
735
|
}
|
|
706
736
|
// src/components/instagram-embed.tsx
|
|
707
737
|
/** A photo post without caption at 375px, the smallest measured (docs/decisions.md D34). */
|
|
708
|
-
const MIN_HEIGHT$
|
|
709
|
-
/**
|
|
710
|
-
* embed.js copies the blockquote's inline style onto its iframe and gives the iframe no
|
|
711
|
-
* width of its own, so without this the post stays 326px wide. The value is the one in
|
|
712
|
-
* Instagram's own embed code (docs/decisions.md D33).
|
|
713
|
-
*/
|
|
738
|
+
const MIN_HEIGHT$3 = 680;
|
|
714
739
|
const FLUID_WIDTH = "width:calc(100% - 2px)";
|
|
715
740
|
function InstagramEmbed(props) {
|
|
716
741
|
const { url, embed: given, onError, maxWidth = 540, captioned, ...shared } = props;
|
|
@@ -731,10 +756,209 @@ function InstagramEmbed(props) {
|
|
|
731
756
|
driver: instagramDriver,
|
|
732
757
|
markup,
|
|
733
758
|
hasFallback: hasFallback(embed.fallbackHtml),
|
|
734
|
-
minHeight: MIN_HEIGHT$
|
|
759
|
+
minHeight: MIN_HEIGHT$3,
|
|
735
760
|
label: "Instagram"
|
|
736
761
|
}, markup);
|
|
737
762
|
}
|
|
763
|
+
// src/components/linkedin-embed.tsx
|
|
764
|
+
const DEFAULT_HEIGHT = 670;
|
|
765
|
+
const DEFAULT_WIDTH = 504;
|
|
766
|
+
/** A fixed height: the `height` prop, else the embed code's, else 670px. */
|
|
767
|
+
function LinkedInEmbed(props) {
|
|
768
|
+
const { url, embed: given, onError, maxWidth, height, collapsed, ...frame } = props;
|
|
769
|
+
const { embed, error } = resolveEmbed(url, given, "linkedin");
|
|
770
|
+
useReportError(error, onError);
|
|
771
|
+
if (!embed) return null;
|
|
772
|
+
const params = new Map(Object.entries(embed.options));
|
|
773
|
+
if (collapsed === true) params.set("collapsed", 1);
|
|
774
|
+
if (collapsed === false) params.delete("collapsed");
|
|
775
|
+
return /* @__PURE__ */ jsx(IframeEmbed, {
|
|
776
|
+
...frame,
|
|
777
|
+
maxWidth: maxWidth ?? embed.size?.width ?? DEFAULT_WIDTH,
|
|
778
|
+
height: height ?? embed.size?.height ?? DEFAULT_HEIGHT,
|
|
779
|
+
src: `https://www.linkedin.com/embed/feed/update/${embed.id}${query(params)}`,
|
|
780
|
+
title: "LinkedIn post",
|
|
781
|
+
allow: "",
|
|
782
|
+
ratio: {
|
|
783
|
+
width: DEFAULT_WIDTH,
|
|
784
|
+
height: DEFAULT_HEIGHT
|
|
785
|
+
}
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
// src/components/pinterest-embed.tsx
|
|
789
|
+
const ATTRIBUTES$1 = {
|
|
790
|
+
width: "data-pin-width",
|
|
791
|
+
lang: "data-pin-lang",
|
|
792
|
+
terse: "data-pin-terse"
|
|
793
|
+
};
|
|
794
|
+
const PIN_WIDTHS = {
|
|
795
|
+
small: 236,
|
|
796
|
+
medium: 345,
|
|
797
|
+
large: 600
|
|
798
|
+
};
|
|
799
|
+
const DEFAULT_PIN_WIDTH = 236;
|
|
800
|
+
const MIN_HEIGHT$2 = 270;
|
|
801
|
+
function PinterestEmbed(props) {
|
|
802
|
+
const { url, embed: given, onError, maxWidth, pinWidth, ...shared } = props;
|
|
803
|
+
const { embed, error } = resolveEmbed(url, given, "pinterest");
|
|
804
|
+
useReportError(error, onError);
|
|
805
|
+
if (!embed) return null;
|
|
806
|
+
const options = withProps(embed.options, { width: pinWidth });
|
|
807
|
+
const pinWidthPx = PIN_WIDTHS[String(options.width)] ?? DEFAULT_PIN_WIDTH;
|
|
808
|
+
const markup = `<a href="${escapeAttribute(embed.url)}"${dataAttributes(ATTRIBUTES$1, options)}></a>`;
|
|
809
|
+
return /* @__PURE__ */ jsx(ScriptEmbed, {
|
|
810
|
+
...shared,
|
|
811
|
+
onError,
|
|
812
|
+
maxWidth: maxWidth ?? pinWidthPx,
|
|
813
|
+
driver: pinterestDriver,
|
|
814
|
+
markup,
|
|
815
|
+
hasFallback: false,
|
|
816
|
+
minHeight: MIN_HEIGHT$2,
|
|
817
|
+
label: "Pinterest"
|
|
818
|
+
}, markup);
|
|
819
|
+
}
|
|
820
|
+
// src/components/reddit-embed.tsx
|
|
821
|
+
const PARAMS$3 = {
|
|
822
|
+
theme: "theme",
|
|
823
|
+
showMedia: "showmedia",
|
|
824
|
+
showTitle: "showtitle",
|
|
825
|
+
showUsername: "showusername",
|
|
826
|
+
locale: "locale",
|
|
827
|
+
depth: "depth"
|
|
828
|
+
};
|
|
829
|
+
const SANDBOX = "allow-scripts allow-same-origin allow-popups";
|
|
830
|
+
const ALLOW$3 = "clipboard-read; clipboard-write";
|
|
831
|
+
const START_HEIGHT = 316;
|
|
832
|
+
/** docs/decisions.md D52. */
|
|
833
|
+
const REDDIT_HEIGHT = {
|
|
834
|
+
origin: "https://embed.reddit.com",
|
|
835
|
+
read(data) {
|
|
836
|
+
if (typeof data !== "string") return void 0;
|
|
837
|
+
const message = JSON.parse(data);
|
|
838
|
+
return message.type === "resize.embed" && typeof message.data === "number" ? message.data : void 0;
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
/** At most 640px wide by default, the width widgets.js gives the frame (docs/decisions.md D52). */
|
|
842
|
+
function RedditEmbed(props) {
|
|
843
|
+
const { url, embed: given, onError, maxWidth = 640, theme, showMedia, showTitle, showUsername, locale, depth, ...shared } = props;
|
|
844
|
+
const { embed, error } = resolveEmbed(url, given, "reddit");
|
|
845
|
+
useReportError(error, onError);
|
|
846
|
+
if (!embed) return null;
|
|
847
|
+
const fromProps = [];
|
|
848
|
+
const values = {
|
|
849
|
+
theme,
|
|
850
|
+
showMedia,
|
|
851
|
+
showTitle,
|
|
852
|
+
showUsername,
|
|
853
|
+
locale,
|
|
854
|
+
depth
|
|
855
|
+
};
|
|
856
|
+
for (const [prop, name] of Object.entries(PARAMS$3)) {
|
|
857
|
+
const value = values[prop];
|
|
858
|
+
if (value !== void 0) fromProps.push([name, value]);
|
|
859
|
+
}
|
|
860
|
+
const path = embed.url.slice(22);
|
|
861
|
+
return /* @__PURE__ */ jsx(IframeEmbed, {
|
|
862
|
+
...shared,
|
|
863
|
+
onError,
|
|
864
|
+
maxWidth,
|
|
865
|
+
src: `https://embed.reddit.com${path}${query([["embed", true]], Object.entries(embed.options), fromProps)}`,
|
|
866
|
+
title: "Reddit post",
|
|
867
|
+
allow: ALLOW$3,
|
|
868
|
+
sandbox: SANDBOX,
|
|
869
|
+
scrolling: "no",
|
|
870
|
+
ratio: {
|
|
871
|
+
width: 1,
|
|
872
|
+
height: 1
|
|
873
|
+
},
|
|
874
|
+
messageHeight: REDDIT_HEIGHT,
|
|
875
|
+
startHeight: embed.size?.height ?? START_HEIGHT
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
// src/components/snapchat-embed.tsx
|
|
879
|
+
const ALLOW$2 = "autoplay; camera; microphone; clipboard-write;";
|
|
880
|
+
/** At most 416px wide by default, the size of Snapchat's own embed code (docs/decisions.md D50). */
|
|
881
|
+
function SnapchatEmbed(props) {
|
|
882
|
+
const { url, embed: given, onError, maxWidth = 416, ...frame } = props;
|
|
883
|
+
const { embed, error } = resolveEmbed(url, given, "snapchat");
|
|
884
|
+
useReportError(error, onError);
|
|
885
|
+
if (!embed) return null;
|
|
886
|
+
const title = embed.options.title;
|
|
887
|
+
return /* @__PURE__ */ jsx(IframeEmbed, {
|
|
888
|
+
...frame,
|
|
889
|
+
maxWidth,
|
|
890
|
+
src: `${embed.url}/embed`,
|
|
891
|
+
title: typeof title === "string" && title !== "" ? title : "Snapchat",
|
|
892
|
+
allow: ALLOW$2,
|
|
893
|
+
scrolling: "no",
|
|
894
|
+
ratio: {
|
|
895
|
+
width: 416,
|
|
896
|
+
height: 692
|
|
897
|
+
},
|
|
898
|
+
size: embed.size
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
// src/components/telegram-embed.tsx
|
|
902
|
+
const PARAMS$2 = [
|
|
903
|
+
["comment", "comment"],
|
|
904
|
+
["userpic", "userpic"],
|
|
905
|
+
["mode", "mode"],
|
|
906
|
+
["single", "single"],
|
|
907
|
+
["color", "color"],
|
|
908
|
+
["dark", "dark"],
|
|
909
|
+
["darkColor", "dark_color"]
|
|
910
|
+
];
|
|
911
|
+
function param(name, value) {
|
|
912
|
+
if (typeof value === "boolean") {
|
|
913
|
+
if (name === "userpic") return String(value);
|
|
914
|
+
if (name === "single") return value ? 1 : void 0;
|
|
915
|
+
return value ? 1 : 0;
|
|
916
|
+
}
|
|
917
|
+
return typeof value === "string" || typeof value === "number" ? value : void 0;
|
|
918
|
+
}
|
|
919
|
+
/** docs/decisions.md D49. */
|
|
920
|
+
const TELEGRAM_HEIGHT = {
|
|
921
|
+
origin: "https://t.me",
|
|
922
|
+
read(data) {
|
|
923
|
+
if (typeof data !== "string") return void 0;
|
|
924
|
+
const message = JSON.parse(data);
|
|
925
|
+
return message.event === "resize" && typeof message.height === "number" ? message.height : void 0;
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
/** At most 500px wide by default, the widest the post renders (docs/decisions.md D49). */
|
|
929
|
+
function TelegramEmbed(props) {
|
|
930
|
+
const { url, embed: given, onError, maxWidth = 500, userpic, color, dark, darkColor, ...shared } = props;
|
|
931
|
+
const { embed, error } = resolveEmbed(url, given, "telegram");
|
|
932
|
+
useReportError(error, onError);
|
|
933
|
+
if (!embed) return null;
|
|
934
|
+
const values = {
|
|
935
|
+
...embed.options,
|
|
936
|
+
...userpic === void 0 ? {} : { userpic },
|
|
937
|
+
...color === void 0 ? {} : { color },
|
|
938
|
+
...dark === void 0 ? {} : { dark },
|
|
939
|
+
...darkColor === void 0 ? {} : { darkColor }
|
|
940
|
+
};
|
|
941
|
+
const params = [["embed", 1]];
|
|
942
|
+
for (const [option, name] of PARAMS$2) {
|
|
943
|
+
const value = param(name, values[option]);
|
|
944
|
+
if (value !== void 0) params.push([name, value]);
|
|
945
|
+
}
|
|
946
|
+
return /* @__PURE__ */ jsx(IframeEmbed, {
|
|
947
|
+
...shared,
|
|
948
|
+
onError,
|
|
949
|
+
maxWidth,
|
|
950
|
+
src: `${embed.url}${query(params)}`,
|
|
951
|
+
title: "Telegram post",
|
|
952
|
+
allow: "",
|
|
953
|
+
scrolling: "no",
|
|
954
|
+
ratio: {
|
|
955
|
+
width: 1,
|
|
956
|
+
height: 1
|
|
957
|
+
},
|
|
958
|
+
messageHeight: TELEGRAM_HEIGHT,
|
|
959
|
+
startHeight: 300
|
|
960
|
+
});
|
|
961
|
+
}
|
|
738
962
|
// src/components/threads-embed.tsx
|
|
739
963
|
/** A text-only post (docs/decisions.md D34). */
|
|
740
964
|
const MIN_HEIGHT$1 = 240;
|
|
@@ -779,10 +1003,7 @@ const PARAMS$1 = {
|
|
|
779
1003
|
forceMuted: "force_muted"
|
|
780
1004
|
};
|
|
781
1005
|
const ALLOW$1 = "accelerometer; autoplay; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; web-share";
|
|
782
|
-
/**
|
|
783
|
-
* Every TikTok input renders the Embed Player iframe (docs/decisions.md D11). It is at
|
|
784
|
-
* most 325px wide, the size of TikTok's own embed code (D40).
|
|
785
|
-
*/
|
|
1006
|
+
/** At most 325px wide by default, the size of TikTok's own embed code (docs/decisions.md D40). */
|
|
786
1007
|
function TikTokEmbed(props) {
|
|
787
1008
|
const { url, embed: given, onError, maxWidth = 325, controls, progressBar, playButton, volumeControl, fullscreenButton, timestamp, loop, autoplay, musicInfo, description, rel, nativeContextMenu, closedCaption, forceMuted, ...frame } = props;
|
|
788
1009
|
const { embed, error } = resolveEmbed(url, given, "tiktok");
|
|
@@ -925,10 +1146,7 @@ function YouTubeEmbed(props) {
|
|
|
925
1146
|
});
|
|
926
1147
|
}
|
|
927
1148
|
// src/components/social-embed.tsx
|
|
928
|
-
/**
|
|
929
|
-
* Recognises embed code or a post URL and renders the platform's component. `height` and
|
|
930
|
-
* `maxHeight` reach only the iframe-based embeds; the others size themselves (brief §6.1).
|
|
931
|
-
*/
|
|
1149
|
+
/** `height` and `maxHeight` reach only the iframe-based embeds: the others size themselves. */
|
|
932
1150
|
function SocialEmbed(props) {
|
|
933
1151
|
const { html, url, fallback, height, maxHeight, onError, ...shared } = props;
|
|
934
1152
|
const frame = {
|
|
@@ -972,6 +1190,33 @@ function SocialEmbed(props) {
|
|
|
972
1190
|
onError,
|
|
973
1191
|
embed
|
|
974
1192
|
});
|
|
1193
|
+
case "telegram": return /* @__PURE__ */ jsx(TelegramEmbed, {
|
|
1194
|
+
...shared,
|
|
1195
|
+
onError,
|
|
1196
|
+
embed
|
|
1197
|
+
});
|
|
1198
|
+
case "snapchat": return /* @__PURE__ */ jsx(SnapchatEmbed, {
|
|
1199
|
+
...shared,
|
|
1200
|
+
...frame,
|
|
1201
|
+
onError,
|
|
1202
|
+
embed
|
|
1203
|
+
});
|
|
1204
|
+
case "linkedin": return /* @__PURE__ */ jsx(LinkedInEmbed, {
|
|
1205
|
+
...shared,
|
|
1206
|
+
...frame,
|
|
1207
|
+
onError,
|
|
1208
|
+
embed
|
|
1209
|
+
});
|
|
1210
|
+
case "reddit": return /* @__PURE__ */ jsx(RedditEmbed, {
|
|
1211
|
+
...shared,
|
|
1212
|
+
onError,
|
|
1213
|
+
embed
|
|
1214
|
+
});
|
|
1215
|
+
case "pinterest": return /* @__PURE__ */ jsx(PinterestEmbed, {
|
|
1216
|
+
...shared,
|
|
1217
|
+
onError,
|
|
1218
|
+
embed
|
|
1219
|
+
});
|
|
975
1220
|
}
|
|
976
1221
|
}
|
|
977
|
-
export { FacebookEmbed, InstagramEmbed, SocialEmbed, ThreadsEmbed, TikTokEmbed, XEmbed, YouTubeEmbed };
|
|
1222
|
+
export { FacebookEmbed, InstagramEmbed, LinkedInEmbed, PinterestEmbed, RedditEmbed, SnapchatEmbed, SocialEmbed, TelegramEmbed, ThreadsEmbed, TikTokEmbed, XEmbed, YouTubeEmbed };
|