social-embeds 0.8.0 → 0.8.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.
@@ -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 };
@@ -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("&", "&").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.8.1",
4
4
  "description": "Turn social media embed code or post URLs into working React components.",
5
5
  "keywords": [
6
6
  "react",