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.
Files changed (53) hide show
  1. package/README.md +5 -5
  2. package/dist/components/iframe-embed.js +15 -31
  3. package/dist/core/drivers.js +6 -6
  4. package/dist/core/layout.js +1 -45
  5. package/dist/core/message-height.js +24 -0
  6. package/dist/core/plans/bluesky.js +3 -1
  7. package/dist/core/plans/flickr.js +16 -6
  8. package/dist/core/plans/imgur.js +3 -1
  9. package/dist/core/plans/reddit.js +3 -1
  10. package/dist/core/plans/strava.js +3 -1
  11. package/dist/core/plans/telegram.js +3 -1
  12. package/dist/core/plans/truthsocial.js +5 -2
  13. package/dist/core/plans/tumblr.js +3 -1
  14. package/dist/core/plans/twitch.js +5 -2
  15. package/dist/core/resolve.js +15 -37
  16. package/dist/core/size-in-src.js +47 -0
  17. package/dist/element.js +1 -1
  18. package/dist/loader/platform-scripts.js +23 -22
  19. package/dist/parser/html.d.ts +1 -6
  20. package/dist/parser/html.js +6 -1
  21. package/dist/parser/lookup.js +11 -5
  22. package/dist/parser/platform.d.ts +9 -3
  23. package/dist/parser/platforms/applemusic.js +2 -0
  24. package/dist/parser/platforms/applepodcasts.js +2 -0
  25. package/dist/parser/platforms/bluesky.js +6 -2
  26. package/dist/parser/platforms/dailymotion.js +2 -0
  27. package/dist/parser/platforms/facebook.js +8 -2
  28. package/dist/parser/platforms/flickr.js +4 -2
  29. package/dist/parser/platforms/giphy.js +2 -0
  30. package/dist/parser/platforms/imgur.js +4 -2
  31. package/dist/parser/platforms/instagram.js +4 -2
  32. package/dist/parser/platforms/kick.js +2 -0
  33. package/dist/parser/platforms/linkedin.js +2 -0
  34. package/dist/parser/platforms/pinterest.js +2 -0
  35. package/dist/parser/platforms/reddit.js +2 -0
  36. package/dist/parser/platforms/rumble.js +4 -0
  37. package/dist/parser/platforms/snapchat.js +2 -0
  38. package/dist/parser/platforms/soundcloud.js +2 -0
  39. package/dist/parser/platforms/spotify.js +2 -0
  40. package/dist/parser/platforms/strava.js +2 -0
  41. package/dist/parser/platforms/streamable.js +2 -0
  42. package/dist/parser/platforms/telegram.js +2 -0
  43. package/dist/parser/platforms/tenor.js +4 -2
  44. package/dist/parser/platforms/threads.js +4 -2
  45. package/dist/parser/platforms/tiktok.js +2 -0
  46. package/dist/parser/platforms/truthsocial.js +2 -0
  47. package/dist/parser/platforms/tumblr.js +2 -0
  48. package/dist/parser/platforms/twitch.js +2 -0
  49. package/dist/parser/platforms/vimeo.js +2 -0
  50. package/dist/parser/platforms/x.js +8 -2
  51. package/dist/parser/platforms/youtube.js +2 -0
  52. package/dist/parser/verify.js +2 -46
  53. 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` | 7 KB |
111
- | `youtube`, `x` | 10 KB |
112
- | `youtube`, `x`, `instagram`, `facebook`, `tiktok` | 13 KB |
113
- | the same five and `genericIframe` | 15 KB |
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 6 KB gzipped to an app (`FacebookEmbed` about 9 KB, `GenericIframeEmbed` about 5 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: …`.
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, srcWithSize, watchWidth } from "../core/layout.js";
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, hostParam, borderRadius, sizeInSrc, width, maxWidth, height, maxHeight, placeholder, lazy = true, onLoad, onError, style, ...rest }) {
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
- const { origin, read } = messageHeight;
56
- const listen = (event) => {
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 || !sizeInSrc || !element) return;
81
- return watchWidth(element, setBoxWidth);
82
- }, [near, sizeInSrc]);
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 : srcWithSize({
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: hostParam ? withHost(frameSrc, hostParam) : frameSrc,
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",
@@ -1,4 +1,4 @@
1
- import { FACEBOOK_SDK_VERSION, loadPlatformScript } from "../loader/platform-scripts.js";
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: () => loadPlatformScript("x"),
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: () => loadPlatformScript("instagram"),
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: () => loadPlatformScript("threads"),
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: () => loadPlatformScript("facebook", { locale }),
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: () => loadPlatformScript("pinterest"),
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");
@@ -26,48 +26,4 @@ function frameStyle(plan, layout, reportedHeight) {
26
26
  }
27
27
  };
28
28
  }
29
- /** `src` for a plan with `sizeInSrc`, in a box `width` pixels wide. */
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: `https://embedr.flickr.com/${album ? "photosets" : "photos"}/${embed.id}${kept.length > 0 ? `?${kept.map((name) => `${name}=true`).join("&")}` : ""}`,
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: true,
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
  };
@@ -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
- hostParam: "parent"
35
+ clientSrc: withParent
36
36
  };
37
37
  }
38
- export { twitchPlan };
38
+ function withParent(src) {
39
+ return `${src}${src.includes("?") ? "&" : "?"}parent=${encodeURIComponent(window.location.hostname)}`;
40
+ }
41
+ export { twitchPlan, withParent };
@@ -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 NAMES = {
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(platform) {
38
- return `social-embeds: a ${NAMES[platform]} URL needs a lookup: use the embed code`;
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 && needsLookup(url) === platform) return { error: lookupMessage(platform) };
45
- if (!embed) return { error: `social-embeds: not a ${NAMES[platform]} post URL: ${String(url)}` };
46
- if (embed.platform !== platform) return { error: `social-embeds: expected a ${NAMES[platform]} embed, got ${NAMES[embed.platform]}` };
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 ${NAMES[platform]} embed's url and id do not match` };
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
- export { NAMES, lookupMessage, resolveEmbed, sourceError };
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 };