social-embeds 0.9.1 → 0.9.3

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 (121) hide show
  1. package/README.md +3 -3
  2. package/dist/components/apple-embed.d.ts +2 -2
  3. package/dist/components/apple-embed.js +8 -30
  4. package/dist/components/bluesky-embed.d.ts +1 -1
  5. package/dist/components/bluesky-embed.js +6 -17
  6. package/dist/components/dailymotion-embed.d.ts +1 -1
  7. package/dist/components/dailymotion-embed.js +11 -21
  8. package/dist/components/facebook-embed.d.ts +1 -1
  9. package/dist/components/facebook-embed.js +14 -35
  10. package/dist/components/flickr-embed.d.ts +1 -1
  11. package/dist/components/flickr-embed.js +10 -21
  12. package/dist/components/giphy-embed.d.ts +1 -1
  13. package/dist/components/giphy-embed.js +5 -16
  14. package/dist/components/iframe-embed.js +15 -1
  15. package/dist/components/imgur-embed.d.ts +1 -1
  16. package/dist/components/imgur-embed.js +6 -17
  17. package/dist/components/instagram-embed.d.ts +1 -1
  18. package/dist/components/instagram-embed.js +6 -20
  19. package/dist/components/kick-embed.d.ts +1 -1
  20. package/dist/components/kick-embed.js +6 -19
  21. package/dist/components/linkedin-embed.d.ts +1 -1
  22. package/dist/components/linkedin-embed.js +6 -17
  23. package/dist/components/pinterest-embed.d.ts +1 -1
  24. package/dist/components/pinterest-embed.js +6 -20
  25. package/dist/components/reddit-embed.d.ts +1 -1
  26. package/dist/components/reddit-embed.js +13 -23
  27. package/dist/components/rumble-embed.d.ts +1 -1
  28. package/dist/components/rumble-embed.js +5 -16
  29. package/dist/components/script-embed.js +14 -1
  30. package/dist/components/snapchat-embed.d.ts +1 -1
  31. package/dist/components/snapchat-embed.js +5 -16
  32. package/dist/components/soundcloud-embed.d.ts +1 -1
  33. package/dist/components/soundcloud-embed.js +16 -27
  34. package/dist/components/spotify-embed.d.ts +1 -1
  35. package/dist/components/spotify-embed.js +6 -16
  36. package/dist/components/strava-embed.d.ts +1 -1
  37. package/dist/components/strava-embed.js +10 -20
  38. package/dist/components/streamable-embed.d.ts +1 -1
  39. package/dist/components/streamable-embed.js +12 -22
  40. package/dist/components/telegram-embed.d.ts +1 -1
  41. package/dist/components/telegram-embed.js +11 -21
  42. package/dist/components/tenor-embed.d.ts +1 -1
  43. package/dist/components/tenor-embed.js +5 -16
  44. package/dist/components/threads-embed.d.ts +1 -1
  45. package/dist/components/threads-embed.js +6 -20
  46. package/dist/components/tiktok-embed.d.ts +1 -1
  47. package/dist/components/tiktok-embed.js +21 -31
  48. package/dist/components/truthsocial-embed.d.ts +1 -1
  49. package/dist/components/truthsocial-embed.js +5 -16
  50. package/dist/components/tumblr-embed.d.ts +1 -1
  51. package/dist/components/tumblr-embed.js +5 -16
  52. package/dist/components/twitch-embed.d.ts +1 -1
  53. package/dist/components/twitch-embed.js +10 -20
  54. package/dist/components/use-plan.js +20 -0
  55. package/dist/components/vimeo-embed.d.ts +1 -1
  56. package/dist/components/vimeo-embed.js +25 -35
  57. package/dist/components/x-embed.d.ts +1 -1
  58. package/dist/components/x-embed.js +13 -27
  59. package/dist/components/youtube-embed.d.ts +1 -1
  60. package/dist/components/youtube-embed.js +28 -38
  61. package/dist/element.js +1 -1
  62. package/dist/parser/matchers/applemusic.js +33 -0
  63. package/dist/parser/matchers/applepodcasts.js +32 -0
  64. package/dist/parser/matchers/bluesky.js +41 -0
  65. package/dist/parser/matchers/dailymotion.js +47 -0
  66. package/dist/parser/matchers/facebook.js +118 -0
  67. package/dist/parser/matchers/flickr.js +100 -0
  68. package/dist/parser/matchers/giphy.js +38 -0
  69. package/dist/parser/matchers/imgur.js +76 -0
  70. package/dist/parser/matchers/instagram.js +43 -0
  71. package/dist/parser/matchers/kick.js +48 -0
  72. package/dist/parser/matchers/linkedin.js +31 -0
  73. package/dist/parser/matchers/pinterest.js +32 -0
  74. package/dist/parser/matchers/reddit.js +43 -0
  75. package/dist/parser/matchers/rumble.js +32 -0
  76. package/dist/parser/matchers/snapchat.js +60 -0
  77. package/dist/parser/matchers/soundcloud.js +87 -0
  78. package/dist/parser/matchers/spotify.js +39 -0
  79. package/dist/parser/matchers/strava.js +31 -0
  80. package/dist/parser/matchers/streamable.js +59 -0
  81. package/dist/parser/matchers/telegram.js +34 -0
  82. package/dist/parser/matchers/tenor.js +34 -0
  83. package/dist/parser/matchers/threads.js +36 -0
  84. package/dist/parser/matchers/tiktok.js +25 -0
  85. package/dist/parser/matchers/truthsocial.js +34 -0
  86. package/dist/parser/matchers/tumblr.js +51 -0
  87. package/dist/parser/matchers/twitch.js +90 -0
  88. package/dist/parser/matchers/vimeo.js +71 -0
  89. package/dist/parser/matchers/x.js +50 -0
  90. package/dist/parser/matchers/youtube.js +123 -0
  91. package/dist/parser/platform.d.ts +10 -4
  92. package/dist/parser/platforms/applemusic.js +4 -30
  93. package/dist/parser/platforms/applepodcasts.js +4 -29
  94. package/dist/parser/platforms/bluesky.js +4 -38
  95. package/dist/parser/platforms/dailymotion.js +4 -44
  96. package/dist/parser/platforms/facebook.js +5 -115
  97. package/dist/parser/platforms/flickr.js +5 -98
  98. package/dist/parser/platforms/giphy.js +4 -35
  99. package/dist/parser/platforms/imgur.js +4 -73
  100. package/dist/parser/platforms/instagram.js +4 -40
  101. package/dist/parser/platforms/kick.js +4 -45
  102. package/dist/parser/platforms/linkedin.js +4 -28
  103. package/dist/parser/platforms/pinterest.js +4 -29
  104. package/dist/parser/platforms/reddit.js +3 -40
  105. package/dist/parser/platforms/rumble.js +3 -29
  106. package/dist/parser/platforms/snapchat.js +3 -57
  107. package/dist/parser/platforms/soundcloud.js +4 -84
  108. package/dist/parser/platforms/spotify.js +4 -36
  109. package/dist/parser/platforms/strava.js +3 -28
  110. package/dist/parser/platforms/streamable.js +3 -56
  111. package/dist/parser/platforms/telegram.js +3 -31
  112. package/dist/parser/platforms/tenor.js +5 -32
  113. package/dist/parser/platforms/threads.js +4 -33
  114. package/dist/parser/platforms/tiktok.js +4 -22
  115. package/dist/parser/platforms/truthsocial.js +3 -31
  116. package/dist/parser/platforms/tumblr.js +3 -48
  117. package/dist/parser/platforms/twitch.js +4 -87
  118. package/dist/parser/platforms/vimeo.js +4 -68
  119. package/dist/parser/platforms/x.js +6 -46
  120. package/dist/parser/platforms/youtube.js +5 -120
  121. package/package.json +1 -1
@@ -0,0 +1,123 @@
1
+ import { underPath } from "../platform.js";
2
+ import { YOUTUBE_HOSTS, YOUTUBE_NOCOOKIE_HOSTS, youtubeFrame } from "../frames.js";
3
+ // src/parser/matchers/youtube.ts
4
+ const BASE = "https://www.youtube.com";
5
+ const VIDEO_ID = /^(?!videoseries|live_stream)[A-Za-z0-9_-]{11}$/;
6
+ const EMBED_PATH = /^\/embed\/(?:(videoseries|live_stream)|([A-Za-z0-9_-]{11}))\/?$/;
7
+ const URL_PATHS = /^\/(shorts|live)\/((?!videoseries|live_stream)[A-Za-z0-9_-]{11})\/?$/;
8
+ const SHORT_LINK_PATH = /^\/([A-Za-z0-9_-]{11})\/?$/;
9
+ const PLAYLIST_ID = /^[A-Za-z0-9_-]{10,64}$/;
10
+ const CHANNEL_ID = /^UC[A-Za-z0-9_-]{22}$/;
11
+ const CHANNEL_LIVE_PATH = /^\/channel\/(UC[A-Za-z0-9_-]{22})\/live\/?$/;
12
+ const DURATION = /^(?:(\d{1,5})h)?(?:(\d{1,6})m)?(?:(\d{1,7})s?)?$/;
13
+ const FRAGMENT_START = /^t=(.+)$/;
14
+ /** `90`, `90s`, `1m30s`, `1h2m3s` → seconds. */
15
+ function seconds(value) {
16
+ if (value === void 0 || value === "") return void 0;
17
+ const match = DURATION.exec(value);
18
+ if (!match) return void 0;
19
+ const [, hours = "0", minutes = "0", secs = "0"] = match;
20
+ return Number(hours) * 3600 + Number(minutes) * 60 + Number(secs);
21
+ }
22
+ /** `start` wins over `t`; `t` is converted, because the embed player only reads `start`. */
23
+ function startOf(query) {
24
+ return seconds(query.get("start")) ?? seconds(query.get("t"));
25
+ }
26
+ function found(id, url) {
27
+ return {
28
+ platform: "youtube",
29
+ kind: "video",
30
+ url,
31
+ id,
32
+ variant: "iframe"
33
+ };
34
+ }
35
+ function video(id, shorts = false) {
36
+ return found(id, shorts ? `${BASE}/shorts/${id}` : `${BASE}/watch?v=${id}`);
37
+ }
38
+ function playlist(id) {
39
+ return id !== void 0 && PLAYLIST_ID.test(id) ? found(id, `${BASE}/playlist?list=${id}`) : null;
40
+ }
41
+ function live(id) {
42
+ return id !== void 0 && CHANNEL_ID.test(id) ? found(id, `${BASE}/channel/${id}/live`) : null;
43
+ }
44
+ /** `key` is the query parameter that names the content, for a playlist or a channel's stream. */
45
+ function fromEmbed(url) {
46
+ const [, series, id] = EMBED_PATH.exec(url.path) ?? [];
47
+ if (id !== void 0) return { found: video(id) };
48
+ if (series === "videoseries") {
49
+ const list = playlist(url.query.get("list"));
50
+ return list && {
51
+ found: list,
52
+ key: "list"
53
+ };
54
+ }
55
+ if (series === "live_stream") {
56
+ const stream = live(url.query.get("channel"));
57
+ return stream && {
58
+ found: stream,
59
+ key: "channel"
60
+ };
61
+ }
62
+ return null;
63
+ }
64
+ function urlOptions(url, nocookie) {
65
+ const start = startOf(url.query) ?? seconds(FRAGMENT_START.exec(url.fragment ?? "")?.[1]);
66
+ return {
67
+ ...start === void 0 ? {} : { start },
68
+ ...nocookie ? { nocookie: true } : {}
69
+ };
70
+ }
71
+ const youtubeMatcher = {
72
+ platform: "youtube",
73
+ name: "YouTube",
74
+ shapes: ["video iframe"],
75
+ match(url) {
76
+ if (url.host === "youtu.be") {
77
+ const [, id] = SHORT_LINK_PATH.exec(url.path) ?? [];
78
+ return id === void 0 ? null : {
79
+ ...video(id),
80
+ options: urlOptions(url, false)
81
+ };
82
+ }
83
+ const nocookie = YOUTUBE_NOCOOKIE_HOSTS.has(url.host);
84
+ if (!nocookie && !YOUTUBE_HOSTS.has(url.host)) return null;
85
+ const options = urlOptions(url, nocookie);
86
+ if (underPath(url.path, "/embed")) {
87
+ const player = fromEmbed(url);
88
+ return player && {
89
+ ...player.found,
90
+ options
91
+ };
92
+ }
93
+ if (nocookie) return null;
94
+ if (/^\/watch\/?$/.test(url.path)) {
95
+ const id = url.query.get("v");
96
+ return id !== void 0 && VIDEO_ID.test(id) ? {
97
+ ...video(id),
98
+ options
99
+ } : null;
100
+ }
101
+ if (/^\/playlist\/?$/.test(url.path)) {
102
+ const list = playlist(url.query.get("list"));
103
+ return list && {
104
+ ...list,
105
+ options
106
+ };
107
+ }
108
+ const [, channel] = CHANNEL_LIVE_PATH.exec(url.path) ?? [];
109
+ const stream = live(channel);
110
+ if (stream) return {
111
+ ...stream,
112
+ options
113
+ };
114
+ const [, kind, id] = URL_PATHS.exec(url.path) ?? [];
115
+ if (kind === void 0 || id === void 0) return null;
116
+ return {
117
+ ...video(id, kind === "shorts"),
118
+ options
119
+ };
120
+ },
121
+ ownsFrame: youtubeFrame
122
+ };
123
+ export { fromEmbed, startOf, youtubeMatcher };
@@ -2,7 +2,11 @@ import { HtmlElement } from "./html.js";
2
2
  import { ParsedEmbed, Platform } from "./types.js";
3
3
  import { ParsedUrl } from "./url.js";
4
4
  export type Options = ParsedEmbed['options'];
5
- export type PlatformParser = {
5
+ /**
6
+ * The URL half of a platform: what a component needs to match a URL and verify an embed, without
7
+ * the embed-code parsing that only `parseEmbed` uses (docs/decisions.md D117).
8
+ */
9
+ export type PlatformMatcher = {
6
10
  platform: Platform;
7
11
  /** For messages: `YouTube`, `Apple Music`. */
8
12
  name: string;
@@ -12,6 +16,11 @@ export type PlatformParser = {
12
16
  trustsFallback?: (html: string, embed: ParsedEmbed) => boolean;
13
17
  /** A post URL that only a network lookup could turn into an embed (docs/decisions.md D70). */
14
18
  needsLookup?: (url: ParsedUrl) => boolean;
19
+ match: (url: ParsedUrl) => ParsedEmbed | null;
20
+ /** True also for frames `parse` rejects, so generic iframe code never bypasses the platform's checks. */
21
+ ownsFrame?: (src: ParsedUrl) => boolean;
22
+ };
23
+ export type PlatformParser = PlatformMatcher & {
15
24
  /** The embed element (blockquote, div or iframe) of the platform's own embed code. */
16
25
  parse: (root: HtmlElement) => ParsedEmbed | null;
17
26
  /** Embed code that is a single `<script>` tag, as Telegram's is. */
@@ -26,7 +35,4 @@ export type PlatformParser = {
26
35
  allowsFbRoot?: boolean;
27
36
  /** A sibling element after the embed that is the platform's own markup, such as a credit; it is dropped. */
28
37
  isOwnExtra?: (element: HtmlElement, embed: ParsedEmbed) => boolean;
29
- match: (url: ParsedUrl) => ParsedEmbed | null;
30
- /** True also for frames `parse` rejects, so generic iframe code never bypasses the platform's checks. */
31
- ownsFrame?: (src: ParsedUrl) => boolean;
32
38
  };
@@ -1,27 +1,10 @@
1
1
  import { sizeOf, withSize } from "../platform.js";
2
2
  import { parseUrl } from "../url.js";
3
- import { APPLE_MUSIC_FRAME_HOST, appleMusicFrame } from "../frames.js";
3
+ import { APPLE_MUSIC_FRAME_HOST } from "../frames.js";
4
+ import { appleContent, applemusicMatcher } from "../matchers/applemusic.js";
4
5
  // src/parser/platforms/applemusic.ts
5
- const PATH = /^\/([a-z]{2})\/(album|song|playlist)\/(?:[^/]{1,200}\/)?(\d{1,15}|pl\.[A-Za-z0-9-]{1,64})\/?$/;
6
- const SONG = /^\d{1,15}$/;
7
- function appleContent(url, host) {
8
- if (url?.host !== host) return null;
9
- const [, country, type, id] = PATH.exec(url.path) ?? [];
10
- if (country === void 0 || type === void 0 || id === void 0) return null;
11
- if (type === "playlist" ? !id.startsWith("pl.") : !SONG.test(id)) return null;
12
- const song = type === "album" ? url.query.get("i") : void 0;
13
- if (song !== void 0 && !SONG.test(song)) return null;
14
- return {
15
- kind: "post",
16
- url: `https://music.apple.com/${country}/${type}/${id}${song ? `?i=${song}` : ""}`,
17
- id: song ?? id,
18
- variant: "iframe"
19
- };
20
- }
21
6
  const applemusic = {
22
- platform: "applemusic",
23
- name: "Apple Music",
24
- shapes: ["post iframe"],
7
+ ...applemusicMatcher,
25
8
  parse(root) {
26
9
  if (root.tag !== "iframe") return null;
27
10
  const found = appleContent(parseUrl(root.attrs.get("src") ?? ""), APPLE_MUSIC_FRAME_HOST);
@@ -33,15 +16,6 @@ const applemusic = {
33
16
  options: {}
34
17
  }, height === void 0 ? void 0 : { height });
35
18
  },
36
- isOwnScript: () => false,
37
- match(url) {
38
- const found = appleContent(url, "music.apple.com");
39
- return found && {
40
- platform: "applemusic",
41
- ...found,
42
- options: {}
43
- };
44
- },
45
- ownsFrame: appleMusicFrame
19
+ isOwnScript: () => false
46
20
  };
47
21
  export { applemusic };
@@ -1,27 +1,10 @@
1
1
  import { sizeOf, withSize } from "../platform.js";
2
2
  import { parseUrl } from "../url.js";
3
- import { APPLE_PODCASTS_FRAME_HOST, applePodcastsFrame } from "../frames.js";
3
+ import { APPLE_PODCASTS_FRAME_HOST } from "../frames.js";
4
+ import { applepodcastsMatcher, content } from "../matchers/applepodcasts.js";
4
5
  // src/parser/platforms/applepodcasts.ts
5
- const PATH = /^\/([a-z]{2})\/podcast\/(?:[^/]{1,200}\/)?(?:id)?(\d{1,15})\/?$/;
6
- const EPISODE = /^\d{1,15}$/;
7
- function content(url, host) {
8
- if (url?.host !== host) return null;
9
- const [, country, show] = PATH.exec(url.path) ?? [];
10
- if (country === void 0 || show === void 0) return null;
11
- const episode = url.query.get("i");
12
- if (episode !== void 0 && !EPISODE.test(episode)) return null;
13
- return {
14
- platform: "applepodcasts",
15
- kind: "post",
16
- url: `https://podcasts.apple.com/${country}/podcast/id${show}${episode ? `?i=${episode}` : ""}`,
17
- id: episode ?? show,
18
- variant: "iframe"
19
- };
20
- }
21
6
  const applepodcasts = {
22
- platform: "applepodcasts",
23
- name: "Apple Podcasts",
24
- shapes: ["post iframe"],
7
+ ...applepodcastsMatcher,
25
8
  parse(root) {
26
9
  if (root.tag !== "iframe") return null;
27
10
  const found = content(parseUrl(root.attrs.get("src") ?? ""), APPLE_PODCASTS_FRAME_HOST);
@@ -30,14 +13,6 @@ const applepodcasts = {
30
13
  options: {}
31
14
  }, sizeOf(root));
32
15
  },
33
- isOwnScript: () => false,
34
- match(url) {
35
- const found = content(url, "podcasts.apple.com");
36
- return found && {
37
- ...found,
38
- options: {}
39
- };
40
- },
41
- ownsFrame: applePodcastsFrame
16
+ isOwnScript: () => false
42
17
  };
43
18
  export { applepodcasts };
@@ -1,11 +1,7 @@
1
- import { blueskyNeedsLookup } from "../lookup.js";
2
- import { descendants, hasClass, rebuild, rebuildsAs } from "../html.js";
1
+ import { descendants, hasClass, rebuild } from "../html.js";
3
2
  import { parseUrl } from "../url.js";
3
+ import { DID, FALLBACK, RKEY, blueskyMatcher, fromUrl, post } from "../matchers/bluesky.js";
4
4
  // src/parser/platforms/bluesky.ts
5
- const DID = "(did:(?:plc:[a-z2-7]{24}|web:[a-z0-9.-]{1,253}))";
6
- const RKEY = "((?!\\.{1,2}(?:/|$))[A-Za-z0-9._~:-]{1,512})";
7
- const HOSTS = /* @__PURE__ */ new Set(["bsky.app", "www.bsky.app"]);
8
- const POST_PATH = new RegExp(`^/profile/${DID}/post/${RKEY}/?$`);
9
5
  const AT_URI = new RegExp(`^at://${DID}/app\\.bsky\\.feed\\.post/${RKEY}$`);
10
6
  const CID = /^[a-z2-7]{20,100}$/;
11
7
  const COLOR_MODES = /* @__PURE__ */ new Set([
@@ -14,31 +10,8 @@ const COLOR_MODES = /* @__PURE__ */ new Set([
14
10
  "system"
15
11
  ]);
16
12
  const SCRIPT = /^(?:https?:)?\/\/embed\.bsky\.app\/static\/embed\.js$/;
17
- const FALLBACK = /* @__PURE__ */ new Map([
18
- ["p", ["lang"]],
19
- ["a", ["href"]],
20
- ["br", []]
21
- ]);
22
- function post(did, rkey) {
23
- return {
24
- platform: "bluesky",
25
- kind: "post",
26
- url: `https://bsky.app/profile/${did}/post/${rkey}`,
27
- id: `${did}/app.bsky.feed.post/${rkey}`,
28
- variant: "iframe"
29
- };
30
- }
31
- function fromUrl(url) {
32
- if (!url || !HOSTS.has(url.host)) return null;
33
- const [, did, rkey] = POST_PATH.exec(url.path) ?? [];
34
- return did === void 0 || rkey === void 0 ? null : post(did, rkey);
35
- }
36
13
  const bluesky = {
37
- platform: "bluesky",
38
- name: "Bluesky",
39
- shapes: ["post iframe"],
40
- needsLookup: blueskyNeedsLookup,
41
- trustsFallback: (html) => rebuildsAs(html, FALLBACK),
14
+ ...blueskyMatcher,
42
15
  parse(root) {
43
16
  if (root.tag !== "blockquote" || !hasClass(root, "bluesky-embed")) return null;
44
17
  const [, did, rkey] = AT_URI.exec(root.attrs.get("data-bluesky-uri") ?? "") ?? [];
@@ -56,13 +29,6 @@ const bluesky = {
56
29
  fallbackHtml: rebuild(root.children, FALLBACK)
57
30
  };
58
31
  },
59
- isOwnScript: (src) => SCRIPT.test(src),
60
- match(url) {
61
- const found = fromUrl(url);
62
- return found && {
63
- ...found,
64
- options: {}
65
- };
66
- }
32
+ isOwnScript: (src) => SCRIPT.test(src)
67
33
  };
68
34
  export { bluesky };
@@ -1,46 +1,10 @@
1
1
  import { sizeOf, unwrapRatio, withSize } from "../platform.js";
2
- import { coerce, parseUrl } from "../url.js";
3
- import { DAILYMOTION_HOSTS, dailymotionFrame } from "../frames.js";
2
+ import { parseUrl } from "../url.js";
3
+ import { dailymotionMatcher, fromPage, fromPlayer } from "../matchers/dailymotion.js";
4
4
  // src/parser/platforms/dailymotion.ts
5
- const VIDEO_ID = /^x[a-z0-9]{4,12}$/;
6
- const PLAYER_ID = /^x[a-z0-9]{3,12}$/;
7
5
  const WRAPPER = /^position:\s*relative;\s*padding-bottom:\s*(\d{1,3}(?:\.\d{1,4})?)%;\s*height:\s*0;\s*overflow:\s*hidden;?$/;
8
- function video(id, options) {
9
- return {
10
- platform: "dailymotion",
11
- kind: "video",
12
- url: `https://www.dailymotion.com/video/${id}`,
13
- id,
14
- variant: "iframe",
15
- options
16
- };
17
- }
18
- /** A geo.dailymotion.com player, with or without a Player ID. Playlists are left out. */
19
- function fromPlayer(url) {
20
- if (url.host !== "geo.dailymotion.com" || url.query.has("playlist")) return null;
21
- const match = /^\/player(?:\/([a-z0-9]+))?\.html$/.exec(url.path);
22
- if (!match) return null;
23
- const [, playerId] = match;
24
- const id = url.query.get("video") ?? "";
25
- if (!VIDEO_ID.test(id)) return null;
26
- if (playerId !== void 0 && !PLAYER_ID.test(playerId)) return null;
27
- const options = Object.fromEntries([...url.query].filter(([key]) => key !== "video").map(([key, value]) => [key, coerce(value)]));
28
- if (playerId !== void 0) options.playerId = playerId;
29
- return video(id, options);
30
- }
31
- function fromPage(url) {
32
- if (url.host === "dai.ly") {
33
- const id = url.path.replace(/^\/|\/$/g, "");
34
- return VIDEO_ID.test(id) ? video(id, {}) : null;
35
- }
36
- if (!DAILYMOTION_HOSTS.has(url.host)) return null;
37
- const [, id] = /^(?:\/embed)?\/video\/([a-z0-9]+)\/?$/.exec(url.path) ?? [];
38
- return id !== void 0 && VIDEO_ID.test(id) ? video(id, {}) : null;
39
- }
40
6
  const dailymotion = {
41
- platform: "dailymotion",
42
- name: "Dailymotion",
43
- shapes: ["video iframe"],
7
+ ...dailymotionMatcher,
44
8
  parse(root) {
45
9
  const wrapped = unwrapRatio(root, WRAPPER);
46
10
  const iframe = wrapped?.iframe ?? root;
@@ -50,10 +14,6 @@ const dailymotion = {
50
14
  const embed = fromPlayer(src) ?? fromPage(src);
51
15
  return embed && withSize(embed, wrapped?.size ?? sizeOf(iframe));
52
16
  },
53
- isOwnScript: () => false,
54
- match(url) {
55
- return fromPlayer(url) ?? fromPage(url);
56
- },
57
- ownsFrame: dailymotionFrame
17
+ isOwnScript: () => false
58
18
  };
59
19
  export { dailymotion };
@@ -1,29 +1,9 @@
1
- import { hasClass, rebuild, rebuildsAs } from "../html.js";
1
+ import { hasClass, rebuild } from "../html.js";
2
2
  import { attributeOptions, sizeOf, withSize } from "../platform.js";
3
3
  import { coerce, parseUrl, pixels } from "../url.js";
4
- import { FACEBOOK_PLUGIN_HOSTS, facebookFrame } from "../frames.js";
4
+ import { FACEBOOK_PLUGIN_HOSTS } from "../frames.js";
5
+ import { FALLBACK, embed, facebookMatcher, target } from "../matchers/facebook.js";
5
6
  // src/parser/platforms/facebook.ts
6
- const HOSTS = /* @__PURE__ */ new Set([
7
- "facebook.com",
8
- "www.facebook.com",
9
- "web.facebook.com",
10
- "m.facebook.com"
11
- ]);
12
- const BASE = "https://www.facebook.com";
13
- const NAME = "[A-Za-z0-9.\\-]{1,80}";
14
- const POST_ID = "(?:\\d{1,25}|pfbid[A-Za-z0-9]{10,120})";
15
- const POSTS_PATH = new RegExp(`^/(${NAME})/posts/(${POST_ID})/?$`);
16
- const PHOTOS_PATH = new RegExp(`^/(${NAME})/photos/([A-Za-z0-9.\\-]{1,80})/(\\d{1,25})/?$`);
17
- const VIDEOS_PATH = new RegExp(`^/(${NAME})/videos/(?:[^/]{1,200}/)?(\\d{1,25})/?$`);
18
- const REEL_PATH = /^\/reel\/(\d{1,25})\/?$/;
19
- const STORY_PATHS = /* @__PURE__ */ new Set(["/permalink.php", "/story.php"]);
20
- const PHOTO_PATHS = /* @__PURE__ */ new Set([
21
- "/photo.php",
22
- "/photo/",
23
- "/photo"
24
- ]);
25
- const STORY_ID = new RegExp(`^${POST_ID}$`);
26
- const NUMERIC_ID = /^\d{1,25}$/;
27
7
  const SCRIPT = /^(?:https?:)?\/\/connect\.facebook\.net\/([a-z]{2,3}_[A-Z]{2})\/sdk\.js(?:[?#].*)?$/;
28
8
  const OPTION_ATTRIBUTES = [
29
9
  ["data-width", "width"],
@@ -39,93 +19,8 @@ const VIDEO_SDK_PARAMETERS = [
39
19
  ["autoplay", "autoplay"],
40
20
  ["show_captions", "showCaptions"]
41
21
  ];
42
- const PARSE_IGNORE = {
43
- name: "class",
44
- value: /^fb-xfbml-parse-ignore$/
45
- };
46
- const FALLBACK = /* @__PURE__ */ new Map([
47
- ["div", [PARSE_IGNORE]],
48
- ["blockquote", ["cite", PARSE_IGNORE]],
49
- ["a", ["href"]],
50
- ["p", []]
51
- ]);
52
- function target(url) {
53
- if (!url || !HOSTS.has(url.host)) return null;
54
- const { path, query } = url;
55
- const [, name, postId] = POSTS_PATH.exec(path) ?? [];
56
- if (name !== void 0 && postId !== void 0) return {
57
- kind: "post",
58
- id: postId,
59
- url: `${BASE}/${name}/posts/${postId}`
60
- };
61
- if (STORY_PATHS.has(path)) {
62
- const storyId = query.get("story_fbid");
63
- if (storyId === void 0 || !STORY_ID.test(storyId)) return null;
64
- const owner = query.get("id");
65
- const ownerQuery = owner !== void 0 && NUMERIC_ID.test(owner) ? `&id=${owner}` : "";
66
- return {
67
- kind: "post",
68
- id: storyId,
69
- url: `${BASE}${path}?story_fbid=${storyId}${ownerQuery}`
70
- };
71
- }
72
- if (PHOTO_PATHS.has(path)) {
73
- const fbid = query.get("fbid");
74
- if (fbid === void 0 || !NUMERIC_ID.test(fbid)) return null;
75
- return {
76
- kind: "post",
77
- id: fbid,
78
- url: `${BASE}${path}?fbid=${fbid}`
79
- };
80
- }
81
- const [, owner, album, photoId] = PHOTOS_PATH.exec(path) ?? [];
82
- if (owner !== void 0 && album !== void 0 && photoId !== void 0) return {
83
- kind: "post",
84
- id: photoId,
85
- url: `${BASE}/${owner}/photos/${album}/${photoId}/`
86
- };
87
- const [, page, videoId] = VIDEOS_PATH.exec(path) ?? [];
88
- if (page !== void 0 && videoId !== void 0) return {
89
- kind: "video",
90
- id: videoId,
91
- url: `${BASE}/${page}/videos/${videoId}/`
92
- };
93
- if (path === "/watch" || path === "/watch/" || path === "/video.php") {
94
- const v = query.get("v");
95
- if (v === void 0 || !NUMERIC_ID.test(v)) return null;
96
- return {
97
- kind: "video",
98
- id: v,
99
- url: `${BASE}${path === "/video.php" ? path : "/watch/"}?v=${v}`
100
- };
101
- }
102
- const [, reelId] = REEL_PATH.exec(path) ?? [];
103
- if (reelId !== void 0) return {
104
- kind: "video",
105
- id: reelId,
106
- url: `${BASE}/reel/${reelId}`
107
- };
108
- return null;
109
- }
110
- function embed(found, kind, variant, options) {
111
- return {
112
- platform: "facebook",
113
- kind,
114
- url: found.url,
115
- id: found.id,
116
- variant,
117
- options
118
- };
119
- }
120
22
  const facebook = {
121
- platform: "facebook",
122
- name: "Facebook",
123
- shapes: [
124
- "post sdk",
125
- "video sdk",
126
- "video iframe"
127
- ],
128
- trustsFallback: (html) => rebuildsAs(html, FALLBACK),
23
+ ...facebookMatcher,
129
24
  parse(root) {
130
25
  if (root.tag === "div") {
131
26
  const kind = hasClass(root, "fb-post") ? "post" : hasClass(root, "fb-video") ? "video" : null;
@@ -177,11 +72,6 @@ const facebook = {
177
72
  const [, locale] = SCRIPT.exec(src) ?? [];
178
73
  return locale === void 0 ? {} : { locale };
179
74
  },
180
- allowsFbRoot: true,
181
- match(url) {
182
- const found = target(url);
183
- return found && embed(found, found.kind, "sdk", {});
184
- },
185
- ownsFrame: facebookFrame
75
+ allowsFbRoot: true
186
76
  };
187
77
  export { facebook };
@@ -1,97 +1,14 @@
1
- import { rebuild, rebuildsAs } from "../html.js";
1
+ import { rebuild } from "../html.js";
2
2
  import { sizeOf, withSize } from "../platform.js";
3
3
  import { parseUrl } from "../url.js";
4
+ import { FALLBACK, IMAGE_SRC, flickrMatcher, fromLongUrl } from "../matchers/flickr.js";
4
5
  // src/parser/platforms/flickr.ts
5
- const HOSTS = /* @__PURE__ */ new Set(["flickr.com", "www.flickr.com"]);
6
- const USER = "((?!\\.{1,2}(?:/|$))[A-Za-z0-9@_.-]{1,64})";
7
- const ID = "(\\d{1,20})";
8
- const PHOTO_PATH = new RegExp(`^/photos/${USER}/${ID}(?:/in/[A-Za-z0-9_@./-]{1,200})?/?$`);
9
- const ALBUM_PATH = new RegExp(`^/photos/${USER}/(?:albums|sets)/${ID}/?$`);
10
- const SHORT_PATH = /^\/p\/([1-9a-km-zA-HJ-NP-Z]{1,12})$/;
11
6
  const SCRIPT = /^(?:https?:)?\/\/embedr\.flickr\.com\/assets\/client-code\.js$/;
12
- const ALPHABET = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
13
7
  const OPTIONS = [
14
8
  "header",
15
9
  "footer",
16
10
  "context"
17
11
  ];
18
- const IMAGE_SRC = /^https:\/\/(?:live|farm\d{1,3})\.staticflickr\.com\//;
19
- const FALLBACK = /* @__PURE__ */ new Map([["a", [{
20
- name: "href",
21
- value: /^https:\/\/(?:www\.)?flickr\.com\//
22
- }]], ["img", [
23
- {
24
- name: "src",
25
- value: IMAGE_SRC
26
- },
27
- {
28
- name: "width",
29
- value: /^\d{1,6}$/
30
- },
31
- {
32
- name: "height",
33
- value: /^\d{1,6}$/
34
- },
35
- "alt"
36
- ]]]);
37
- /** The photo id of a `flic.kr/p/{code}` code, or `null` for an empty code or a character outside Flickr's base-58 alphabet. */
38
- function decodeFlickrCode(code) {
39
- if (code === "") return null;
40
- let id = 0n;
41
- for (const character of code) {
42
- const digit = ALPHABET.indexOf(character);
43
- if (digit === -1) return null;
44
- id = id * 58n + BigInt(digit);
45
- }
46
- return id.toString();
47
- }
48
- function encodeFlickrCode(id) {
49
- let rest = BigInt(id);
50
- let code = "";
51
- while (rest > 0n) {
52
- code = ALPHABET.charAt(Number(rest % 58n)) + code;
53
- rest /= 58n;
54
- }
55
- return code;
56
- }
57
- function photo(user, id) {
58
- return {
59
- platform: "flickr",
60
- kind: "post",
61
- url: `https://www.flickr.com/photos/${user}/${id}/`,
62
- id,
63
- variant: "iframe"
64
- };
65
- }
66
- function album(user, id) {
67
- return {
68
- platform: "flickr",
69
- kind: "post",
70
- url: `https://www.flickr.com/photos/${user}/albums/${id}`,
71
- id,
72
- variant: "iframe"
73
- };
74
- }
75
- function fromLongUrl(url) {
76
- if (!url || !HOSTS.has(url.host)) return null;
77
- const [, albumUser, albumId] = ALBUM_PATH.exec(url.path) ?? [];
78
- if (albumUser !== void 0 && albumId !== void 0) return album(albumUser, albumId);
79
- const [, user, id] = PHOTO_PATH.exec(url.path) ?? [];
80
- return user === void 0 || id === void 0 ? null : photo(user, id);
81
- }
82
- function fromShortUrl(url) {
83
- if (url.host !== "flic.kr") return null;
84
- const [, code] = SHORT_PATH.exec(url.path) ?? [];
85
- const id = code === void 0 ? null : decodeFlickrCode(code);
86
- if (code === void 0 || id === null || encodeFlickrCode(id) !== code) return null;
87
- return {
88
- platform: "flickr",
89
- kind: "post",
90
- url: `https://flic.kr/p/${code}`,
91
- id,
92
- variant: "iframe"
93
- };
94
- }
95
12
  function onlyImage(root) {
96
13
  let image = null;
97
14
  for (const child of root.children) {
@@ -102,10 +19,7 @@ function onlyImage(root) {
102
19
  return image;
103
20
  }
104
21
  const flickr = {
105
- platform: "flickr",
106
- name: "Flickr",
107
- shapes: ["post iframe"],
108
- trustsFallback: (html) => rebuildsAs(html, FALLBACK),
22
+ ...flickrMatcher,
109
23
  parse(root) {
110
24
  if (root.tag !== "a" || root.attrs.get("data-flickr-embed") !== "true") return null;
111
25
  const found = fromLongUrl(parseUrl(root.attrs.get("href") ?? ""));
@@ -124,13 +38,6 @@ const flickr = {
124
38
  fallbackHtml: rebuild([linked], FALLBACK)
125
39
  }, size?.width !== void 0 && size.height !== void 0 ? size : void 0);
126
40
  },
127
- isOwnScript: (src) => SCRIPT.test(src),
128
- match(url) {
129
- const found = fromLongUrl(url) ?? fromShortUrl(url);
130
- return found && {
131
- ...found,
132
- options: {}
133
- };
134
- }
41
+ isOwnScript: (src) => SCRIPT.test(src)
135
42
  };
136
- export { decodeFlickrCode, flickr };
43
+ export { flickr };