social-embeds 0.9.0 → 0.9.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.
- package/README.md +5 -5
- package/dist/components/iframe-embed.js +15 -31
- package/dist/core/drivers.js +6 -6
- package/dist/core/layout.js +1 -45
- package/dist/core/message-height.js +24 -0
- package/dist/core/plans/bluesky.js +3 -1
- package/dist/core/plans/flickr.js +16 -6
- package/dist/core/plans/imgur.js +3 -1
- package/dist/core/plans/reddit.js +3 -1
- package/dist/core/plans/strava.js +3 -1
- package/dist/core/plans/telegram.js +3 -1
- package/dist/core/plans/truthsocial.js +5 -2
- package/dist/core/plans/tumblr.js +3 -1
- package/dist/core/plans/twitch.js +5 -2
- package/dist/core/resolve.js +15 -37
- package/dist/core/size-in-src.js +47 -0
- package/dist/element.js +1 -1
- package/dist/loader/platform-scripts.js +23 -22
- package/dist/parser/html.d.ts +1 -6
- package/dist/parser/html.js +6 -1
- package/dist/parser/lookup.js +11 -5
- package/dist/parser/platform.d.ts +9 -3
- package/dist/parser/platforms/applemusic.js +2 -0
- package/dist/parser/platforms/applepodcasts.js +2 -0
- package/dist/parser/platforms/bluesky.js +6 -2
- package/dist/parser/platforms/dailymotion.js +2 -0
- package/dist/parser/platforms/facebook.js +8 -2
- package/dist/parser/platforms/flickr.js +4 -2
- package/dist/parser/platforms/giphy.js +2 -0
- package/dist/parser/platforms/imgur.js +4 -2
- package/dist/parser/platforms/instagram.js +4 -2
- package/dist/parser/platforms/kick.js +2 -0
- package/dist/parser/platforms/linkedin.js +2 -0
- package/dist/parser/platforms/pinterest.js +2 -0
- package/dist/parser/platforms/reddit.js +2 -0
- package/dist/parser/platforms/rumble.js +4 -0
- package/dist/parser/platforms/snapchat.js +2 -0
- package/dist/parser/platforms/soundcloud.js +2 -0
- package/dist/parser/platforms/spotify.js +2 -0
- package/dist/parser/platforms/strava.js +2 -0
- package/dist/parser/platforms/streamable.js +2 -0
- package/dist/parser/platforms/telegram.js +2 -0
- package/dist/parser/platforms/tenor.js +4 -2
- package/dist/parser/platforms/threads.js +4 -2
- package/dist/parser/platforms/tiktok.js +2 -0
- package/dist/parser/platforms/truthsocial.js +2 -0
- package/dist/parser/platforms/tumblr.js +2 -0
- package/dist/parser/platforms/twitch.js +2 -0
- package/dist/parser/platforms/vimeo.js +2 -0
- package/dist/parser/platforms/x.js +8 -2
- package/dist/parser/platforms/youtube.js +2 -0
- package/dist/parser/verify.js +2 -46
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -107,10 +107,10 @@ export const SocialEmbed = createSocialEmbed([
|
|
|
107
107
|
|
|
108
108
|
| Platforms | Gzipped |
|
|
109
109
|
| ------------------------------------------------- | ------- |
|
|
110
|
-
| `youtube` |
|
|
111
|
-
| `youtube`, `x` |
|
|
112
|
-
| `youtube`, `x`, `instagram`, `facebook`, `tiktok` |
|
|
113
|
-
| the same five and `genericIframe` |
|
|
110
|
+
| `youtube` | 6 KB |
|
|
111
|
+
| `youtube`, `x` | 9 KB |
|
|
112
|
+
| `youtube`, `x`, `instagram`, `facebook`, `tiktok` | 12 KB |
|
|
113
|
+
| the same five and `genericIframe` | 14 KB |
|
|
114
114
|
| every platform (`<SocialEmbed>`) | 28 KB |
|
|
115
115
|
|
|
116
116
|
- **Names:** each platform is exported under its id, as in `embed.platform`: `x`, `instagram`, `threads`, `facebook`, `tiktok`, `youtube`, `telegram`, `snapchat`, `linkedin`, `reddit`, `pinterest`, `spotify`, `vimeo`, `twitch`, `dailymotion`, `bluesky`, `soundcloud`, `applemusic`, `applepodcasts`, `tumblr`, `kick`, `giphy`, `truthsocial`, `imgur`, `tenor`, `streamable`, `strava`, `flickr` and `rumble`. `genericIframe` adds [generic iframe embeds](#generic-iframe-embeds). The order of the list does not matter.
|
|
@@ -121,7 +121,7 @@ export const SocialEmbed = createSocialEmbed([
|
|
|
121
121
|
|
|
122
122
|
Each platform component takes a `url`, or an `embed` already parsed by [`social-embeds/parser`](#parser), plus the shared props and its own option props. Option props override options from the embed code.
|
|
123
123
|
|
|
124
|
-
A platform component bundles only its own platform's parser: one component adds about 5 to
|
|
124
|
+
A platform component bundles only its own platform's parser: one component adds about 3.5 to 5.5 KB gzipped to an app (`FacebookEmbed` about 7.4 KB), against about 28 KB for `<SocialEmbed>`, which recognises every platform. A URL of another platform is reported as `social-embeds: not a {Platform} post URL: …`, and an `embed` of another platform as `social-embeds: expected a {Platform} embed, got {id}`.
|
|
125
125
|
|
|
126
126
|
```tsx
|
|
127
127
|
import {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FRAME_TIMEOUT_MS } from "../core/timing.js";
|
|
2
|
-
import { frameStyle
|
|
2
|
+
import { frameStyle } from "../core/layout.js";
|
|
3
3
|
import { useNearViewport } from "./hooks.js";
|
|
4
4
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
5
5
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
@@ -25,7 +25,7 @@ const FILL = {
|
|
|
25
25
|
function IframeEmbed(props) {
|
|
26
26
|
return /* @__PURE__ */ jsx(Frame, { ...props }, props.src);
|
|
27
27
|
}
|
|
28
|
-
function Frame({ src, title, allow, allowFullscreen = true, referrerPolicy, scrolling, sandbox, ratio, size, messageHeight, startHeight,
|
|
28
|
+
function Frame({ src, title, allow, allowFullscreen = true, referrerPolicy, scrolling, sandbox, ratio, size, messageHeight, startHeight, clientSrc, borderRadius, sizeInSrc, width, maxWidth, height, maxHeight, placeholder, lazy = true, onLoad, onError, style, ...rest }) {
|
|
29
29
|
const frame = useRef(null);
|
|
30
30
|
const iframe = useRef(null);
|
|
31
31
|
const near = useNearViewport(frame, lazy);
|
|
@@ -52,34 +52,25 @@ function Frame({ src, title, allow, allowFullscreen = true, referrerPolicy, scro
|
|
|
52
52
|
}, []);
|
|
53
53
|
useEffect(() => {
|
|
54
54
|
if (!near || !messageHeight) return;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (event.origin !== origin) return;
|
|
58
|
-
if (!iframe.current || event.source !== iframe.current.contentWindow) return;
|
|
59
|
-
let value;
|
|
60
|
-
try {
|
|
61
|
-
value = read(event.data);
|
|
62
|
-
} catch {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (typeof value !== "number" || !(value > 0 && value <= 5e4)) return;
|
|
66
|
-
setReportedHeight(Math.ceil(value));
|
|
55
|
+
return messageHeight.listen(() => iframe.current, (value) => {
|
|
56
|
+
setReportedHeight(value);
|
|
67
57
|
report();
|
|
68
|
-
};
|
|
69
|
-
window.addEventListener("message", listen);
|
|
70
|
-
return () => {
|
|
71
|
-
window.removeEventListener("message", listen);
|
|
72
|
-
};
|
|
58
|
+
});
|
|
73
59
|
}, [
|
|
74
60
|
near,
|
|
75
61
|
messageHeight,
|
|
76
62
|
report
|
|
77
63
|
]);
|
|
64
|
+
const sizing = useRef(sizeInSrc);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
sizing.current = sizeInSrc;
|
|
67
|
+
});
|
|
68
|
+
const sized = sizeInSrc !== void 0;
|
|
78
69
|
useEffect(() => {
|
|
79
70
|
const element = frame.current;
|
|
80
|
-
if (!near || !
|
|
81
|
-
return
|
|
82
|
-
}, [near,
|
|
71
|
+
if (!near || !sized || !element) return;
|
|
72
|
+
return sizing.current?.watch(element, setBoxWidth);
|
|
73
|
+
}, [near, sized]);
|
|
83
74
|
useEffect(() => {
|
|
84
75
|
if (!near || !inView) return;
|
|
85
76
|
const failure = messageHeight ? "did not report its height" : "did not load";
|
|
@@ -116,11 +107,7 @@ function Frame({ src, title, allow, allowFullscreen = true, referrerPolicy, scro
|
|
|
116
107
|
}, reportedHeight),
|
|
117
108
|
...style
|
|
118
109
|
};
|
|
119
|
-
const frameSrc = !sizeInSrc ? src : boxWidth === void 0 ? void 0 :
|
|
120
|
-
src,
|
|
121
|
-
ratio,
|
|
122
|
-
size
|
|
123
|
-
}, boxWidth);
|
|
110
|
+
const frameSrc = !sizeInSrc ? src : boxWidth === void 0 ? void 0 : sizeInSrc.src(boxWidth);
|
|
124
111
|
return /* @__PURE__ */ jsx("div", {
|
|
125
112
|
...rest,
|
|
126
113
|
style: wrapper,
|
|
@@ -129,7 +116,7 @@ function Frame({ src, title, allow, allowFullscreen = true, referrerPolicy, scro
|
|
|
129
116
|
style: FILL,
|
|
130
117
|
children: [near && frameSrc !== void 0 && /* @__PURE__ */ jsx("iframe", {
|
|
131
118
|
ref: iframe,
|
|
132
|
-
src:
|
|
119
|
+
src: clientSrc ? clientSrc(frameSrc) : frameSrc,
|
|
133
120
|
title,
|
|
134
121
|
allow,
|
|
135
122
|
allowFullScreen: allowFullscreen,
|
|
@@ -152,9 +139,6 @@ function Frame({ src, title, allow, allowFullscreen = true, referrerPolicy, scro
|
|
|
152
139
|
})
|
|
153
140
|
});
|
|
154
141
|
}
|
|
155
|
-
function withHost(src, name) {
|
|
156
|
-
return `${src}${src.includes("?") ? "&" : "?"}${encodeURIComponent(name)}=${encodeURIComponent(window.location.hostname)}`;
|
|
157
|
-
}
|
|
158
142
|
function Skeleton({ minHeight }) {
|
|
159
143
|
return /* @__PURE__ */ jsx("div", {
|
|
160
144
|
"aria-hidden": "true",
|
package/dist/core/drivers.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FACEBOOK_SDK_VERSION,
|
|
1
|
+
import { FACEBOOK_SDK_VERSION, loadFacebook, loadInstagram, loadPinterest, loadThreads, loadX } from "../loader/platform-scripts.js";
|
|
2
2
|
// src/core/drivers.ts
|
|
3
3
|
const globals = () => window;
|
|
4
4
|
/** Thrown by `render` when another attempt cannot succeed. */
|
|
@@ -35,7 +35,7 @@ function hasSizedIframe(container) {
|
|
|
35
35
|
return [...container.querySelectorAll("iframe")].some((iframe) => Number.parseFloat(iframe.style.height) > 0);
|
|
36
36
|
}
|
|
37
37
|
const xDriver = {
|
|
38
|
-
load: () =>
|
|
38
|
+
load: () => loadX(),
|
|
39
39
|
render(container, done) {
|
|
40
40
|
const twttr = globals().twttr;
|
|
41
41
|
const onRendered = (event) => {
|
|
@@ -61,7 +61,7 @@ function processEmbeds() {
|
|
|
61
61
|
process();
|
|
62
62
|
}
|
|
63
63
|
const instagramDriver = {
|
|
64
|
-
load: () =>
|
|
64
|
+
load: () => loadInstagram(),
|
|
65
65
|
render(container, done) {
|
|
66
66
|
inDocument(container, "Instagram");
|
|
67
67
|
const stop = watch(container, (element) => element.querySelector("iframe.instagram-media-rendered") !== null, done);
|
|
@@ -71,7 +71,7 @@ const instagramDriver = {
|
|
|
71
71
|
}
|
|
72
72
|
};
|
|
73
73
|
const threadsDriver = {
|
|
74
|
-
load: () =>
|
|
74
|
+
load: () => loadThreads(),
|
|
75
75
|
render(container, done) {
|
|
76
76
|
inDocument(container, "Threads");
|
|
77
77
|
const stop = watch(container, (element) => element.querySelector("iframe") !== null && element.querySelector("blockquote") === null, done);
|
|
@@ -85,7 +85,7 @@ function facebookDriver(locale) {
|
|
|
85
85
|
let driver = facebookDrivers.get(locale);
|
|
86
86
|
if (!driver) {
|
|
87
87
|
driver = {
|
|
88
|
-
load: () =>
|
|
88
|
+
load: () => loadFacebook(locale),
|
|
89
89
|
render(container, done) {
|
|
90
90
|
const fb = globals().FB;
|
|
91
91
|
const parse = fb?.XFBML?.parse;
|
|
@@ -111,7 +111,7 @@ function facebookDriver(locale) {
|
|
|
111
111
|
return driver;
|
|
112
112
|
}
|
|
113
113
|
const pinterestDriver = {
|
|
114
|
-
load: () =>
|
|
114
|
+
load: () => loadPinterest(),
|
|
115
115
|
render(container, done) {
|
|
116
116
|
const build = globals().PinUtils?.build;
|
|
117
117
|
if (!build) throw new Error("social-embeds: PinUtils.build is missing");
|
package/dist/core/layout.js
CHANGED
|
@@ -26,48 +26,4 @@ function frameStyle(plan, layout, reportedHeight) {
|
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
function srcWithSize(plan, width) {
|
|
31
|
-
const aspect = aspectOf(plan);
|
|
32
|
-
const height = Math.round(width * aspect.height / aspect.width);
|
|
33
|
-
const query = plan.src.indexOf("?");
|
|
34
|
-
const path = query === -1 ? plan.src : plan.src.slice(0, query);
|
|
35
|
-
const rest = query === -1 ? "" : `&${plan.src.slice(query + 1)}`;
|
|
36
|
-
return `${path}?width=${String(width)}&height=${String(height)}${rest}`;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Calls `onWidth` with the element's rounded width once it has one, then with each new width
|
|
40
|
-
* that has held for WIDTH_SETTLE_MS, so a resize rebuilds the frame once.
|
|
41
|
-
*/
|
|
42
|
-
function watchWidth(element, onWidth) {
|
|
43
|
-
let last;
|
|
44
|
-
let timer;
|
|
45
|
-
const take = (width) => {
|
|
46
|
-
const rounded = Math.round(width);
|
|
47
|
-
if (rounded <= 0) return;
|
|
48
|
-
clearTimeout(timer);
|
|
49
|
-
if (rounded === last) return;
|
|
50
|
-
if (last === void 0) {
|
|
51
|
-
last = rounded;
|
|
52
|
-
onWidth(rounded);
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
timer = setTimeout(() => {
|
|
56
|
-
last = rounded;
|
|
57
|
-
onWidth(rounded);
|
|
58
|
-
}, 200);
|
|
59
|
-
};
|
|
60
|
-
if (typeof ResizeObserver === "undefined") {
|
|
61
|
-
take(element.getBoundingClientRect().width);
|
|
62
|
-
return () => {};
|
|
63
|
-
}
|
|
64
|
-
const observer = new ResizeObserver((entries) => {
|
|
65
|
-
for (const entry of entries) take(entry.contentRect.width);
|
|
66
|
-
});
|
|
67
|
-
observer.observe(element);
|
|
68
|
-
return () => {
|
|
69
|
-
observer.disconnect();
|
|
70
|
-
clearTimeout(timer);
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
export { frameStyle, srcWithSize, watchWidth };
|
|
29
|
+
export { aspectOf, frameStyle };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import "./timing.js";
|
|
2
|
+
// src/core/message-height.ts
|
|
3
|
+
/** `MessageHeight.listen`: the heights the frame itself posts, from the platform's origin only. */
|
|
4
|
+
function listenForHeight(frame, onHeight) {
|
|
5
|
+
const { origin, read } = this;
|
|
6
|
+
const listen = (event) => {
|
|
7
|
+
if (event.origin !== origin) return;
|
|
8
|
+
const element = frame();
|
|
9
|
+
if (!element || event.source !== element.contentWindow) return;
|
|
10
|
+
let value;
|
|
11
|
+
try {
|
|
12
|
+
value = read(event.data);
|
|
13
|
+
} catch {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (typeof value !== "number" || !(value > 0 && value <= 5e4)) return;
|
|
17
|
+
onHeight(Math.ceil(value));
|
|
18
|
+
};
|
|
19
|
+
window.addEventListener("message", listen);
|
|
20
|
+
return () => {
|
|
21
|
+
window.removeEventListener("message", listen);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export { listenForHeight };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { query } from "../params.js";
|
|
2
|
+
import { listenForHeight } from "../message-height.js";
|
|
2
3
|
// src/core/plans/bluesky.ts
|
|
3
4
|
/** The frame posts `{ height }` once it has painted (docs/decisions.md D70). */
|
|
4
5
|
const BLUESKY_HEIGHT = {
|
|
@@ -6,7 +7,8 @@ const BLUESKY_HEIGHT = {
|
|
|
6
7
|
read(data) {
|
|
7
8
|
const height = data?.height;
|
|
8
9
|
return typeof height === "number" ? height : void 0;
|
|
9
|
-
}
|
|
10
|
+
},
|
|
11
|
+
listen: listenForHeight
|
|
10
12
|
};
|
|
11
13
|
/**
|
|
12
14
|
* At most 600px wide by default, Bluesky's oEmbed width. Renders the frame `embed.js` would
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { srcWithSize, watchWidth } from "../size-in-src.js";
|
|
1
2
|
// src/core/plans/flickr.ts
|
|
2
3
|
const OPTIONS = [
|
|
3
4
|
"header",
|
|
@@ -15,21 +16,30 @@ function flickrPlan(embed, options) {
|
|
|
15
16
|
width,
|
|
16
17
|
height
|
|
17
18
|
} : void 0;
|
|
19
|
+
const src = `https://embedr.flickr.com/${album ? "photosets" : "photos"}/${embed.id}${kept.length > 0 ? `?${kept.map((name) => `${name}=true`).join("&")}` : ""}`;
|
|
20
|
+
const ratio = {
|
|
21
|
+
width: 4,
|
|
22
|
+
height: 3
|
|
23
|
+
};
|
|
18
24
|
return {
|
|
19
25
|
kind: "iframe",
|
|
20
|
-
src
|
|
26
|
+
src,
|
|
21
27
|
title: album ? "Flickr album" : "Flickr photo",
|
|
22
28
|
allow: "",
|
|
23
29
|
scrolling: "no",
|
|
24
|
-
ratio
|
|
25
|
-
width: 4,
|
|
26
|
-
height: 3
|
|
27
|
-
},
|
|
30
|
+
ratio,
|
|
28
31
|
...size ? {
|
|
29
32
|
size,
|
|
30
33
|
maxWidth: size.width
|
|
31
34
|
} : {},
|
|
32
|
-
sizeInSrc:
|
|
35
|
+
sizeInSrc: {
|
|
36
|
+
watch: watchWidth,
|
|
37
|
+
src: (width) => srcWithSize({
|
|
38
|
+
src,
|
|
39
|
+
ratio,
|
|
40
|
+
size
|
|
41
|
+
}, width)
|
|
42
|
+
},
|
|
33
43
|
...embed.fallbackHtml === void 0 ? {} : { fallbackHtml: embed.fallbackHtml.replace(/<img(?=[\s>])/g, `<img style="${IMAGE_STYLE}"`) },
|
|
34
44
|
fallbackImage: true
|
|
35
45
|
};
|
package/dist/core/plans/imgur.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { listenForHeight } from "../message-height.js";
|
|
1
2
|
// src/core/plans/imgur.ts
|
|
2
3
|
/** The frame posts one JSON string at `load`, unasked (docs/decisions.md D94). */
|
|
3
4
|
const IMGUR_HEIGHT = {
|
|
@@ -6,7 +7,8 @@ const IMGUR_HEIGHT = {
|
|
|
6
7
|
if (typeof data !== "string") return void 0;
|
|
7
8
|
const message = JSON.parse(data);
|
|
8
9
|
return message.message === "resize_imgur" && typeof message.height === "number" ? message.height : void 0;
|
|
9
|
-
}
|
|
10
|
+
},
|
|
11
|
+
listen: listenForHeight
|
|
10
12
|
};
|
|
11
13
|
/** At most 540px wide by default, the widest `embed.js` makes the frame (docs/decisions.md D94). */
|
|
12
14
|
function imgurPlan(embed, options) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { query } from "../params.js";
|
|
2
|
+
import { listenForHeight } from "../message-height.js";
|
|
2
3
|
// src/core/plans/reddit.ts
|
|
3
4
|
const PARAMS = {
|
|
4
5
|
theme: "theme",
|
|
@@ -18,7 +19,8 @@ const REDDIT_HEIGHT = {
|
|
|
18
19
|
if (typeof data !== "string") return void 0;
|
|
19
20
|
const message = JSON.parse(data);
|
|
20
21
|
return message.type === "resize.embed" && typeof message.data === "number" ? message.data : void 0;
|
|
21
|
-
}
|
|
22
|
+
},
|
|
23
|
+
listen: listenForHeight
|
|
22
24
|
};
|
|
23
25
|
/** At most 640px wide by default, the width widgets.js gives the frame (docs/decisions.md D52). */
|
|
24
26
|
function redditPlan(embed, options) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { listenForHeight } from "../message-height.js";
|
|
1
2
|
// src/core/plans/strava.ts
|
|
2
3
|
const NS = "social-embeds";
|
|
3
4
|
/** The frame posts `[ns, 'BROADCAST_IFRAME_HEIGHT', height]` once, unasked (docs/decisions.md D97). */
|
|
@@ -7,7 +8,8 @@ const STRAVA_HEIGHT = {
|
|
|
7
8
|
if (!Array.isArray(data)) return void 0;
|
|
8
9
|
const [, type, height] = data;
|
|
9
10
|
return type === "BROADCAST_IFRAME_HEIGHT" && typeof height === "number" ? height : void 0;
|
|
10
|
-
}
|
|
11
|
+
},
|
|
12
|
+
listen: listenForHeight
|
|
11
13
|
};
|
|
12
14
|
function styleOf(value) {
|
|
13
15
|
return value === "minimal" ? "minimal" : "standard";
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { query } from "../params.js";
|
|
2
|
+
import { listenForHeight } from "../message-height.js";
|
|
2
3
|
// src/core/plans/telegram.ts
|
|
3
4
|
const PARAMS = [
|
|
4
5
|
["comment", "comment"],
|
|
@@ -24,7 +25,8 @@ const TELEGRAM_HEIGHT = {
|
|
|
24
25
|
if (typeof data !== "string") return void 0;
|
|
25
26
|
const message = JSON.parse(data);
|
|
26
27
|
return message.event === "resize" && typeof message.height === "number" ? message.height : void 0;
|
|
27
|
-
}
|
|
28
|
+
},
|
|
29
|
+
listen: listenForHeight
|
|
28
30
|
};
|
|
29
31
|
/** At most 500px wide by default, the widest the post renders (docs/decisions.md D49). */
|
|
30
32
|
function telegramPlan(embed, options) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { listenForHeight } from "../message-height.js";
|
|
1
2
|
// src/core/plans/truthsocial.ts
|
|
2
3
|
const VIDEO_EXTRA = 165;
|
|
3
4
|
function setHeight(data) {
|
|
@@ -7,7 +8,8 @@ function setHeight(data) {
|
|
|
7
8
|
/** The frame posts `{ type: 'setHeight', id, height }` without being asked (docs/decisions.md D93). */
|
|
8
9
|
const TRUTH_SOCIAL_HEIGHT = {
|
|
9
10
|
origin: "https://truthsocial.com",
|
|
10
|
-
read: setHeight
|
|
11
|
+
read: setHeight,
|
|
12
|
+
listen: listenForHeight
|
|
11
13
|
};
|
|
12
14
|
/** A video post reports its height before the video is sized (docs/decisions.md D100). */
|
|
13
15
|
const TRUTH_SOCIAL_VIDEO_HEIGHT = {
|
|
@@ -15,7 +17,8 @@ const TRUTH_SOCIAL_VIDEO_HEIGHT = {
|
|
|
15
17
|
read(data) {
|
|
16
18
|
const height = setHeight(data);
|
|
17
19
|
return height === void 0 || height <= 0 ? void 0 : height + VIDEO_EXTRA;
|
|
18
|
-
}
|
|
20
|
+
},
|
|
21
|
+
listen: listenForHeight
|
|
19
22
|
};
|
|
20
23
|
const CANONICAL = /^https:\/\/truthsocial\.com\/(@[^/]+)\/posts\/\d+$/;
|
|
21
24
|
/** At most 600px wide by default, the width of Truth Social's embed code (docs/decisions.md D93). */
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { listenForHeight } from "../message-height.js";
|
|
1
2
|
// src/core/plans/tumblr.ts
|
|
2
3
|
/** The frame posts a JSON string once it has painted (docs/decisions.md D74). */
|
|
3
4
|
const TUMBLR_HEIGHT = {
|
|
@@ -7,7 +8,8 @@ const TUMBLR_HEIGHT = {
|
|
|
7
8
|
const message = JSON.parse(data);
|
|
8
9
|
const [height] = Array.isArray(message.args) ? message.args : [];
|
|
9
10
|
return message.method === "tumblr-post:sizeChange" && typeof height === "number" ? height : void 0;
|
|
10
|
-
}
|
|
11
|
+
},
|
|
12
|
+
listen: listenForHeight
|
|
11
13
|
};
|
|
12
14
|
/** At most 540px wide by default, Tumblr's oEmbed width. Renders the frame `post.js` would create (docs/decisions.md D77). */
|
|
13
15
|
function tumblrPlan(embed) {
|
|
@@ -32,7 +32,10 @@ function twitchPlan(embed, options) {
|
|
|
32
32
|
height: 9
|
|
33
33
|
},
|
|
34
34
|
size: embed.size,
|
|
35
|
-
|
|
35
|
+
clientSrc: withParent
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
|
-
|
|
38
|
+
function withParent(src) {
|
|
39
|
+
return `${src}${src.includes("?") ? "&" : "?"}parent=${encodeURIComponent(window.location.hostname)}`;
|
|
40
|
+
}
|
|
41
|
+
export { twitchPlan, withParent };
|
package/dist/core/resolve.js
CHANGED
|
@@ -1,57 +1,35 @@
|
|
|
1
1
|
import { needsLookup } from "../parser/lookup.js";
|
|
2
2
|
import { matchWith } from "../parser/match-url.js";
|
|
3
|
+
import { parseUrl } from "../parser/url.js";
|
|
3
4
|
import { verifyWith } from "../parser/verify.js";
|
|
4
5
|
// src/core/resolve.ts
|
|
5
|
-
const
|
|
6
|
-
x: "X",
|
|
7
|
-
instagram: "Instagram",
|
|
8
|
-
facebook: "Facebook",
|
|
9
|
-
threads: "Threads",
|
|
10
|
-
tiktok: "TikTok",
|
|
11
|
-
youtube: "YouTube",
|
|
12
|
-
telegram: "Telegram",
|
|
13
|
-
snapchat: "Snapchat",
|
|
14
|
-
linkedin: "LinkedIn",
|
|
15
|
-
reddit: "Reddit",
|
|
16
|
-
pinterest: "Pinterest",
|
|
17
|
-
spotify: "Spotify",
|
|
18
|
-
vimeo: "Vimeo",
|
|
19
|
-
twitch: "Twitch",
|
|
20
|
-
dailymotion: "Dailymotion",
|
|
6
|
+
const LOOKUP_NAMES = {
|
|
21
7
|
bluesky: "Bluesky",
|
|
22
|
-
soundcloud: "SoundCloud",
|
|
23
|
-
applemusic: "Apple Music",
|
|
24
|
-
applepodcasts: "Apple Podcasts",
|
|
25
|
-
tumblr: "Tumblr",
|
|
26
|
-
kick: "Kick",
|
|
27
|
-
giphy: "Giphy",
|
|
28
|
-
truthsocial: "Truth Social",
|
|
29
|
-
imgur: "Imgur",
|
|
30
|
-
tenor: "Tenor",
|
|
31
|
-
streamable: "Streamable",
|
|
32
|
-
strava: "Strava",
|
|
33
|
-
flickr: "Flickr",
|
|
34
8
|
rumble: "Rumble"
|
|
35
9
|
};
|
|
36
10
|
/** For a post URL that only a network lookup could resolve (docs/decisions.md D70). */
|
|
37
|
-
function lookupMessage(
|
|
38
|
-
return `social-embeds: a ${
|
|
11
|
+
function lookupMessage(name) {
|
|
12
|
+
return `social-embeds: a ${name} URL needs a lookup: use the embed code`;
|
|
39
13
|
}
|
|
40
14
|
/** Takes the platform's own parser, so a per-platform component bundles no other (docs/decisions.md D103). */
|
|
41
15
|
function resolveEmbed(url, given, parser) {
|
|
42
|
-
const { platform } = parser;
|
|
16
|
+
const { platform, name } = parser;
|
|
43
17
|
const embed = given ?? (url === void 0 ? null : matchWith(parser, url));
|
|
44
|
-
if (!embed && url !== void 0 &&
|
|
45
|
-
if (!embed) return { error: `social-embeds: not a ${
|
|
46
|
-
if (embed.platform !== platform) return { error: `social-embeds: expected a ${
|
|
18
|
+
if (!embed && url !== void 0 && lookupNeeded(parser, url)) return { error: lookupMessage(name) };
|
|
19
|
+
if (!embed) return { error: `social-embeds: not a ${name} post URL: ${String(url)}` };
|
|
20
|
+
if (embed.platform !== platform) return { error: `social-embeds: expected a ${name} embed, got ${embed.platform}` };
|
|
47
21
|
const verified = verifyWith(parser, embed);
|
|
48
|
-
if (!verified) return { error: `social-embeds: the ${
|
|
22
|
+
if (!verified) return { error: `social-embeds: the ${name} embed's url and id do not match` };
|
|
49
23
|
return { embed: verified };
|
|
50
24
|
}
|
|
51
25
|
/** The message `<SocialEmbed>` reports when its input matches no platform. */
|
|
52
26
|
function sourceError(source) {
|
|
53
27
|
if ("html" in source) return "social-embeds: unrecognised embed code";
|
|
54
28
|
const lookup = needsLookup(source.url);
|
|
55
|
-
return lookup ? lookupMessage(lookup) : `social-embeds: not a supported post URL: ${source.url}`;
|
|
29
|
+
return lookup ? lookupMessage(LOOKUP_NAMES[lookup] ?? lookup) : `social-embeds: not a supported post URL: ${source.url}`;
|
|
56
30
|
}
|
|
57
|
-
|
|
31
|
+
function lookupNeeded(parser, url) {
|
|
32
|
+
const parsed = typeof url === "string" ? parseUrl(url) : null;
|
|
33
|
+
return parsed !== null && parser.needsLookup?.(parsed) === true;
|
|
34
|
+
}
|
|
35
|
+
export { lookupMessage, resolveEmbed, sourceError };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { aspectOf } from "./layout.js";
|
|
2
|
+
// src/core/size-in-src.ts
|
|
3
|
+
/** `src` in a box `width` pixels wide. */
|
|
4
|
+
function srcWithSize(plan, width) {
|
|
5
|
+
const aspect = aspectOf(plan);
|
|
6
|
+
const height = Math.round(width * aspect.height / aspect.width);
|
|
7
|
+
const query = plan.src.indexOf("?");
|
|
8
|
+
const path = query === -1 ? plan.src : plan.src.slice(0, query);
|
|
9
|
+
const rest = query === -1 ? "" : `&${plan.src.slice(query + 1)}`;
|
|
10
|
+
return `${path}?width=${String(width)}&height=${String(height)}${rest}`;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Calls `onWidth` with the element's rounded width once it has one, then with each new width
|
|
14
|
+
* that has held for WIDTH_SETTLE_MS, so a resize rebuilds the frame once.
|
|
15
|
+
*/
|
|
16
|
+
function watchWidth(element, onWidth) {
|
|
17
|
+
let last;
|
|
18
|
+
let timer;
|
|
19
|
+
const take = (width) => {
|
|
20
|
+
const rounded = Math.round(width);
|
|
21
|
+
if (rounded <= 0) return;
|
|
22
|
+
clearTimeout(timer);
|
|
23
|
+
if (rounded === last) return;
|
|
24
|
+
if (last === void 0) {
|
|
25
|
+
last = rounded;
|
|
26
|
+
onWidth(rounded);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
timer = setTimeout(() => {
|
|
30
|
+
last = rounded;
|
|
31
|
+
onWidth(rounded);
|
|
32
|
+
}, 200);
|
|
33
|
+
};
|
|
34
|
+
if (typeof ResizeObserver === "undefined") {
|
|
35
|
+
take(element.getBoundingClientRect().width);
|
|
36
|
+
return () => {};
|
|
37
|
+
}
|
|
38
|
+
const observer = new ResizeObserver((entries) => {
|
|
39
|
+
for (const entry of entries) take(entry.contentRect.width);
|
|
40
|
+
});
|
|
41
|
+
observer.observe(element);
|
|
42
|
+
return () => {
|
|
43
|
+
observer.disconnect();
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export { srcWithSize, watchWidth };
|