social-embeds 0.2.0 → 0.2.1
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.
|
@@ -12,16 +12,11 @@ type ParsedEmbed = {
|
|
|
12
12
|
};
|
|
13
13
|
fallbackHtml?: string;
|
|
14
14
|
};
|
|
15
|
-
/**
|
|
16
|
-
* Recognises a post URL. The result has no fallback content, and options only for data the
|
|
17
|
-
* URL itself carries (docs/decisions.md D13). Never throws.
|
|
18
|
-
*/
|
|
15
|
+
/** Never throws. Options hold only what the URL itself carries (docs/decisions.md D13). */
|
|
19
16
|
declare function matchUrl(url: string): ParsedEmbed | null;
|
|
20
17
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* `<div id="fb-root">`, whitespace and comments. Anything else returns `null`
|
|
24
|
-
* (docs/decisions.md D14). Never throws.
|
|
18
|
+
* Never throws. `null` unless the input is one embed element plus, at most, the platform's
|
|
19
|
+
* own scripts, `<div id="fb-root">`, whitespace and comments (docs/decisions.md D14).
|
|
25
20
|
*/
|
|
26
21
|
declare function parseEmbed(html: string): ParsedEmbed | null;
|
|
27
22
|
export { Platform as i, matchUrl as n, ParsedEmbed as r, parseEmbed as t };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { r as ParsedEmbed } from "./index-
|
|
1
|
+
import { r as ParsedEmbed } from "./index-DCF8EEVz.js";
|
|
2
2
|
import { ComponentProps, ReactNode } from "react";
|
|
3
|
-
/** A platform component takes a post URL or an already-parsed embed (brief §5.2). */
|
|
4
3
|
type EmbedSource = {
|
|
5
4
|
url: string;
|
|
6
5
|
embed?: never;
|
|
@@ -8,7 +7,6 @@ type EmbedSource = {
|
|
|
8
7
|
embed: ParsedEmbed;
|
|
9
8
|
url?: never;
|
|
10
9
|
};
|
|
11
|
-
/** Props every embed component shares (brief §6). */
|
|
12
10
|
type SharedProps = Omit<ComponentProps<'div'>, 'children' | 'onLoad' | 'onError'> & {
|
|
13
11
|
/** Wrapper width. Default `'100%'`. */
|
|
14
12
|
width?: number | string;
|
|
@@ -23,7 +21,6 @@ type SharedProps = Omit<ComponentProps<'div'>, 'children' | 'onLoad' | 'onError'
|
|
|
23
21
|
/** Fires on invalid input, script load failure or init timeout. */
|
|
24
22
|
onError?: (error: Error) => void;
|
|
25
23
|
};
|
|
26
|
-
/** Iframe-based embeds (Group B) may also be given a fixed or a maximum height. */
|
|
27
24
|
type FrameProps = SharedProps & {
|
|
28
25
|
/** A fixed height in pixels, instead of the embed's aspect ratio. */
|
|
29
26
|
height?: number;
|
|
@@ -31,8 +28,7 @@ type FrameProps = SharedProps & {
|
|
|
31
28
|
maxHeight?: number;
|
|
32
29
|
};
|
|
33
30
|
/**
|
|
34
|
-
*
|
|
35
|
-
* `data-lazy` have no props, because `width` and `lazy` are the wrapper's
|
|
31
|
+
* `data-width` and `data-lazy` have no props, because `width` and `lazy` are the wrapper's
|
|
36
32
|
* (docs/decisions.md D33).
|
|
37
33
|
*/
|
|
38
34
|
type FacebookOptions = {
|
|
@@ -48,12 +44,6 @@ type FacebookOptions = {
|
|
|
48
44
|
};
|
|
49
45
|
/** `height` and `maxHeight` apply only to a video that renders as an iframe. */
|
|
50
46
|
type FacebookEmbedProps = EmbedSource & Omit<FrameProps, keyof FacebookOptions> & FacebookOptions;
|
|
51
|
-
/**
|
|
52
|
-
* Facebook rendering rules (brief §5.4): posts always through the SDK; video embed code as
|
|
53
|
-
* the iframe that came in; video URLs through the SDK. The parser has set `variant`.
|
|
54
|
-
* Video embed code that shows the post text also goes through the SDK (D44), unless
|
|
55
|
-
* `showText` turns the text off: then it is the plain iframe (D45).
|
|
56
|
-
*/
|
|
57
47
|
export declare function FacebookEmbed(props: FacebookEmbedProps): import("react").JSX.Element | null;
|
|
58
48
|
type InstagramOptions = {
|
|
59
49
|
/** Show the caption (`data-instgrm-captioned`). */
|
|
@@ -85,14 +75,11 @@ type TikTokOptions = {
|
|
|
85
75
|
forceMuted?: boolean;
|
|
86
76
|
};
|
|
87
77
|
type TikTokEmbedProps = EmbedSource & Omit<FrameProps, keyof TikTokOptions> & TikTokOptions;
|
|
88
|
-
/**
|
|
89
|
-
* Every TikTok input renders the Embed Player iframe (docs/decisions.md D11). It is at
|
|
90
|
-
* most 325px wide, the size of TikTok's own embed code (D40).
|
|
91
|
-
*/
|
|
78
|
+
/** At most 325px wide by default, the size of TikTok's own embed code (docs/decisions.md D40). */
|
|
92
79
|
export declare function TikTokEmbed(props: TikTokEmbedProps): import("react").JSX.Element | null;
|
|
93
80
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
81
|
+
* `data-width` has no prop, because `width` is the wrapper's: the post fills the wrapper up
|
|
82
|
+
* to `maxWidth` (docs/decisions.md D33).
|
|
96
83
|
*/
|
|
97
84
|
type XOptions = {
|
|
98
85
|
theme?: 'light' | 'dark';
|
|
@@ -131,7 +118,6 @@ type YouTubeOptions = {
|
|
|
131
118
|
};
|
|
132
119
|
type YouTubeEmbedProps = EmbedSource & Omit<FrameProps, keyof YouTubeOptions> & YouTubeOptions;
|
|
133
120
|
export declare function YouTubeEmbed(props: YouTubeEmbedProps): import("react").JSX.Element | null;
|
|
134
|
-
/** Embed code or a post URL; never both (brief §5.2). */
|
|
135
121
|
type SocialEmbedSource = {
|
|
136
122
|
html: string;
|
|
137
123
|
url?: never;
|
|
@@ -139,21 +125,14 @@ type SocialEmbedSource = {
|
|
|
139
125
|
url: string;
|
|
140
126
|
html?: never;
|
|
141
127
|
};
|
|
142
|
-
/**
|
|
143
|
-
* Platform options come from the embed code. Div attributes that share a name with one
|
|
144
|
-
* (`lang`, `color`, `rel`) are left out, because each platform reads that name its own way.
|
|
145
|
-
*/
|
|
146
128
|
type PlatformOption = keyof XOptions | keyof InstagramOptions | keyof ThreadsOptions | keyof FacebookOptions | keyof TikTokOptions | keyof YouTubeOptions;
|
|
147
129
|
type SocialEmbedProps = SocialEmbedSource & Omit<FrameProps, PlatformOption> & {
|
|
148
130
|
/**
|
|
149
|
-
* Called with the original string when `html` is not a recognised embed code
|
|
150
|
-
*
|
|
131
|
+
* Called with the original string when `html` is not a recognised embed code. Without
|
|
132
|
+
* it, nothing is rendered and `onError` is called.
|
|
151
133
|
*/
|
|
152
134
|
fallback?: (html: string) => ReactNode;
|
|
153
135
|
};
|
|
154
|
-
/**
|
|
155
|
-
* Recognises embed code or a post URL and renders the platform's component. `height` and
|
|
156
|
-
* `maxHeight` reach only the iframe-based embeds; the others size themselves (brief §6.1).
|
|
157
|
-
*/
|
|
136
|
+
/** `height` and `maxHeight` reach only the iframe-based embeds: the others size themselves. */
|
|
158
137
|
export declare function SocialEmbed(props: SocialEmbedProps): string | number | bigint | boolean | import("react").JSX.Element | Iterable<ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | null | undefined;
|
|
159
138
|
export type { EmbedSource, FacebookEmbedProps, FacebookOptions, FrameProps, InstagramEmbedProps, InstagramOptions, SharedProps, SocialEmbedProps, SocialEmbedSource, ThreadsEmbedProps, ThreadsOptions, TikTokEmbedProps, TikTokOptions, XEmbedProps, XOptions, YouTubeEmbedProps, YouTubeOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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-
|
|
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-BrlUKvvo.js";
|
|
3
3
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
4
4
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
5
|
// src/loader/load-script.ts
|
|
@@ -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",
|
|
@@ -173,7 +171,6 @@ function loadPlatformScript(platform, { locale = "en_US" } = {}) {
|
|
|
173
171
|
}
|
|
174
172
|
// src/components/drivers.ts
|
|
175
173
|
const globals = () => window;
|
|
176
|
-
/** Calls `done` once `rendered(container)` holds, checking after every DOM change. */
|
|
177
174
|
function watch(container, rendered, done) {
|
|
178
175
|
if (rendered(container)) {
|
|
179
176
|
done();
|
|
@@ -198,17 +195,10 @@ function watch(container, rendered, done) {
|
|
|
198
195
|
observer.disconnect();
|
|
199
196
|
};
|
|
200
197
|
}
|
|
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
198
|
function reveal(container, className) {
|
|
208
199
|
const blockquote = container.querySelector("blockquote");
|
|
209
200
|
if (blockquote && !blockquote.hasAttribute("class")) blockquote.classList.add(className);
|
|
210
201
|
}
|
|
211
|
-
/** An iframe in `container` that the platform has given a height. */
|
|
212
202
|
function hasSizedIframe(container) {
|
|
213
203
|
return [...container.querySelectorAll("iframe")].some((iframe) => Number.parseFloat(iframe.style.height) > 0);
|
|
214
204
|
}
|
|
@@ -285,11 +275,7 @@ function facebookDriver(locale) {
|
|
|
285
275
|
}
|
|
286
276
|
// src/components/hooks.ts
|
|
287
277
|
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
|
-
*/
|
|
278
|
+
/** False on the server and in the first client render, so the markup matches on hydration. */
|
|
293
279
|
function useNearViewport(ref, lazy) {
|
|
294
280
|
const [near, setNear] = useState(false);
|
|
295
281
|
useEffect(() => {
|
|
@@ -326,11 +312,6 @@ const FILL = {
|
|
|
326
312
|
position: "absolute",
|
|
327
313
|
inset: 0
|
|
328
314
|
};
|
|
329
|
-
/**
|
|
330
|
-
* Group B: a responsive wrapper whose aspect ratio comes from the input, with the
|
|
331
|
-
* iframe filling it. The iframe is created on the client only, so its load event is
|
|
332
|
-
* never missed before hydration; the placeholder covers the box until then.
|
|
333
|
-
*/
|
|
334
315
|
function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, ratio, size, width = "100%", maxWidth, height, maxHeight, placeholder, lazy = true, onLoad, style, ...rest }) {
|
|
335
316
|
const frame = useRef(null);
|
|
336
317
|
const near = useNearViewport(frame, lazy);
|
|
@@ -381,7 +362,6 @@ function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, ratio, size
|
|
|
381
362
|
})
|
|
382
363
|
});
|
|
383
364
|
}
|
|
384
|
-
/** The default placeholder: a neutral box, readable on light and dark pages. */
|
|
385
365
|
function Skeleton({ minHeight }) {
|
|
386
366
|
return /* @__PURE__ */ jsx("div", {
|
|
387
367
|
"aria-hidden": "true",
|
|
@@ -394,7 +374,6 @@ function Skeleton({ minHeight }) {
|
|
|
394
374
|
});
|
|
395
375
|
}
|
|
396
376
|
// src/parser/verify.ts
|
|
397
|
-
/** The kind and variant pairs the parser produces for each platform. */
|
|
398
377
|
const SHAPES = {
|
|
399
378
|
x: ["post sdk"],
|
|
400
379
|
instagram: ["post sdk"],
|
|
@@ -408,16 +387,8 @@ const SHAPES = {
|
|
|
408
387
|
youtube: ["video iframe"]
|
|
409
388
|
};
|
|
410
389
|
/**
|
|
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.
|
|
390
|
+
* Security: the embed may be host-built, and its url, id and `fallbackHtml` are written
|
|
391
|
+
* into the page: only what this parser itself produces passes (docs/decisions.md D35).
|
|
421
392
|
*/
|
|
422
393
|
function verifyEmbed(embed) {
|
|
423
394
|
const matched = matchUrl(embed.url);
|
|
@@ -446,7 +417,6 @@ const NAMES = {
|
|
|
446
417
|
tiktok: "TikTok",
|
|
447
418
|
youtube: "YouTube"
|
|
448
419
|
};
|
|
449
|
-
/** The embed for `platform`, or a message saying why the input is not one. */
|
|
450
420
|
function resolveEmbed(url, given, platform) {
|
|
451
421
|
const embed = given ?? (url === void 0 ? null : matchUrl(url));
|
|
452
422
|
if (!embed) return { error: `social-embeds: not a ${NAMES[platform]} post URL: ${String(url)}` };
|
|
@@ -455,17 +425,14 @@ function resolveEmbed(url, given, platform) {
|
|
|
455
425
|
if (!verified) return { error: `social-embeds: the ${NAMES[platform]} embed's url and id do not match` };
|
|
456
426
|
return { embed: verified };
|
|
457
427
|
}
|
|
458
|
-
/**
|
|
428
|
+
/** Later entries replace earlier ones with the same name. */
|
|
459
429
|
function query(...sources) {
|
|
460
430
|
const params = /* @__PURE__ */ new Map();
|
|
461
431
|
for (const source of sources) for (const [key, value] of source ?? []) params.set(key, String(value));
|
|
462
432
|
const pairs = [...params].map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
463
433
|
return pairs.length > 0 ? `?${pairs.join("&")}` : "";
|
|
464
434
|
}
|
|
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
|
-
*/
|
|
435
|
+
/** Player parameters take 0 and 1, so booleans become numbers. */
|
|
469
436
|
function playerParams(props, names) {
|
|
470
437
|
const params = [];
|
|
471
438
|
for (const [prop, name] of Object.entries(names)) {
|
|
@@ -477,11 +444,6 @@ function playerParams(props, names) {
|
|
|
477
444
|
}
|
|
478
445
|
// src/components/facebook-video-iframe.tsx
|
|
479
446
|
const ALLOW$2 = "autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share";
|
|
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
|
-
*/
|
|
485
447
|
function FacebookVideoIframe({ embed, ...frame }) {
|
|
486
448
|
const params = Object.entries(embed.options).filter(([key]) => key !== "href");
|
|
487
449
|
return /* @__PURE__ */ jsx(IframeEmbed, {
|
|
@@ -498,7 +460,7 @@ function FacebookVideoIframe({ embed, ...frame }) {
|
|
|
498
460
|
});
|
|
499
461
|
}
|
|
500
462
|
// src/components/markup.ts
|
|
501
|
-
/** `
|
|
463
|
+
/** `true` becomes a valueless attribute. */
|
|
502
464
|
function attributes(entries) {
|
|
503
465
|
let html = "";
|
|
504
466
|
for (const [name, value] of Object.entries(entries)) {
|
|
@@ -507,10 +469,7 @@ function attributes(entries) {
|
|
|
507
469
|
}
|
|
508
470
|
return html;
|
|
509
471
|
}
|
|
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
|
-
*/
|
|
472
|
+
/** Booleans are written as `"true"` and `"false"`, the way the platforms document them. */
|
|
514
473
|
function dataAttributes(names, options) {
|
|
515
474
|
const entries = {};
|
|
516
475
|
for (const [key, name] of Object.entries(names)) {
|
|
@@ -535,11 +494,6 @@ const RETRY_DELAY_MS = 1e3;
|
|
|
535
494
|
function toError(error) {
|
|
536
495
|
return error instanceof Error ? error : new Error(String(error));
|
|
537
496
|
}
|
|
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
497
|
function ScriptEmbed({ driver, markup, hasFallback, minHeight, label, width = "100%", maxWidth, placeholder, lazy = true, onLoad, onError, style, ...rest }) {
|
|
544
498
|
const container = useRef(null);
|
|
545
499
|
const near = useNearViewport(container, lazy);
|
|
@@ -632,7 +586,6 @@ const ATTRIBUTES$1 = {
|
|
|
632
586
|
autoplay: "data-autoplay",
|
|
633
587
|
showCaptions: "data-show-captions"
|
|
634
588
|
};
|
|
635
|
-
/** The video options under the names `plugins/video.php` reads them by. */
|
|
636
589
|
const PLAYER_PARAMETERS = {
|
|
637
590
|
showText: "show_text",
|
|
638
591
|
allowfullscreen: "allowfullscreen",
|
|
@@ -646,12 +599,6 @@ const MIN_HEIGHT$3 = {
|
|
|
646
599
|
post: 300,
|
|
647
600
|
video: 200
|
|
648
601
|
};
|
|
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
602
|
function FacebookEmbed(props) {
|
|
656
603
|
const { url, embed: given, onError, locale, showText, allowfullscreen, autoplay, showCaptions, height, maxHeight, ...shared } = props;
|
|
657
604
|
const { embed, error } = resolveEmbed(url, given, "facebook");
|
|
@@ -706,11 +653,6 @@ function FacebookEmbed(props) {
|
|
|
706
653
|
// src/components/instagram-embed.tsx
|
|
707
654
|
/** A photo post without caption at 375px, the smallest measured (docs/decisions.md D34). */
|
|
708
655
|
const MIN_HEIGHT$2 = 680;
|
|
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
|
-
*/
|
|
714
656
|
const FLUID_WIDTH = "width:calc(100% - 2px)";
|
|
715
657
|
function InstagramEmbed(props) {
|
|
716
658
|
const { url, embed: given, onError, maxWidth = 540, captioned, ...shared } = props;
|
|
@@ -779,10 +721,7 @@ const PARAMS$1 = {
|
|
|
779
721
|
forceMuted: "force_muted"
|
|
780
722
|
};
|
|
781
723
|
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
|
-
*/
|
|
724
|
+
/** At most 325px wide by default, the size of TikTok's own embed code (docs/decisions.md D40). */
|
|
786
725
|
function TikTokEmbed(props) {
|
|
787
726
|
const { url, embed: given, onError, maxWidth = 325, controls, progressBar, playButton, volumeControl, fullscreenButton, timestamp, loop, autoplay, musicInfo, description, rel, nativeContextMenu, closedCaption, forceMuted, ...frame } = props;
|
|
788
727
|
const { embed, error } = resolveEmbed(url, given, "tiktok");
|
|
@@ -925,10 +864,7 @@ function YouTubeEmbed(props) {
|
|
|
925
864
|
});
|
|
926
865
|
}
|
|
927
866
|
// 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
|
-
*/
|
|
867
|
+
/** `height` and `maxHeight` reach only the iframe-based embeds: the others size themselves. */
|
|
932
868
|
function SocialEmbed(props) {
|
|
933
869
|
const { html, url, fallback, height, maxHeight, onError, ...shared } = props;
|
|
934
870
|
const frame = {
|
|
@@ -28,7 +28,6 @@ const NAMED_ENTITIES = /* @__PURE__ */ new Map([
|
|
|
28
28
|
["apos", "'"],
|
|
29
29
|
["nbsp", "\xA0"]
|
|
30
30
|
]);
|
|
31
|
-
/** Parses an HTML fragment into a node list, or `null` if it is not well-formed. */
|
|
32
31
|
function parseHtml(input) {
|
|
33
32
|
const root = {
|
|
34
33
|
type: "element",
|
|
@@ -124,7 +123,7 @@ function decodeEntities(value) {
|
|
|
124
123
|
function hasClass(element, name) {
|
|
125
124
|
return (element.attrs.get("class") ?? "").split(/\s+/).includes(name);
|
|
126
125
|
}
|
|
127
|
-
/**
|
|
126
|
+
/** In document order. */
|
|
128
127
|
function descendants(nodes) {
|
|
129
128
|
const found = [];
|
|
130
129
|
for (const node of nodes) {
|
|
@@ -136,9 +135,8 @@ function descendants(nodes) {
|
|
|
136
135
|
const URL_ATTRIBUTES = /* @__PURE__ */ new Set(["href", "cite"]);
|
|
137
136
|
const HTTP_URL = /^https?:\/\//i;
|
|
138
137
|
/**
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
* copied as written: the tokenizer guarantees it contains no `<`, so it cannot open a tag.
|
|
138
|
+
* Security: the result reaches `dangerouslySetInnerHTML`. Elements off the list are dropped
|
|
139
|
+
* with their content, `href` and `cite` must be http(s) URLs, and text never holds a `<`.
|
|
142
140
|
*/
|
|
143
141
|
function rebuild(nodes, whitelist) {
|
|
144
142
|
let html = "";
|
|
@@ -169,7 +167,7 @@ function escapeAttribute(value) {
|
|
|
169
167
|
}
|
|
170
168
|
// src/parser/url.ts
|
|
171
169
|
const URL_PATTERN = /^(?:https?:)?\/\/([^/?#\s@]+)((?:\/[^?#\s]*)?)(?:\?([^#\s]*))?(?:#\S*)?$/i;
|
|
172
|
-
/**
|
|
170
|
+
/** Absolute or protocol-relative http(s) URLs only. */
|
|
173
171
|
function parseUrl(input) {
|
|
174
172
|
const match = URL_PATTERN.exec(input.trim());
|
|
175
173
|
if (!match) return null;
|
|
@@ -203,12 +201,11 @@ function coerce(value) {
|
|
|
203
201
|
if (/^(?:0|[1-9]\d{0,14})$/.test(value)) return Number(value);
|
|
204
202
|
return value;
|
|
205
203
|
}
|
|
206
|
-
/** Positive integer pixel size from an attribute or query value. */
|
|
207
204
|
function pixels(value) {
|
|
208
205
|
return value !== void 0 && /^[1-9]\d{0,5}$/.test(value) ? Number(value) : void 0;
|
|
209
206
|
}
|
|
210
207
|
// src/parser/platform.ts
|
|
211
|
-
/**
|
|
208
|
+
/** The first attribute present for a key wins. */
|
|
212
209
|
function attributeOptions(element, mapping, coerceValue) {
|
|
213
210
|
const options = {};
|
|
214
211
|
for (const [attribute, key] of mapping) {
|
|
@@ -423,10 +420,6 @@ const FALLBACK$2 = /* @__PURE__ */ new Map([
|
|
|
423
420
|
["a", ["href"]],
|
|
424
421
|
["p", []]
|
|
425
422
|
]);
|
|
426
|
-
/**
|
|
427
|
-
* embed.js serves only /p/, /reel/ and /tv/ permalinks; /reels/ and /{user}/… return 404
|
|
428
|
-
* (docs/decisions.md D15), so the canonical URL drops the username and maps reels → reel.
|
|
429
|
-
*/
|
|
430
423
|
function post$2(url) {
|
|
431
424
|
if (!url || !HOSTS$3.has(url.host)) return null;
|
|
432
425
|
const [, user, type, code] = POST_PATH$1.exec(url.path) ?? [];
|
|
@@ -520,7 +513,6 @@ const VIDEO_PATH = /^\/@([A-Za-z0-9._]{0,64})\/video\/(\d{1,25})\/?$/;
|
|
|
520
513
|
const PLAYER_PATH = /^\/player\/v1\/(\d{1,25})\/?$/;
|
|
521
514
|
const VIDEO_ID$1 = /^\d{1,25}$/;
|
|
522
515
|
const SCRIPT$1 = /^(?:https?:)?\/\/www\.tiktok\.com\/embed\.js$/;
|
|
523
|
-
/** `user` is unknown for player iframe input; TikTok serves `/@/video/{id}` for that case. */
|
|
524
516
|
function video$1(id, user = "", options = {}) {
|
|
525
517
|
return {
|
|
526
518
|
platform: "tiktok",
|
|
@@ -712,10 +704,7 @@ const platforms = [
|
|
|
712
704
|
}
|
|
713
705
|
];
|
|
714
706
|
// src/parser/match-url.ts
|
|
715
|
-
/**
|
|
716
|
-
* Recognises a post URL. The result has no fallback content, and options only for data the
|
|
717
|
-
* URL itself carries (docs/decisions.md D13). Never throws.
|
|
718
|
-
*/
|
|
707
|
+
/** Never throws. Options hold only what the URL itself carries (docs/decisions.md D13). */
|
|
719
708
|
function matchUrl(url) {
|
|
720
709
|
if (typeof url !== "string") return null;
|
|
721
710
|
const parsed = parseUrl(url);
|
|
@@ -728,10 +717,8 @@ function matchUrl(url) {
|
|
|
728
717
|
}
|
|
729
718
|
// src/parser/parse-embed.ts
|
|
730
719
|
/**
|
|
731
|
-
*
|
|
732
|
-
*
|
|
733
|
-
* `<div id="fb-root">`, whitespace and comments. Anything else returns `null`
|
|
734
|
-
* (docs/decisions.md D14). Never throws.
|
|
720
|
+
* Never throws. `null` unless the input is one embed element plus, at most, the platform's
|
|
721
|
+
* own scripts, `<div id="fb-root">`, whitespace and comments (docs/decisions.md D14).
|
|
735
722
|
*/
|
|
736
723
|
function parseEmbed(html) {
|
|
737
724
|
if (typeof html !== "string") return null;
|
package/dist/parser.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { i as Platform, n as matchUrl, r as ParsedEmbed, t as parseEmbed } from "./index-
|
|
1
|
+
import { i as Platform, n as matchUrl, r as ParsedEmbed, t as parseEmbed } from "./index-DCF8EEVz.js";
|
|
2
2
|
export { type ParsedEmbed, type Platform, matchUrl, parseEmbed };
|
package/dist/parser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { n as matchUrl, t as parseEmbed } from "./parser-
|
|
1
|
+
export { n as matchUrl, t as parseEmbed } from "./parser-BrlUKvvo.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "social-embeds",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Turn social media embed code or post URLs into working React components.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Atanas Mitrev",
|
|
@@ -47,10 +47,10 @@
|
|
|
47
47
|
"publint": "0.3.25",
|
|
48
48
|
"react": "19.3.1",
|
|
49
49
|
"react-dom": "19.3.1",
|
|
50
|
-
"tsdown": "0.23.
|
|
50
|
+
"tsdown": "0.23.7",
|
|
51
51
|
"typescript": "6.0.3",
|
|
52
52
|
"typescript-eslint": "8.72.1",
|
|
53
|
-
"vite": "8.3.
|
|
53
|
+
"vite": "8.3.7",
|
|
54
54
|
"vitest": "5.1.0"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|