social-embeds 0.8.0 → 0.9.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.
Files changed (44) hide show
  1. package/README.md +91 -48
  2. package/dist/components/hooks.js +3 -1
  3. package/dist/components/platforms.d.ts +32 -0
  4. package/dist/components/platforms.js +384 -0
  5. package/dist/components/script-embed.js +8 -7
  6. package/dist/components/social-embed.d.ts +31 -4
  7. package/dist/components/social-embed.js +48 -211
  8. package/dist/core/drivers.js +8 -1
  9. package/dist/core/plans/youtube.js +15 -3
  10. package/dist/element.d.ts +1 -1
  11. package/dist/element.js +1 -1
  12. package/dist/index.d.ts +21 -20
  13. package/dist/index.js +3 -2
  14. package/dist/loader/load-script.js +2 -0
  15. package/dist/parser/frames.js +70 -0
  16. package/dist/parser/html.d.ts +19 -0
  17. package/dist/parser/iframe.js +2 -2
  18. package/dist/parser/match-url.js +6 -2
  19. package/dist/parser/parse-embed.js +14 -10
  20. package/dist/parser/platform.d.ts +26 -0
  21. package/dist/parser/platforms/applemusic.js +3 -3
  22. package/dist/parser/platforms/applepodcasts.js +3 -3
  23. package/dist/parser/platforms/dailymotion.js +6 -8
  24. package/dist/parser/platforms/facebook.js +5 -9
  25. package/dist/parser/platforms/giphy.js +6 -6
  26. package/dist/parser/platforms/kick.js +4 -4
  27. package/dist/parser/platforms/linkedin.js +4 -4
  28. package/dist/parser/platforms/pinterest.js +3 -3
  29. package/dist/parser/platforms/reddit.js +3 -2
  30. package/dist/parser/platforms/rumble.js +3 -2
  31. package/dist/parser/platforms/soundcloud.js +3 -3
  32. package/dist/parser/platforms/spotify.js +4 -4
  33. package/dist/parser/platforms/streamable.js +3 -2
  34. package/dist/parser/platforms/tenor.js +3 -2
  35. package/dist/parser/platforms/tiktok.js +6 -6
  36. package/dist/parser/platforms/truthsocial.js +2 -1
  37. package/dist/parser/platforms/tumblr.js +4 -2
  38. package/dist/parser/platforms/twitch.js +6 -7
  39. package/dist/parser/platforms/vimeo.js +31 -12
  40. package/dist/parser/platforms/x.js +1 -1
  41. package/dist/parser/platforms/youtube.js +73 -23
  42. package/dist/parser/url.d.ts +6 -0
  43. package/dist/parser/url.js +1 -1
  44. package/package.json +1 -1
@@ -1,8 +1,8 @@
1
1
  import { hasClass } from "../html.js";
2
- import { sizeOf, underPath, withSize } from "../platform.js";
2
+ import { sizeOf, withSize } from "../platform.js";
3
3
  import { coerce, parseUrl } from "../url.js";
4
+ import { TIKTOK_HOSTS, tiktokFrame } from "../frames.js";
4
5
  // src/parser/platforms/tiktok.ts
5
- const HOSTS = /* @__PURE__ */ new Set(["tiktok.com", "www.tiktok.com"]);
6
6
  const VIDEO_PATH = /^\/@([A-Za-z0-9._]{0,64})\/video\/(\d{1,25})\/?$/;
7
7
  const PLAYER_PATH = /^\/player\/v1\/(\d{1,25})\/?$/;
8
8
  const VIDEO_ID = /^\d{1,25}$/;
@@ -22,7 +22,7 @@ const tiktok = {
22
22
  parse(root) {
23
23
  if (root.tag === "blockquote" && hasClass(root, "tiktok-embed")) {
24
24
  const cite = parseUrl(root.attrs.get("cite") ?? "");
25
- const [, user, citeId] = cite && HOSTS.has(cite.host) ? VIDEO_PATH.exec(cite.path) ?? [] : [];
25
+ const [, user, citeId] = cite && TIKTOK_HOSTS.has(cite.host) ? VIDEO_PATH.exec(cite.path) ?? [] : [];
26
26
  const dataId = root.attrs.get("data-video-id");
27
27
  if (dataId !== void 0 && !VIDEO_ID.test(dataId)) return null;
28
28
  if (dataId !== void 0 && citeId !== void 0 && dataId !== citeId) return null;
@@ -31,7 +31,7 @@ const tiktok = {
31
31
  }
32
32
  if (root.tag !== "iframe") return null;
33
33
  const src = parseUrl(root.attrs.get("src") ?? "");
34
- if (!src || !HOSTS.has(src.host)) return null;
34
+ if (!src || !TIKTOK_HOSTS.has(src.host)) return null;
35
35
  const [, id] = PLAYER_PATH.exec(src.path) ?? [];
36
36
  if (id === void 0) return null;
37
37
  const options = Object.fromEntries([...src.query].map(([key, value]) => [key, coerce(value)]));
@@ -39,10 +39,10 @@ const tiktok = {
39
39
  },
40
40
  isOwnScript: (src) => SCRIPT.test(src),
41
41
  match(url) {
42
- if (!HOSTS.has(url.host)) return null;
42
+ if (!TIKTOK_HOSTS.has(url.host)) return null;
43
43
  const [, user, id] = VIDEO_PATH.exec(url.path) ?? [];
44
44
  return user === void 0 || id === void 0 ? null : video(id, user);
45
45
  },
46
- ownsFrame: (src) => HOSTS.has(src.host) && underPath(src.path, "/player")
46
+ ownsFrame: tiktokFrame
47
47
  };
48
48
  export { tiktok };
@@ -1,6 +1,7 @@
1
1
  import { hasClass } from "../html.js";
2
2
  import { sizeOf, withSize } from "../platform.js";
3
3
  import { parseUrl } from "../url.js";
4
+ import { truthsocialFrame } from "../frames.js";
4
5
  // src/parser/platforms/truthsocial.ts
5
6
  const HOSTS = /* @__PURE__ */ new Set(["truthsocial.com", "www.truthsocial.com"]);
6
7
  const USER = "(@[A-Za-z0-9_.-]{1,64})";
@@ -44,6 +45,6 @@ const truthsocial = {
44
45
  options: {}
45
46
  };
46
47
  },
47
- ownsFrame: (src) => src.host === "truthsocial.com" && src.path.startsWith("/@")
48
+ ownsFrame: truthsocialFrame
48
49
  };
49
50
  export { truthsocial };
@@ -8,6 +8,7 @@ const WWW_PATH = new RegExp(`^/${BLOG}/${POST}(?:/[^/]{1,200})?/?$`);
8
8
  const BLOG_HOST = new RegExp(`^${BLOG}\\.tumblr\\.com$`);
9
9
  const BLOG_PATH = new RegExp(`^/post/${POST}(?:/[^/]{1,200})?/?$`);
10
10
  const FRAME_PATH = new RegExp(`^/embed/post/(?:t:[A-Za-z0-9_-]{1,64}|${BLOG})/${POST}/v2$`);
11
+ const OLD_FRAME_PATH = new RegExp(`^/embed/post/(?:t:)?[A-Za-z0-9_-]{1,64}/${POST}$`);
11
12
  const NOT_BLOGS = /* @__PURE__ */ new Set([
12
13
  "dashboard",
13
14
  "explore",
@@ -21,7 +22,7 @@ const NOT_BLOGS = /* @__PURE__ */ new Set([
21
22
  "api",
22
23
  "help"
23
24
  ]);
24
- const SCRIPT = /^(?:https?:)?\/\/assets\.tumblr\.com\/post\.js(?:\?[^#]*)?$/;
25
+ const SCRIPT = /^(?:https?:)?\/\/(?:secure\.)?assets\.tumblr\.com\/post\.js(?:\?[^#]*)?$/;
25
26
  function fromUrl(url) {
26
27
  if (!url) return null;
27
28
  let blog;
@@ -46,7 +47,8 @@ const tumblr = {
46
47
  if (root.tag !== "div" || !hasClass(root, "tumblr-post")) return null;
47
48
  const frame = parseUrl(root.attrs.get("data-href") ?? "");
48
49
  if (frame?.host !== "embed.tumblr.com") return null;
49
- const [, frameBlog, framePost] = FRAME_PATH.exec(frame.path) ?? [];
50
+ const [, frameBlog, v2Post] = FRAME_PATH.exec(frame.path) ?? [];
51
+ const framePost = v2Post ?? OLD_FRAME_PATH.exec(frame.path)?.[1];
50
52
  if (framePost === void 0) return null;
51
53
  const [link, ...rest] = root.children.filter((node) => node.type !== "text" || node.raw.trim() !== "");
52
54
  if (rest.length > 0 || link?.type !== "element" || link.tag !== "a") return null;
@@ -1,13 +1,12 @@
1
- import { sizeOf, underPath, withSize } from "../platform.js";
1
+ import { sizeOf, withSize } from "../platform.js";
2
2
  import { coerce, parseUrl } from "../url.js";
3
+ import { twitchFrame } from "../frames.js";
3
4
  // src/parser/platforms/twitch.ts
4
5
  const HOSTS = /* @__PURE__ */ new Set([
5
6
  "twitch.tv",
6
7
  "www.twitch.tv",
7
8
  "m.twitch.tv"
8
9
  ]);
9
- const PLAYER_HOST = "player.twitch.tv";
10
- const CLIPS_HOST = "clips.twitch.tv";
11
10
  const CHANNEL = /^[A-Za-z0-9_]{3,25}$/;
12
11
  const VIDEO = /^v?(\d{1,12})$/;
13
12
  const COLLECTION = /^[A-Za-z0-9_-]{6,30}$/;
@@ -66,7 +65,7 @@ function fromPlayer(url) {
66
65
  return list !== void 0 && COLLECTION.test(list) ? collection(list) : null;
67
66
  }
68
67
  function fromPage(url) {
69
- if (url.host === CLIPS_HOST) {
68
+ if (url.host === "clips.twitch.tv") {
70
69
  const slug = url.path.replace(/^\/|\/$/g, "");
71
70
  return CLIP.test(slug) ? {
72
71
  found: clip(slug),
@@ -106,8 +105,8 @@ const twitch = {
106
105
  const src = parseUrl(root.attrs.get("src") ?? "");
107
106
  if (!src) return null;
108
107
  let found = null;
109
- if (src.host === PLAYER_HOST && /^\/?$/.test(src.path)) found = fromPlayer(src);
110
- if (src.host === CLIPS_HOST && src.path === "/embed") {
108
+ if (src.host === "player.twitch.tv" && /^\/?$/.test(src.path)) found = fromPlayer(src);
109
+ if (src.host === "clips.twitch.tv" && src.path === "/embed") {
111
110
  const slug = src.query.get("clip") ?? "";
112
111
  found = CLIP.test(slug) ? clip(slug) : null;
113
112
  }
@@ -124,6 +123,6 @@ const twitch = {
124
123
  options: page.options
125
124
  };
126
125
  },
127
- ownsFrame: (src) => src.host === PLAYER_HOST || src.host === CLIPS_HOST && underPath(src.path, "/embed")
126
+ ownsFrame: twitchFrame
128
127
  };
129
128
  export { twitch };
@@ -1,5 +1,6 @@
1
1
  import { sizeOf, unwrapRatio, withSize } from "../platform.js";
2
2
  import { coerce, parseUrl } from "../url.js";
3
+ import { vimeoFrame } from "../frames.js";
3
4
  // src/parser/platforms/vimeo.ts
4
5
  const HOSTS = /* @__PURE__ */ new Set(["vimeo.com", "www.vimeo.com"]);
5
6
  const VIDEO = "(\\d{1,12})";
@@ -19,7 +20,6 @@ const DROPPED = /* @__PURE__ */ new Set([
19
20
  "player_id"
20
21
  ]);
21
22
  const START = /^t=(\d+(?:\.\d+)?|(?:\d+h)?(?:\d+m)?(?:\d+s)?)$/;
22
- const PLAYER_HOST = "player.vimeo.com";
23
23
  function video(id, hash) {
24
24
  return {
25
25
  platform: "vimeo",
@@ -33,8 +33,30 @@ function start(url) {
33
33
  const [, time] = START.exec(url.fragment ?? "") ?? [];
34
34
  return time ? { t: time } : {};
35
35
  }
36
+ function fromPage(url) {
37
+ if (!url || !HOSTS.has(url.host)) return null;
38
+ for (const path of PAGE_PATHS) {
39
+ const [, id, hash] = path.exec(url.path) ?? [];
40
+ if (id !== void 0) return video(id, hash);
41
+ }
42
+ return null;
43
+ }
44
+ function isLink(node, test) {
45
+ if (node?.type !== "element" || node.tag !== "a" || node.attrs.size !== 1) return false;
46
+ const href = parseUrl(node.attrs.get("href") ?? "");
47
+ return href !== null && HOSTS.has(href.host) && test(href) && node.children.every((child) => child.type === "text");
48
+ }
49
+ function isText(node, text) {
50
+ return node?.type === "text" && node.raw.trim() === text;
51
+ }
52
+ /** Older embed code's credit: the video, its author and Vimeo, linked; it is dropped. */
53
+ function isCredit(element, embed) {
54
+ if (element.tag !== "p" || element.attrs.size !== 0) return false;
55
+ const [title, from, author, on, home, stop, ...rest] = element.children;
56
+ return rest.length === 0 && isLink(title, (url) => fromPage(url)?.id === embed.id) && isText(from, "from") && isLink(author, () => true) && isText(on, "on") && isLink(home, (url) => url.path === "" || url.path === "/") && isText(stop, ".");
57
+ }
36
58
  function fromPlayer(url) {
37
- if (url?.host !== PLAYER_HOST) return null;
59
+ if (url?.host !== "player.vimeo.com") return null;
38
60
  const [, id] = PLAYER_PATH.exec(url.path) ?? [];
39
61
  if (id === void 0) return null;
40
62
  const hash = url.query.get("h");
@@ -58,19 +80,16 @@ const vimeo = {
58
80
  return embed && withSize(embed, wrapped?.size ?? sizeOf(iframe));
59
81
  },
60
82
  isOwnScript: (src) => SCRIPT.test(src),
83
+ isOwnExtra: isCredit,
61
84
  match(url) {
62
85
  const player = fromPlayer(url);
63
86
  if (player) return player;
64
- if (!HOSTS.has(url.host)) return null;
65
- for (const path of PAGE_PATHS) {
66
- const [, id, hash] = path.exec(url.path) ?? [];
67
- if (id !== void 0) return {
68
- ...video(id, hash),
69
- options: start(url)
70
- };
71
- }
72
- return null;
87
+ const page = fromPage(url);
88
+ return page && {
89
+ ...page,
90
+ options: start(url)
91
+ };
73
92
  },
74
- ownsFrame: (src) => src.host === PLAYER_HOST
93
+ ownsFrame: vimeoFrame
75
94
  };
76
95
  export { vimeo };
@@ -44,7 +44,7 @@ const x = {
44
44
  platform: "x",
45
45
  fallback: FALLBACK,
46
46
  parse(root) {
47
- if (root.tag !== "blockquote" || !hasClass(root, "twitter-tweet")) return null;
47
+ if (root.tag !== "blockquote" || !(hasClass(root, "twitter-tweet") || hasClass(root, "twitter-video"))) return null;
48
48
  const status = descendants(root.children).reverse().map((element) => element.tag === "a" ? post(parseUrl(element.attrs.get("href") ?? "")) : null).find((found) => found !== null);
49
49
  if (!status) return null;
50
50
  return {
@@ -1,17 +1,17 @@
1
1
  import { sizeOf, underPath, withSize } from "../platform.js";
2
2
  import { coerce, parseUrl } from "../url.js";
3
+ import { YOUTUBE_HOSTS, YOUTUBE_NOCOOKIE_HOSTS, youtubeFrame } from "../frames.js";
3
4
  // src/parser/platforms/youtube.ts
4
- const YOUTUBE_HOSTS = /* @__PURE__ */ new Set([
5
- "youtube.com",
6
- "www.youtube.com",
7
- "m.youtube.com"
8
- ]);
9
- const NOCOOKIE_HOSTS = /* @__PURE__ */ new Set(["youtube-nocookie.com", "www.youtube-nocookie.com"]);
10
- const VIDEO_ID = /^(?!videoseries)[A-Za-z0-9_-]{11}$/;
11
- const EMBED_PATH = /^\/embed\/((?!videoseries)[A-Za-z0-9_-]{11})\/?$/;
12
- const URL_PATHS = /^\/(shorts|embed|live)\/((?!videoseries)[A-Za-z0-9_-]{11})\/?$/;
5
+ const BASE = "https://www.youtube.com";
6
+ const VIDEO_ID = /^(?!videoseries|live_stream)[A-Za-z0-9_-]{11}$/;
7
+ const EMBED_PATH = /^\/embed\/(?:(videoseries|live_stream)|([A-Za-z0-9_-]{11}))\/?$/;
8
+ const URL_PATHS = /^\/(shorts|live)\/((?!videoseries|live_stream)[A-Za-z0-9_-]{11})\/?$/;
13
9
  const SHORT_LINK_PATH = /^\/([A-Za-z0-9_-]{11})\/?$/;
10
+ const PLAYLIST_ID = /^[A-Za-z0-9_-]{10,64}$/;
11
+ const CHANNEL_ID = /^UC[A-Za-z0-9_-]{22}$/;
12
+ const CHANNEL_LIVE_PATH = /^\/channel\/(UC[A-Za-z0-9_-]{22})\/live\/?$/;
14
13
  const DURATION = /^(?:(\d{1,5})h)?(?:(\d{1,6})m)?(?:(\d{1,7})s?)?$/;
14
+ const FRAGMENT_START = /^t=(.+)$/;
15
15
  /** `90`, `90s`, `1m30s`, `1h2m3s` → seconds. */
16
16
  function seconds(value) {
17
17
  if (value === void 0 || value === "") return void 0;
@@ -24,17 +24,46 @@ function seconds(value) {
24
24
  function startOf(query) {
25
25
  return seconds(query.get("start")) ?? seconds(query.get("t"));
26
26
  }
27
- function video(id, shorts = false) {
27
+ function found(id, url) {
28
28
  return {
29
29
  platform: "youtube",
30
30
  kind: "video",
31
- url: shorts ? `https://www.youtube.com/shorts/${id}` : `https://www.youtube.com/watch?v=${id}`,
31
+ url,
32
32
  id,
33
33
  variant: "iframe"
34
34
  };
35
35
  }
36
+ function video(id, shorts = false) {
37
+ return found(id, shorts ? `${BASE}/shorts/${id}` : `${BASE}/watch?v=${id}`);
38
+ }
39
+ function playlist(id) {
40
+ return id !== void 0 && PLAYLIST_ID.test(id) ? found(id, `${BASE}/playlist?list=${id}`) : null;
41
+ }
42
+ function live(id) {
43
+ return id !== void 0 && CHANNEL_ID.test(id) ? found(id, `${BASE}/channel/${id}/live`) : null;
44
+ }
45
+ /** `key` is the query parameter that names the content, for a playlist or a channel's stream. */
46
+ function fromEmbed(url) {
47
+ const [, series, id] = EMBED_PATH.exec(url.path) ?? [];
48
+ if (id !== void 0) return { found: video(id) };
49
+ if (series === "videoseries") {
50
+ const list = playlist(url.query.get("list"));
51
+ return list && {
52
+ found: list,
53
+ key: "list"
54
+ };
55
+ }
56
+ if (series === "live_stream") {
57
+ const stream = live(url.query.get("channel"));
58
+ return stream && {
59
+ found: stream,
60
+ key: "channel"
61
+ };
62
+ }
63
+ return null;
64
+ }
36
65
  function urlOptions(url, nocookie) {
37
- const start = startOf(url.query);
66
+ const start = startOf(url.query) ?? seconds(FRAGMENT_START.exec(url.fragment ?? "")?.[1]);
38
67
  return {
39
68
  ...start === void 0 ? {} : { start },
40
69
  ...nocookie ? { nocookie: true } : {}
@@ -45,17 +74,17 @@ const youtube = {
45
74
  parse(root) {
46
75
  if (root.tag !== "iframe") return null;
47
76
  const src = parseUrl(root.attrs.get("src") ?? "");
48
- const nocookie = src !== null && NOCOOKIE_HOSTS.has(src.host);
77
+ const nocookie = src !== null && YOUTUBE_NOCOOKIE_HOSTS.has(src.host);
49
78
  if (!src || !(nocookie || YOUTUBE_HOSTS.has(src.host))) return null;
50
- const [, id] = EMBED_PATH.exec(src.path) ?? [];
51
- if (id === void 0) return null;
52
- const entries = [...src.query].filter(([key]) => key !== "t").map(([key, value]) => [key, coerce(value)]);
79
+ const player = fromEmbed(src);
80
+ if (!player) return null;
81
+ const entries = [...src.query].filter(([key]) => key !== "t" && key !== player.key).map(([key, value]) => [key, coerce(value)]);
53
82
  const options = Object.fromEntries(entries);
54
83
  const start = startOf(src.query);
55
84
  if (start !== void 0) options.start = start;
56
85
  if (nocookie) options.nocookie = true;
57
86
  return withSize({
58
- ...video(id),
87
+ ...player.found,
59
88
  options
60
89
  }, sizeOf(root));
61
90
  },
@@ -68,23 +97,44 @@ const youtube = {
68
97
  options: urlOptions(url, false)
69
98
  };
70
99
  }
71
- const nocookie = NOCOOKIE_HOSTS.has(url.host);
100
+ const nocookie = YOUTUBE_NOCOOKIE_HOSTS.has(url.host);
72
101
  if (!nocookie && !YOUTUBE_HOSTS.has(url.host)) return null;
73
- if (!nocookie && /^\/watch\/?$/.test(url.path)) {
102
+ const options = urlOptions(url, nocookie);
103
+ if (underPath(url.path, "/embed")) {
104
+ const player = fromEmbed(url);
105
+ return player && {
106
+ ...player.found,
107
+ options
108
+ };
109
+ }
110
+ if (nocookie) return null;
111
+ if (/^\/watch\/?$/.test(url.path)) {
74
112
  const id = url.query.get("v");
75
113
  return id !== void 0 && VIDEO_ID.test(id) ? {
76
114
  ...video(id),
77
- options: urlOptions(url, false)
115
+ options
78
116
  } : null;
79
117
  }
118
+ if (/^\/playlist\/?$/.test(url.path)) {
119
+ const list = playlist(url.query.get("list"));
120
+ return list && {
121
+ ...list,
122
+ options
123
+ };
124
+ }
125
+ const [, channel] = CHANNEL_LIVE_PATH.exec(url.path) ?? [];
126
+ const stream = live(channel);
127
+ if (stream) return {
128
+ ...stream,
129
+ options
130
+ };
80
131
  const [, kind, id] = URL_PATHS.exec(url.path) ?? [];
81
132
  if (kind === void 0 || id === void 0) return null;
82
- if (nocookie && kind !== "embed") return null;
83
133
  return {
84
134
  ...video(id, kind === "shorts"),
85
- options: urlOptions(url, nocookie)
135
+ options
86
136
  };
87
137
  },
88
- ownsFrame: (src) => NOCOOKIE_HOSTS.has(src.host) || YOUTUBE_HOSTS.has(src.host) && underPath(src.path, "/embed")
138
+ ownsFrame: youtubeFrame
89
139
  };
90
140
  export { youtube };
@@ -0,0 +1,6 @@
1
+ export type ParsedUrl = {
2
+ host: string;
3
+ path: string;
4
+ query: ReadonlyMap<string, string>;
5
+ fragment?: string;
6
+ };
@@ -6,7 +6,7 @@ function parseUrl(input) {
6
6
  if (!match) return null;
7
7
  const [, host = "", path = "", search = "", fragment] = match;
8
8
  const query = /* @__PURE__ */ new Map();
9
- for (const pair of search.split("&")) {
9
+ for (const pair of search.replaceAll("&amp;", "&").split("&")) {
10
10
  if (pair === "") continue;
11
11
  const eq = pair.indexOf("=");
12
12
  const key = decodeComponent(eq === -1 ? pair : pair.slice(0, eq));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "social-embeds",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "Turn social media embed code or post URLs into working React components.",
5
5
  "keywords": [
6
6
  "react",