social-embeds 0.3.0 → 0.4.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,3 +1,150 @@
1
+ // src/parser/url.ts
2
+ const URL_PATTERN = /^(?:https?:)?\/\/([^/?#\s@]+)((?:\/[^?#\s]*)?)(?:\?([^#\s]*))?(?:#(\S*))?$/i;
3
+ /** Absolute or protocol-relative http(s) URLs only. */
4
+ function parseUrl(input) {
5
+ const match = URL_PATTERN.exec(input.trim());
6
+ if (!match) return null;
7
+ const [, host = "", path = "", search = "", fragment] = match;
8
+ const query = /* @__PURE__ */ new Map();
9
+ for (const pair of search.split("&")) {
10
+ if (pair === "") continue;
11
+ const eq = pair.indexOf("=");
12
+ const key = decodeComponent(eq === -1 ? pair : pair.slice(0, eq));
13
+ const value = decodeComponent(eq === -1 ? "" : pair.slice(eq + 1));
14
+ if (key === null || value === null) return null;
15
+ if (!query.has(key)) query.set(key, value);
16
+ }
17
+ return {
18
+ host: host.toLowerCase(),
19
+ path,
20
+ query,
21
+ ...fragment ? { fragment } : {}
22
+ };
23
+ }
24
+ function decodeComponent(component) {
25
+ try {
26
+ return decodeURIComponent(component.replace(/\+/g, " "));
27
+ } catch {
28
+ return null;
29
+ }
30
+ }
31
+ /** Types an option value: booleans, canonical integers, otherwise the string as given. */
32
+ function coerce(value) {
33
+ if (value === "true") return true;
34
+ if (value === "false") return false;
35
+ if (/^(?:0|[1-9]\d{0,14})$/.test(value)) return Number(value);
36
+ return value;
37
+ }
38
+ function pixels(value) {
39
+ return value !== void 0 && /^[1-9]\d{0,5}$/.test(value) ? Number(value) : void 0;
40
+ }
41
+ // src/parser/platform.ts
42
+ /** The first attribute present for a key wins. */
43
+ function attributeOptions(element, mapping, coerceValue) {
44
+ const options = {};
45
+ for (const [attribute, key] of mapping) {
46
+ const value = element.attrs.get(attribute);
47
+ if (value !== void 0 && !(key in options)) options[key] = coerceValue(value);
48
+ }
49
+ return options;
50
+ }
51
+ function sizeOf(element) {
52
+ const width = pixels(element.attrs.get("width"));
53
+ const height = pixels(element.attrs.get("height"));
54
+ if (width === void 0 && height === void 0) return void 0;
55
+ return {
56
+ ...width === void 0 ? {} : { width },
57
+ ...height === void 0 ? {} : { height }
58
+ };
59
+ }
60
+ /** Adds `size` only when known, so results stay free of `undefined` properties. */
61
+ function withSize(embed, size) {
62
+ return size ? {
63
+ ...embed,
64
+ size
65
+ } : embed;
66
+ }
67
+ /**
68
+ * A platform's own responsive wrapper: a `div` whose only attribute is a `style` that `pattern`
69
+ * matches, around one iframe. The padding percentage it captures is the video's ratio.
70
+ */
71
+ function unwrapRatio(root, pattern) {
72
+ if (root.tag !== "div" || root.attrs.size !== 1) return null;
73
+ const [, percent] = pattern.exec(root.attrs.get("style") ?? "") ?? [];
74
+ if (percent === void 0) return null;
75
+ let iframe;
76
+ for (const child of root.children) {
77
+ if (child.type === "text" && /^\s*$/.test(child.raw)) continue;
78
+ if (child.type !== "element" || child.tag !== "iframe" || iframe) return null;
79
+ iframe = child;
80
+ }
81
+ const height = Math.round(Number(percent) * 100);
82
+ if (!iframe || !(height > 0 && height <= 1e5)) return null;
83
+ const divisor = gcd(1e4, height);
84
+ return {
85
+ iframe,
86
+ size: {
87
+ width: 1e4 / divisor,
88
+ height: height / divisor
89
+ }
90
+ };
91
+ }
92
+ function gcd(a, b) {
93
+ return b === 0 ? a : gcd(b, a % b);
94
+ }
95
+ // src/parser/platforms/dailymotion.ts
96
+ const VIDEO_ID$2 = /^x[a-z0-9]{4,12}$/;
97
+ const PLAYER_ID = /^x[a-z0-9]{3,12}$/;
98
+ const WRAPPER$1 = /^position:\s*relative;\s*padding-bottom:\s*(\d{1,3}(?:\.\d{1,4})?)%;\s*height:\s*0;\s*overflow:\s*hidden;?$/;
99
+ const HOSTS$10 = /* @__PURE__ */ new Set(["dailymotion.com", "www.dailymotion.com"]);
100
+ function video$3(id, options) {
101
+ return {
102
+ platform: "dailymotion",
103
+ kind: "video",
104
+ url: `https://www.dailymotion.com/video/${id}`,
105
+ id,
106
+ variant: "iframe",
107
+ options
108
+ };
109
+ }
110
+ /** A geo.dailymotion.com player, with or without a Player ID. Playlists are left out. */
111
+ function fromPlayer$2(url) {
112
+ if (url.host !== "geo.dailymotion.com" || url.query.has("playlist")) return null;
113
+ const match = /^\/player(?:\/([a-z0-9]+))?\.html$/.exec(url.path);
114
+ if (!match) return null;
115
+ const [, playerId] = match;
116
+ const id = url.query.get("video") ?? "";
117
+ if (!VIDEO_ID$2.test(id)) return null;
118
+ if (playerId !== void 0 && !PLAYER_ID.test(playerId)) return null;
119
+ const options = Object.fromEntries([...url.query].filter(([key]) => key !== "video").map(([key, value]) => [key, coerce(value)]));
120
+ if (playerId !== void 0) options.playerId = playerId;
121
+ return video$3(id, options);
122
+ }
123
+ function fromPage$1(url) {
124
+ if (url.host === "dai.ly") {
125
+ const id = url.path.replace(/^\/|\/$/g, "");
126
+ return VIDEO_ID$2.test(id) ? video$3(id, {}) : null;
127
+ }
128
+ if (!HOSTS$10.has(url.host)) return null;
129
+ const [, id] = /^(?:\/embed)?\/video\/([a-z0-9]+)\/?$/.exec(url.path) ?? [];
130
+ return id !== void 0 && VIDEO_ID$2.test(id) ? video$3(id, {}) : null;
131
+ }
132
+ const dailymotion = {
133
+ platform: "dailymotion",
134
+ parse(root) {
135
+ const wrapped = unwrapRatio(root, WRAPPER$1);
136
+ const iframe = wrapped?.iframe ?? root;
137
+ if (iframe.tag !== "iframe") return null;
138
+ const src = parseUrl(iframe.attrs.get("src") ?? "");
139
+ if (!src) return null;
140
+ const embed = fromPlayer$2(src) ?? fromPage$1(src);
141
+ return embed && withSize(embed, wrapped?.size ?? sizeOf(iframe));
142
+ },
143
+ isOwnScript: () => false,
144
+ match(url) {
145
+ return fromPlayer$2(url) ?? fromPage$1(url);
146
+ }
147
+ };
1
148
  // src/parser/html.ts
2
149
  const VOID_TAGS = /* @__PURE__ */ new Set([
3
150
  "area",
@@ -165,73 +312,8 @@ function rebuild(nodes, whitelist) {
165
312
  function escapeAttribute(value) {
166
313
  return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
167
314
  }
168
- // src/parser/url.ts
169
- const URL_PATTERN = /^(?:https?:)?\/\/([^/?#\s@]+)((?:\/[^?#\s]*)?)(?:\?([^#\s]*))?(?:#\S*)?$/i;
170
- /** Absolute or protocol-relative http(s) URLs only. */
171
- function parseUrl(input) {
172
- const match = URL_PATTERN.exec(input.trim());
173
- if (!match) return null;
174
- const [, host = "", path = "", search = ""] = match;
175
- const query = /* @__PURE__ */ new Map();
176
- for (const pair of search.split("&")) {
177
- if (pair === "") continue;
178
- const eq = pair.indexOf("=");
179
- const key = decodeComponent(eq === -1 ? pair : pair.slice(0, eq));
180
- const value = decodeComponent(eq === -1 ? "" : pair.slice(eq + 1));
181
- if (key === null || value === null) return null;
182
- if (!query.has(key)) query.set(key, value);
183
- }
184
- return {
185
- host: host.toLowerCase(),
186
- path,
187
- query
188
- };
189
- }
190
- function decodeComponent(component) {
191
- try {
192
- return decodeURIComponent(component.replace(/\+/g, " "));
193
- } catch {
194
- return null;
195
- }
196
- }
197
- /** Types an option value: booleans, canonical integers, otherwise the string as given. */
198
- function coerce(value) {
199
- if (value === "true") return true;
200
- if (value === "false") return false;
201
- if (/^(?:0|[1-9]\d{0,14})$/.test(value)) return Number(value);
202
- return value;
203
- }
204
- function pixels(value) {
205
- return value !== void 0 && /^[1-9]\d{0,5}$/.test(value) ? Number(value) : void 0;
206
- }
207
- // src/parser/platform.ts
208
- /** The first attribute present for a key wins. */
209
- function attributeOptions(element, mapping, coerceValue) {
210
- const options = {};
211
- for (const [attribute, key] of mapping) {
212
- const value = element.attrs.get(attribute);
213
- if (value !== void 0 && !(key in options)) options[key] = coerceValue(value);
214
- }
215
- return options;
216
- }
217
- function sizeOf(element) {
218
- const width = pixels(element.attrs.get("width"));
219
- const height = pixels(element.attrs.get("height"));
220
- if (width === void 0 && height === void 0) return void 0;
221
- return {
222
- ...width === void 0 ? {} : { width },
223
- ...height === void 0 ? {} : { height }
224
- };
225
- }
226
- /** Adds `size` only when known, so results stay free of `undefined` properties. */
227
- function withSize(embed, size) {
228
- return size ? {
229
- ...embed,
230
- size
231
- } : embed;
232
- }
233
315
  // src/parser/platforms/facebook.ts
234
- const HOSTS$7 = /* @__PURE__ */ new Set([
316
+ const HOSTS$9 = /* @__PURE__ */ new Set([
235
317
  "facebook.com",
236
318
  "www.facebook.com",
237
319
  "m.facebook.com"
@@ -252,7 +334,7 @@ const PHOTO_PATHS = /* @__PURE__ */ new Set([
252
334
  ]);
253
335
  const STORY_ID = new RegExp(`^${POST_ID}$`);
254
336
  const NUMERIC_ID = /^\d{1,25}$/;
255
- const SCRIPT$8 = /^(?:https?:)?\/\/connect\.facebook\.net\/([a-z]{2,3}_[A-Z]{2})\/sdk\.js(?:[?#].*)?$/;
337
+ const SCRIPT$9 = /^(?:https?:)?\/\/connect\.facebook\.net\/([a-z]{2,3}_[A-Z]{2})\/sdk\.js(?:[?#].*)?$/;
256
338
  const OPTION_ATTRIBUTES$5 = [
257
339
  ["data-width", "width"],
258
340
  ["data-show-text", "showText"],
@@ -277,8 +359,8 @@ const FALLBACK$3 = /* @__PURE__ */ new Map([
277
359
  ["a", ["href"]],
278
360
  ["p", []]
279
361
  ]);
280
- function target(url) {
281
- if (!url || !HOSTS$7.has(url.host)) return null;
362
+ function target$1(url) {
363
+ if (!url || !HOSTS$9.has(url.host)) return null;
282
364
  const { path, query } = url;
283
365
  const [, name, postId] = POSTS_PATH$1.exec(path) ?? [];
284
366
  if (name !== void 0 && postId !== void 0) return {
@@ -351,7 +433,7 @@ const facebook = {
351
433
  parse(root) {
352
434
  if (root.tag === "div") {
353
435
  const kind = hasClass(root, "fb-post") ? "post" : hasClass(root, "fb-video") ? "video" : null;
354
- const found = target(parseUrl(root.attrs.get("data-href") ?? ""));
436
+ const found = target$1(parseUrl(root.attrs.get("data-href") ?? ""));
355
437
  if (!kind || !found) return null;
356
438
  return {
357
439
  ...embed(found, kind, "sdk", attributeOptions(root, OPTION_ATTRIBUTES$5, coerce)),
@@ -361,7 +443,7 @@ const facebook = {
361
443
  if (root.tag !== "iframe") return null;
362
444
  const src = parseUrl(root.attrs.get("src") ?? "");
363
445
  if (!src || !PLUGIN_HOSTS.has(src.host)) return null;
364
- const found = target(parseUrl(src.query.get("href") ?? ""));
446
+ const found = target$1(parseUrl(src.query.get("href") ?? ""));
365
447
  if (!found) return null;
366
448
  if (src.path === "/plugins/post.php") {
367
449
  const options = {};
@@ -393,19 +475,19 @@ const facebook = {
393
475
  }
394
476
  return null;
395
477
  },
396
- isOwnScript: (src) => SCRIPT$8.test(src),
478
+ isOwnScript: (src) => SCRIPT$9.test(src),
397
479
  scriptOptions(src) {
398
- const [, locale] = SCRIPT$8.exec(src) ?? [];
480
+ const [, locale] = SCRIPT$9.exec(src) ?? [];
399
481
  return locale === void 0 ? {} : { locale };
400
482
  },
401
483
  allowsFbRoot: true,
402
484
  match(url) {
403
- const found = target(url);
485
+ const found = target$1(url);
404
486
  return found && embed(found, found.kind, "sdk", {});
405
487
  }
406
488
  };
407
489
  // src/parser/platforms/instagram.ts
408
- const HOSTS$6 = /* @__PURE__ */ new Set(["instagram.com", "www.instagram.com"]);
490
+ const HOSTS$8 = /* @__PURE__ */ new Set(["instagram.com", "www.instagram.com"]);
409
491
  const POST_PATH$3 = /^\/(?:([A-Za-z0-9._]{1,30})\/)?(p|reel|reels|tv)\/([A-Za-z0-9_-]{1,64})\/?$/;
410
492
  const TYPES = /* @__PURE__ */ new Set([
411
493
  "p",
@@ -414,14 +496,14 @@ const TYPES = /* @__PURE__ */ new Set([
414
496
  "tv"
415
497
  ]);
416
498
  const USER_POST_TYPES = /* @__PURE__ */ new Set(["p", "reel"]);
417
- const SCRIPT$7 = /^(?:https?:)?\/\/www\.instagram\.com\/embed\.js$/;
499
+ const SCRIPT$8 = /^(?:https?:)?\/\/www\.instagram\.com\/embed\.js$/;
418
500
  const FALLBACK$2 = /* @__PURE__ */ new Map([
419
501
  ["div", []],
420
502
  ["a", ["href"]],
421
503
  ["p", []]
422
504
  ]);
423
505
  function post$5(url) {
424
- if (!url || !HOSTS$6.has(url.host)) return null;
506
+ if (!url || !HOSTS$8.has(url.host)) return null;
425
507
  const [, user, type, code] = POST_PATH$3.exec(url.path) ?? [];
426
508
  if (type === void 0 || code === void 0) return null;
427
509
  if (user !== void 0 && (TYPES.has(user) || !USER_POST_TYPES.has(type))) return null;
@@ -451,7 +533,7 @@ const instagram = {
451
533
  fallbackHtml: rebuild(root.children, FALLBACK$2)
452
534
  };
453
535
  },
454
- isOwnScript: (src) => SCRIPT$7.test(src),
536
+ isOwnScript: (src) => SCRIPT$8.test(src),
455
537
  match(url) {
456
538
  const found = post$5(url);
457
539
  return found && {
@@ -500,7 +582,7 @@ const linkedin = {
500
582
  const HOST = /^(?:(?:www|[a-z]{2})\.)?pinterest\.(?:[a-z]{2,3}|co\.[a-z]{2}|com\.[a-z]{2})$/;
501
583
  const PIN_PATH = /^\/pin\/(?:[A-Za-z0-9-]{0,200}--)?(\d{1,25})\/?$/;
502
584
  const PIN_ID = /^\d{1,25}$/;
503
- const SCRIPT$6 = /^(?:https?:)?\/\/assets\.pinterest\.com\/js\/pinit(?:_main)?\.js$/;
585
+ const SCRIPT$7 = /^(?:https?:)?\/\/assets\.pinterest\.com\/js\/pinit(?:_main)?\.js$/;
504
586
  const OPTION_ATTRIBUTES$4 = [
505
587
  ["data-pin-width", "width"],
506
588
  ["data-pin-lang", "lang"],
@@ -541,7 +623,7 @@ const pinterest = {
541
623
  options: {}
542
624
  } : null;
543
625
  },
544
- isOwnScript: (src) => SCRIPT$6.test(src),
626
+ isOwnScript: (src) => SCRIPT$7.test(src),
545
627
  match(url) {
546
628
  const found = pinOf(url);
547
629
  return found && {
@@ -551,14 +633,14 @@ const pinterest = {
551
633
  }
552
634
  };
553
635
  // src/parser/platforms/reddit.ts
554
- const HOSTS$5 = /* @__PURE__ */ new Set([
636
+ const HOSTS$7 = /* @__PURE__ */ new Set([
555
637
  "reddit.com",
556
638
  "www.reddit.com",
557
639
  "old.reddit.com",
558
640
  "new.reddit.com"
559
641
  ]);
560
642
  const POST_PATH$2 = /^\/r\/([A-Za-z0-9_]{2,21})\/comments\/([a-z0-9]{1,12})(?:\/([^/]{1,300}))?(?:\/([a-z0-9]{1,16}))?\/?$/;
561
- const SCRIPT$5 = /^(?:https?:)?\/\/embed\.reddit\.com\/widgets\.js$/;
643
+ const SCRIPT$6 = /^(?:https?:)?\/\/embed\.reddit\.com\/widgets\.js$/;
562
644
  const OPTION_ATTRIBUTES$3 = [
563
645
  "depth",
564
646
  "style",
@@ -572,7 +654,7 @@ const OPTION_ATTRIBUTES$3 = [
572
654
  "snippet"
573
655
  ].map((name) => [`data-embed-${name}`, name]);
574
656
  function post$3(url) {
575
- if (!url || !HOSTS$5.has(url.host)) return null;
657
+ if (!url || !HOSTS$7.has(url.host)) return null;
576
658
  const [, sub, postId, slug, commentId] = POST_PATH$2.exec(url.path) ?? [];
577
659
  if (sub === void 0 || postId === void 0) return null;
578
660
  const base = {
@@ -605,7 +687,7 @@ const reddit = {
605
687
  options: attributeOptions(root, OPTION_ATTRIBUTES$3, coerce)
606
688
  }, height === void 0 ? void 0 : { height });
607
689
  },
608
- isOwnScript: (src) => SCRIPT$5.test(src),
690
+ isOwnScript: (src) => SCRIPT$6.test(src),
609
691
  match(url) {
610
692
  const found = post$3(url);
611
693
  return found && {
@@ -615,15 +697,15 @@ const reddit = {
615
697
  }
616
698
  };
617
699
  // src/parser/platforms/snapchat.ts
618
- const HOSTS$4 = /* @__PURE__ */ new Set(["snapchat.com", "www.snapchat.com"]);
700
+ const HOSTS$6 = /* @__PURE__ */ new Set(["snapchat.com", "www.snapchat.com"]);
619
701
  const USER = "([A-Za-z][A-Za-z0-9._-]{1,29})";
620
702
  const SPOTLIGHT = new RegExp(`^(?:/@${USER})?/spotlight/([A-Za-z0-9_-]{20,120})$`);
621
703
  const PROFILE = new RegExp(`^/(?:@|add/)${USER}$`);
622
704
  const LENS = /^\/lens\/([0-9a-f]{32}|\d{1,20})$/;
623
705
  const HIGHLIGHT = new RegExp(`^/@${USER}/highlight/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$`);
624
- const SCRIPT$4 = /^(?:https?:)?\/\/www\.snapchat\.com\/embed\.js$/;
625
- function content(url) {
626
- if (!url || !HOSTS$4.has(url.host)) return null;
706
+ const SCRIPT$5 = /^(?:https?:)?\/\/www\.snapchat\.com\/embed\.js$/;
707
+ function content$1(url) {
708
+ if (!url || !HOSTS$6.has(url.host)) return null;
627
709
  const path = url.path.replace(/\/$/, "");
628
710
  const base = {
629
711
  platform: "snapchat",
@@ -668,13 +750,13 @@ const snapchat = {
668
750
  if (root.tag !== "blockquote" || !hasClass(root, "snapchat-embed")) return null;
669
751
  const embedUrl = parseUrl(root.attrs.get("data-snapchat-embed-url") ?? "");
670
752
  if (!embedUrl?.path.endsWith("/embed")) return null;
671
- const found = content({
753
+ const found = content$1({
672
754
  ...embedUrl,
673
755
  path: embedUrl.path.slice(0, -6)
674
756
  });
675
757
  if (!found) return null;
676
758
  if (!descendants(root.children).filter((el) => el.tag === "a").every((link) => {
677
- const linked = content(parseUrl(link.attrs.get("href") ?? ""));
759
+ const linked = content$1(parseUrl(link.attrs.get("href") ?? ""));
678
760
  return linked?.kind === found.kind && linked.id === found.id;
679
761
  })) return null;
680
762
  const title = root.attrs.get("data-snapchat-embed-title");
@@ -688,25 +770,71 @@ const snapchat = {
688
770
  height
689
771
  });
690
772
  },
691
- isOwnScript: (src) => SCRIPT$4.test(src),
773
+ isOwnScript: (src) => SCRIPT$5.test(src),
692
774
  match(url) {
693
- const found = content(url);
775
+ const found = content$1(url);
694
776
  return found && {
695
777
  ...found,
696
778
  options: {}
697
779
  };
698
780
  }
699
781
  };
782
+ // src/parser/platforms/spotify.ts
783
+ const CONTENT_PATH = new RegExp(`^(?:/intl-[a-z]{2}(?:-[A-Za-z]{2})?)?(?:/embed)?/(track|album|playlist|artist|episode|show|audiobook|chapter)/([A-Za-z0-9]{22})(/video)?/?$`);
784
+ const DROPPED$1 = /* @__PURE__ */ new Set(["utm_source", "si"]);
785
+ function content(url) {
786
+ if (url?.host !== "open.spotify.com") return null;
787
+ const [, type, id, video] = CONTENT_PATH.exec(url.path) ?? [];
788
+ if (type === void 0 || id === void 0) return null;
789
+ return {
790
+ platform: "spotify",
791
+ kind: video ? "video" : "post",
792
+ url: `https://open.spotify.com/${type}/${id}`,
793
+ id,
794
+ variant: "iframe",
795
+ query: url.query
796
+ };
797
+ }
798
+ function options(query) {
799
+ return Object.fromEntries([...query].filter(([key]) => !DROPPED$1.has(key)).map(([key, value]) => [key, coerce(value)]));
800
+ }
801
+ const spotify = {
802
+ platform: "spotify",
803
+ parse(root) {
804
+ if (root.tag !== "iframe") return null;
805
+ const src = parseUrl(root.attrs.get("src") ?? "");
806
+ if (!src?.path.startsWith("/embed/")) return null;
807
+ const found = content(src);
808
+ if (!found) return null;
809
+ const { query, ...embed } = found;
810
+ return withSize({
811
+ ...embed,
812
+ options: options(query)
813
+ }, sizeOf(root));
814
+ },
815
+ isOwnScript: () => false,
816
+ match(url) {
817
+ const found = content(url);
818
+ return found && {
819
+ platform: found.platform,
820
+ kind: "post",
821
+ url: found.url,
822
+ id: found.id,
823
+ variant: found.variant,
824
+ options: {}
825
+ };
826
+ }
827
+ };
700
828
  // src/parser/platforms/telegram.ts
701
- const HOSTS$3 = /* @__PURE__ */ new Set([
829
+ const HOSTS$5 = /* @__PURE__ */ new Set([
702
830
  "t.me",
703
831
  "telegram.me",
704
832
  "www.telegram.me"
705
833
  ]);
706
- const CHANNEL = "[A-Za-z][A-Za-z0-9_]{2,31}";
707
- const POST = new RegExp(`^(${CHANNEL})/(\\d{1,10})$`);
708
- const POST_PATH$1 = new RegExp(`^/(?:s/)?(${CHANNEL})/(\\d{1,10})/?$`);
709
- const SCRIPT$3 = /^(?:https?:)?\/\/telegram\.org\/js\/telegram-widget\.js(?:\?\d{1,3})?$/;
834
+ const CHANNEL$1 = "[A-Za-z][A-Za-z0-9_]{2,31}";
835
+ const POST = new RegExp(`^(${CHANNEL$1})/(\\d{1,10})$`);
836
+ const POST_PATH$1 = new RegExp(`^/(?:s/)?(${CHANNEL$1})/(\\d{1,10})/?$`);
837
+ const SCRIPT$4 = /^(?:https?:)?\/\/telegram\.org\/js\/telegram-widget\.js(?:\?\d{1,3})?$/;
710
838
  const OTHER_WIDGETS = [
711
839
  "data-telegram-login",
712
840
  "data-telegram-discussion",
@@ -735,7 +863,7 @@ const telegram = {
735
863
  platform: "telegram",
736
864
  parse: () => null,
737
865
  parseScript(script) {
738
- if (!SCRIPT$3.test(script.attrs.get("src") ?? "")) return null;
866
+ if (!SCRIPT$4.test(script.attrs.get("src") ?? "")) return null;
739
867
  if (OTHER_WIDGETS.some((name) => script.attrs.has(name))) return null;
740
868
  const [, channel, id] = POST.exec(script.attrs.get("data-telegram-post") ?? "") ?? [];
741
869
  if (channel === void 0 || id === void 0) return null;
@@ -743,9 +871,9 @@ const telegram = {
743
871
  if (script.attrs.has("data-single")) options.single = true;
744
872
  return post$2(channel, id, options);
745
873
  },
746
- isOwnScript: (src) => SCRIPT$3.test(src),
874
+ isOwnScript: (src) => SCRIPT$4.test(src),
747
875
  match(url) {
748
- if (!HOSTS$3.has(url.host)) return null;
876
+ if (!HOSTS$5.has(url.host)) return null;
749
877
  const [, channel, id] = POST_PATH$1.exec(url.path) ?? [];
750
878
  if (channel === void 0 || id === void 0) return null;
751
879
  const options = {};
@@ -756,18 +884,18 @@ const telegram = {
756
884
  }
757
885
  };
758
886
  // src/parser/platforms/threads.ts
759
- const HOSTS$2 = /* @__PURE__ */ new Set([
887
+ const HOSTS$4 = /* @__PURE__ */ new Set([
760
888
  "threads.com",
761
889
  "www.threads.com",
762
890
  "threads.net",
763
891
  "www.threads.net"
764
892
  ]);
765
893
  const POST_PATH = /^\/(?:@[A-Za-z0-9._]{1,30}\/post|t)\/([A-Za-z0-9_-]{1,64})\/?$/;
766
- const SCRIPT$2 = /^(?:https?:)?\/\/www\.threads\.(?:com|net)\/embed\.js$/;
894
+ const SCRIPT$3 = /^(?:https?:)?\/\/www\.threads\.(?:com|net)\/embed\.js$/;
767
895
  const OPTION_ATTRIBUTES$1 = [["data-theme", "theme"], ["data-text-post-version", "version"]];
768
896
  const FALLBACK$1 = /* @__PURE__ */ new Map([["a", ["href"]], ["div", []]]);
769
897
  function post$1(url) {
770
- if (!url || !HOSTS$2.has(url.host)) return null;
898
+ if (!url || !HOSTS$4.has(url.host)) return null;
771
899
  const [, code] = POST_PATH.exec(url.path) ?? [];
772
900
  if (code === void 0) return null;
773
901
  return {
@@ -793,7 +921,7 @@ const threads = {
793
921
  fallbackHtml: rebuild(root.children, FALLBACK$1)
794
922
  };
795
923
  },
796
- isOwnScript: (src) => SCRIPT$2.test(src),
924
+ isOwnScript: (src) => SCRIPT$3.test(src),
797
925
  match(url) {
798
926
  const found = post$1(url);
799
927
  return found && {
@@ -803,12 +931,12 @@ const threads = {
803
931
  }
804
932
  };
805
933
  // src/parser/platforms/tiktok.ts
806
- const HOSTS$1 = /* @__PURE__ */ new Set(["tiktok.com", "www.tiktok.com"]);
934
+ const HOSTS$3 = /* @__PURE__ */ new Set(["tiktok.com", "www.tiktok.com"]);
807
935
  const VIDEO_PATH = /^\/@([A-Za-z0-9._]{0,64})\/video\/(\d{1,25})\/?$/;
808
- const PLAYER_PATH = /^\/player\/v1\/(\d{1,25})\/?$/;
936
+ const PLAYER_PATH$1 = /^\/player\/v1\/(\d{1,25})\/?$/;
809
937
  const VIDEO_ID$1 = /^\d{1,25}$/;
810
- const SCRIPT$1 = /^(?:https?:)?\/\/www\.tiktok\.com\/embed\.js$/;
811
- function video$1(id, user = "", options = {}) {
938
+ const SCRIPT$2 = /^(?:https?:)?\/\/www\.tiktok\.com\/embed\.js$/;
939
+ function video$2(id, user = "", options = {}) {
812
940
  return {
813
941
  platform: "tiktok",
814
942
  kind: "video",
@@ -823,25 +951,219 @@ const tiktok = {
823
951
  parse(root) {
824
952
  if (root.tag === "blockquote" && hasClass(root, "tiktok-embed")) {
825
953
  const cite = parseUrl(root.attrs.get("cite") ?? "");
826
- const [, user, citeId] = cite && HOSTS$1.has(cite.host) ? VIDEO_PATH.exec(cite.path) ?? [] : [];
954
+ const [, user, citeId] = cite && HOSTS$3.has(cite.host) ? VIDEO_PATH.exec(cite.path) ?? [] : [];
827
955
  const dataId = root.attrs.get("data-video-id");
828
956
  if (dataId !== void 0 && !VIDEO_ID$1.test(dataId)) return null;
829
957
  if (dataId !== void 0 && citeId !== void 0 && dataId !== citeId) return null;
830
958
  const id = dataId ?? citeId;
831
- return id === void 0 ? null : video$1(id, user);
959
+ return id === void 0 ? null : video$2(id, user);
832
960
  }
833
961
  if (root.tag !== "iframe") return null;
834
962
  const src = parseUrl(root.attrs.get("src") ?? "");
835
- if (!src || !HOSTS$1.has(src.host)) return null;
836
- const [, id] = PLAYER_PATH.exec(src.path) ?? [];
963
+ if (!src || !HOSTS$3.has(src.host)) return null;
964
+ const [, id] = PLAYER_PATH$1.exec(src.path) ?? [];
837
965
  if (id === void 0) return null;
838
- return withSize(video$1(id, "", Object.fromEntries([...src.query].map(([key, value]) => [key, coerce(value)]))), sizeOf(root));
966
+ return withSize(video$2(id, "", Object.fromEntries([...src.query].map(([key, value]) => [key, coerce(value)]))), sizeOf(root));
967
+ },
968
+ isOwnScript: (src) => SCRIPT$2.test(src),
969
+ match(url) {
970
+ if (!HOSTS$3.has(url.host)) return null;
971
+ const [, user, id] = VIDEO_PATH.exec(url.path) ?? [];
972
+ return user === void 0 || id === void 0 ? null : video$2(id, user);
973
+ }
974
+ };
975
+ // src/parser/platforms/twitch.ts
976
+ const HOSTS$2 = /* @__PURE__ */ new Set([
977
+ "twitch.tv",
978
+ "www.twitch.tv",
979
+ "m.twitch.tv"
980
+ ]);
981
+ const CHANNEL = /^[A-Za-z0-9_]{3,25}$/;
982
+ const VIDEO$1 = /^v?(\d{1,12})$/;
983
+ const COLLECTION = /^[A-Za-z0-9_-]{6,30}$/;
984
+ const CLIP = /^[A-Za-z0-9_-]{6,100}$/;
985
+ const TIME = /^(?:\d+h)?(?:\d+m)?(?:\d+s)?$/;
986
+ const RESERVED = /* @__PURE__ */ new Set([
987
+ "directory",
988
+ "videos",
989
+ "collections",
990
+ "settings",
991
+ "subscriptions",
992
+ "inventory",
993
+ "wallet",
994
+ "drops",
995
+ "search",
996
+ "downloads",
997
+ "jobs",
998
+ "turbo",
999
+ "p",
1000
+ "login",
1001
+ "signup"
1002
+ ]);
1003
+ const PLAYER_OPTIONS = [
1004
+ "autoplay",
1005
+ "muted",
1006
+ "time"
1007
+ ];
1008
+ function target(path, id) {
1009
+ return {
1010
+ platform: "twitch",
1011
+ kind: "video",
1012
+ url: path,
1013
+ id,
1014
+ variant: "iframe"
1015
+ };
1016
+ }
1017
+ const channel = (name) => target(`https://www.twitch.tv/${name.toLowerCase()}`, name.toLowerCase());
1018
+ const vod = (id) => target(`https://www.twitch.tv/videos/${id}`, id);
1019
+ const collection = (id) => target(`https://www.twitch.tv/collections/${id}`, id);
1020
+ const clip = (slug) => target(`https://clips.twitch.tv/${slug}`, slug);
1021
+ function playerOptions(query) {
1022
+ const options = {};
1023
+ for (const name of PLAYER_OPTIONS) {
1024
+ const value = query.get(name);
1025
+ if (value !== void 0) options[name] = coerce(value);
1026
+ }
1027
+ return options;
1028
+ }
1029
+ /** The player's own precedence: channel, then video, then collection. */
1030
+ function fromPlayer$1(url) {
1031
+ const name = url.query.get("channel");
1032
+ if (name !== void 0) return CHANNEL.test(name) ? channel(name) : null;
1033
+ const [, id] = VIDEO$1.exec(url.query.get("video") ?? "") ?? [];
1034
+ if (id !== void 0) return vod(id);
1035
+ const list = url.query.get("collection");
1036
+ return list !== void 0 && COLLECTION.test(list) ? collection(list) : null;
1037
+ }
1038
+ function fromPage(url) {
1039
+ if (url.host === "clips.twitch.tv") {
1040
+ const slug = url.path.replace(/^\/|\/$/g, "");
1041
+ return CLIP.test(slug) ? {
1042
+ found: clip(slug),
1043
+ options: {}
1044
+ } : null;
1045
+ }
1046
+ if (!HOSTS$2.has(url.host)) return null;
1047
+ const parts = url.path.replace(/^\/|\/$/g, "").split("/");
1048
+ const [first = "", second, third] = parts;
1049
+ if (parts.length === 2 && first === "videos") {
1050
+ const [, id] = VIDEO$1.exec(second ?? "") ?? [];
1051
+ if (id === void 0) return null;
1052
+ const time = url.query.get("t");
1053
+ return {
1054
+ found: vod(id),
1055
+ options: time !== void 0 && TIME.test(time) && time ? { time } : {}
1056
+ };
1057
+ }
1058
+ if (parts.length === 2 && first === "collections") return COLLECTION.test(second ?? "") ? {
1059
+ found: collection(second ?? ""),
1060
+ options: {}
1061
+ } : null;
1062
+ if (parts.length === 3 && second === "clip") return CHANNEL.test(first) && CLIP.test(third ?? "") ? {
1063
+ found: clip(third ?? ""),
1064
+ options: {}
1065
+ } : null;
1066
+ if (parts.length === 1 && CHANNEL.test(first) && !RESERVED.has(first.toLowerCase())) return {
1067
+ found: channel(first),
1068
+ options: {}
1069
+ };
1070
+ return null;
1071
+ }
1072
+ const twitch = {
1073
+ platform: "twitch",
1074
+ parse(root) {
1075
+ if (root.tag !== "iframe") return null;
1076
+ const src = parseUrl(root.attrs.get("src") ?? "");
1077
+ if (!src) return null;
1078
+ let found = null;
1079
+ if (src.host === "player.twitch.tv" && /^\/?$/.test(src.path)) found = fromPlayer$1(src);
1080
+ if (src.host === "clips.twitch.tv" && src.path === "/embed") {
1081
+ const slug = src.query.get("clip") ?? "";
1082
+ found = CLIP.test(slug) ? clip(slug) : null;
1083
+ }
1084
+ return found && withSize({
1085
+ ...found,
1086
+ options: playerOptions(src.query)
1087
+ }, sizeOf(root));
1088
+ },
1089
+ isOwnScript: () => false,
1090
+ match(url) {
1091
+ const page = fromPage(url);
1092
+ return page && {
1093
+ ...page.found,
1094
+ options: page.options
1095
+ };
1096
+ }
1097
+ };
1098
+ // src/parser/platforms/vimeo.ts
1099
+ const HOSTS$1 = /* @__PURE__ */ new Set(["vimeo.com", "www.vimeo.com"]);
1100
+ const VIDEO = "(\\d{1,12})";
1101
+ const HASH = "([0-9a-f]{6,20})";
1102
+ const PAGE_PATHS = [
1103
+ new RegExp(`^/${VIDEO}(?:/${HASH})?/?$`),
1104
+ new RegExp(`^/channels/[A-Za-z0-9_-]{1,64}/${VIDEO}/?$`),
1105
+ new RegExp(`^/groups/[A-Za-z0-9_-]{1,64}/videos/${VIDEO}/?$`),
1106
+ new RegExp(`^/showcase/\\d{1,12}/video/${VIDEO}/?$`)
1107
+ ];
1108
+ const PLAYER_PATH = new RegExp(`^/video/${VIDEO}/?$`);
1109
+ const WRAPPER = /^padding:\s*(\d{1,3}(?:\.\d{1,4})?)% 0 0 0;\s*position:\s*relative;?$/;
1110
+ const SCRIPT$1 = /^(?:https?:)?\/\/player\.vimeo\.com\/api\/player\.js$/;
1111
+ const DROPPED = /* @__PURE__ */ new Set([
1112
+ "h",
1113
+ "app_id",
1114
+ "player_id"
1115
+ ]);
1116
+ const START = /^t=(\d+(?:\.\d+)?|(?:\d+h)?(?:\d+m)?(?:\d+s)?)$/;
1117
+ function video$1(id, hash) {
1118
+ return {
1119
+ platform: "vimeo",
1120
+ kind: "video",
1121
+ url: `https://vimeo.com/${id}${hash ? `/${hash}` : ""}`,
1122
+ id,
1123
+ variant: "iframe"
1124
+ };
1125
+ }
1126
+ function start(url) {
1127
+ const [, time] = START.exec(url.fragment ?? "") ?? [];
1128
+ return time ? { t: time } : {};
1129
+ }
1130
+ function fromPlayer(url) {
1131
+ if (url?.host !== "player.vimeo.com") return null;
1132
+ const [, id] = PLAYER_PATH.exec(url.path) ?? [];
1133
+ if (id === void 0) return null;
1134
+ const hash = url.query.get("h");
1135
+ if (hash !== void 0 && !new RegExp(`^${HASH}$`).test(hash)) return null;
1136
+ const options = Object.fromEntries([...url.query].filter(([key]) => !DROPPED.has(key)).map(([key, value]) => [key, coerce(value)]));
1137
+ return {
1138
+ ...video$1(id, hash),
1139
+ options: {
1140
+ ...options,
1141
+ ...start(url)
1142
+ }
1143
+ };
1144
+ }
1145
+ const vimeo = {
1146
+ platform: "vimeo",
1147
+ parse(root) {
1148
+ const wrapped = unwrapRatio(root, WRAPPER);
1149
+ const iframe = wrapped?.iframe ?? root;
1150
+ if (iframe.tag !== "iframe") return null;
1151
+ const embed = fromPlayer(parseUrl(iframe.attrs.get("src") ?? ""));
1152
+ return embed && withSize(embed, wrapped?.size ?? sizeOf(iframe));
839
1153
  },
840
1154
  isOwnScript: (src) => SCRIPT$1.test(src),
841
1155
  match(url) {
1156
+ const player = fromPlayer(url);
1157
+ if (player) return player;
842
1158
  if (!HOSTS$1.has(url.host)) return null;
843
- const [, user, id] = VIDEO_PATH.exec(url.path) ?? [];
844
- return user === void 0 || id === void 0 ? null : video$1(id, user);
1159
+ for (const path of PAGE_PATHS) {
1160
+ const [, id, hash] = path.exec(url.path) ?? [];
1161
+ if (id !== void 0) return {
1162
+ ...video$1(id, hash),
1163
+ options: start(url)
1164
+ };
1165
+ }
1166
+ return null;
845
1167
  }
846
1168
  };
847
1169
  // src/parser/platforms/x.ts
@@ -1001,7 +1323,11 @@ const platforms = [
1001
1323
  snapchat,
1002
1324
  linkedin,
1003
1325
  reddit,
1004
- pinterest
1326
+ pinterest,
1327
+ spotify,
1328
+ vimeo,
1329
+ twitch,
1330
+ dailymotion
1005
1331
  ];
1006
1332
  // src/parser/match-url.ts
1007
1333
  /** Never throws. Options hold only what the URL itself carries (docs/decisions.md D13). */