social-embeds 0.7.1 → 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.
Files changed (176) hide show
  1. package/README.md +63 -51
  2. package/dist/components/apple-embed.d.ts +7 -0
  3. package/dist/components/apple-embed.js +39 -0
  4. package/dist/components/bluesky-embed.d.ts +10 -0
  5. package/dist/components/bluesky-embed.js +27 -0
  6. package/dist/components/dailymotion-embed.d.ts +9 -0
  7. package/dist/components/dailymotion-embed.js +30 -0
  8. package/dist/components/facebook-embed.d.ts +6 -0
  9. package/dist/components/facebook-embed.js +40 -0
  10. package/dist/components/flickr-embed.d.ts +6 -0
  11. package/dist/components/flickr-embed.js +27 -0
  12. package/dist/components/generic-iframe-embed.d.ts +11 -0
  13. package/dist/components/generic-iframe-embed.js +26 -0
  14. package/dist/components/giphy-embed.d.ts +4 -0
  15. package/dist/components/giphy-embed.js +22 -0
  16. package/dist/components/hooks.js +38 -0
  17. package/dist/components/iframe-embed.js +169 -0
  18. package/dist/components/imgur-embed.d.ts +6 -0
  19. package/dist/components/imgur-embed.js +23 -0
  20. package/dist/components/instagram-embed.d.ts +5 -0
  21. package/dist/components/instagram-embed.js +25 -0
  22. package/dist/components/kick-embed.d.ts +6 -0
  23. package/dist/components/kick-embed.js +25 -0
  24. package/dist/components/linkedin-embed.d.ts +6 -0
  25. package/dist/components/linkedin-embed.js +23 -0
  26. package/dist/components/pinterest-embed.d.ts +5 -0
  27. package/dist/components/pinterest-embed.js +25 -0
  28. package/dist/components/reddit-embed.d.ts +6 -0
  29. package/dist/components/reddit-embed.js +29 -0
  30. package/dist/components/rumble-embed.d.ts +4 -0
  31. package/dist/components/rumble-embed.js +22 -0
  32. package/dist/components/script-embed.js +108 -0
  33. package/dist/components/shared.d.ts +29 -0
  34. package/dist/components/snapchat-embed.d.ts +4 -0
  35. package/dist/components/snapchat-embed.js +22 -0
  36. package/dist/components/social-embed.d.ts +60 -0
  37. package/dist/components/social-embed.js +221 -0
  38. package/dist/components/soundcloud-embed.d.ts +6 -0
  39. package/dist/components/soundcloud-embed.js +33 -0
  40. package/dist/components/spotify-embed.d.ts +9 -0
  41. package/dist/components/spotify-embed.js +25 -0
  42. package/dist/components/strava-embed.d.ts +6 -0
  43. package/dist/components/strava-embed.js +26 -0
  44. package/dist/components/streamable-embed.d.ts +6 -0
  45. package/dist/components/streamable-embed.js +28 -0
  46. package/dist/components/telegram-embed.d.ts +6 -0
  47. package/dist/components/telegram-embed.js +27 -0
  48. package/dist/components/tenor-embed.d.ts +4 -0
  49. package/dist/components/tenor-embed.js +22 -0
  50. package/dist/components/threads-embed.d.ts +5 -0
  51. package/dist/components/threads-embed.js +25 -0
  52. package/dist/components/tiktok-embed.d.ts +6 -0
  53. package/dist/components/tiktok-embed.js +37 -0
  54. package/dist/components/truthsocial-embed.d.ts +4 -0
  55. package/dist/components/truthsocial-embed.js +22 -0
  56. package/dist/components/tumblr-embed.d.ts +4 -0
  57. package/dist/components/tumblr-embed.js +22 -0
  58. package/dist/components/twitch-embed.d.ts +6 -0
  59. package/dist/components/twitch-embed.js +26 -0
  60. package/dist/components/vimeo-embed.d.ts +5 -0
  61. package/dist/components/vimeo-embed.js +40 -0
  62. package/dist/components/x-embed.d.ts +5 -0
  63. package/dist/components/x-embed.js +32 -0
  64. package/dist/components/youtube-embed.d.ts +5 -0
  65. package/dist/components/youtube-embed.js +43 -0
  66. package/dist/core/drivers.js +125 -0
  67. package/dist/core/layout.js +73 -0
  68. package/dist/core/markup.js +30 -0
  69. package/dist/core/params.js +26 -0
  70. package/dist/core/plans/apple.js +44 -0
  71. package/dist/core/plans/bluesky.d.ts +3 -0
  72. package/dist/core/plans/bluesky.js +34 -0
  73. package/dist/core/plans/dailymotion.d.ts +8 -0
  74. package/dist/core/plans/dailymotion.js +37 -0
  75. package/dist/core/plans/facebook.d.ts +15 -0
  76. package/dist/core/plans/facebook.js +79 -0
  77. package/dist/core/plans/flickr.d.ts +7 -0
  78. package/dist/core/plans/flickr.js +37 -0
  79. package/dist/core/plans/generic.js +44 -0
  80. package/dist/core/plans/giphy.js +16 -0
  81. package/dist/core/plans/imgur.d.ts +4 -0
  82. package/dist/core/plans/imgur.js +34 -0
  83. package/dist/core/plans/instagram.d.ts +4 -0
  84. package/dist/core/plans/instagram.js +26 -0
  85. package/dist/core/plans/kick.d.ts +5 -0
  86. package/dist/core/plans/kick.js +22 -0
  87. package/dist/core/plans/linkedin.d.ts +4 -0
  88. package/dist/core/plans/linkedin.js +23 -0
  89. package/dist/core/plans/pinterest.d.ts +4 -0
  90. package/dist/core/plans/pinterest.js +31 -0
  91. package/dist/core/plans/reddit.d.ts +11 -0
  92. package/dist/core/plans/reddit.js +46 -0
  93. package/dist/core/plans/rumble.js +21 -0
  94. package/dist/core/plans/snapchat.js +20 -0
  95. package/dist/core/plans/soundcloud.d.ts +14 -0
  96. package/dist/core/plans/soundcloud.js +37 -0
  97. package/dist/core/plans/spotify.d.ts +4 -0
  98. package/dist/core/plans/spotify.js +29 -0
  99. package/dist/core/plans/strava.d.ts +8 -0
  100. package/dist/core/plans/strava.js +38 -0
  101. package/dist/core/plans/streamable.d.ts +8 -0
  102. package/dist/core/plans/streamable.js +31 -0
  103. package/dist/core/plans/telegram.d.ts +10 -0
  104. package/dist/core/plans/telegram.js +59 -0
  105. package/dist/core/plans/tenor.js +16 -0
  106. package/dist/core/plans/threads.d.ts +4 -0
  107. package/dist/core/plans/threads.js +25 -0
  108. package/dist/core/plans/tiktok.d.ts +17 -0
  109. package/dist/core/plans/tiktok.js +36 -0
  110. package/dist/core/plans/truthsocial.js +43 -0
  111. package/dist/core/plans/tumblr.js +29 -0
  112. package/dist/core/plans/twitch.d.ts +7 -0
  113. package/dist/core/plans/twitch.js +38 -0
  114. package/dist/core/plans/vimeo.d.ts +23 -0
  115. package/dist/core/plans/vimeo.js +42 -0
  116. package/dist/core/plans/x.d.ts +12 -0
  117. package/dist/core/plans/x.js +31 -0
  118. package/dist/core/plans/youtube.d.ts +25 -0
  119. package/dist/core/plans/youtube.js +59 -0
  120. package/dist/core/post-link.js +19 -0
  121. package/dist/core/resolve.js +57 -0
  122. package/dist/core/timing.js +7 -0
  123. package/dist/element.d.ts +1 -1
  124. package/dist/element.js +1 -1
  125. package/dist/index.d.ts +53 -344
  126. package/dist/index.js +30 -2575
  127. package/dist/loader/load-script.js +116 -0
  128. package/dist/loader/platform-scripts.js +89 -0
  129. package/dist/parser/frames.js +70 -0
  130. package/dist/parser/html.js +172 -0
  131. package/dist/parser/iframe.d.ts +24 -0
  132. package/dist/parser/iframe.js +151 -0
  133. package/dist/parser/lookup.js +20 -0
  134. package/dist/parser/match-url.d.ts +3 -0
  135. package/dist/parser/match-url.js +22 -0
  136. package/dist/parser/parse-embed.d.ts +7 -0
  137. package/dist/parser/parse-embed.js +69 -0
  138. package/dist/parser/platform.js +60 -0
  139. package/dist/parser/platforms/applemusic.js +45 -0
  140. package/dist/parser/platforms/applepodcasts.js +41 -0
  141. package/dist/parser/platforms/bluesky.js +64 -0
  142. package/dist/parser/platforms/dailymotion.js +57 -0
  143. package/dist/parser/platforms/facebook.js +181 -0
  144. package/dist/parser/platforms/flickr.js +134 -0
  145. package/dist/parser/platforms/giphy.js +68 -0
  146. package/dist/parser/platforms/imgur.js +107 -0
  147. package/dist/parser/platforms/index.js +62 -0
  148. package/dist/parser/platforms/instagram.js +59 -0
  149. package/dist/parser/platforms/kick.js +61 -0
  150. package/dist/parser/platforms/linkedin.js +42 -0
  151. package/dist/parser/platforms/pinterest.js +59 -0
  152. package/dist/parser/platforms/reddit.js +69 -0
  153. package/dist/parser/platforms/rumble.js +44 -0
  154. package/dist/parser/platforms/snapchat.js +87 -0
  155. package/dist/parser/platforms/soundcloud.js +128 -0
  156. package/dist/parser/platforms/spotify.js +51 -0
  157. package/dist/parser/platforms/strava.js +55 -0
  158. package/dist/parser/platforms/streamable.js +95 -0
  159. package/dist/parser/platforms/telegram.js +61 -0
  160. package/dist/parser/platforms/tenor.js +79 -0
  161. package/dist/parser/platforms/threads.js +51 -0
  162. package/dist/parser/platforms/tiktok.js +48 -0
  163. package/dist/parser/platforms/truthsocial.js +50 -0
  164. package/dist/parser/platforms/tumblr.js +72 -0
  165. package/dist/parser/platforms/twitch.js +128 -0
  166. package/dist/parser/platforms/vimeo.js +95 -0
  167. package/dist/parser/platforms/x.js +65 -0
  168. package/dist/parser/platforms/youtube.js +140 -0
  169. package/dist/parser/types.d.ts +14 -0
  170. package/dist/parser/url.js +41 -0
  171. package/dist/parser/verify.js +63 -0
  172. package/dist/parser.d.ts +4 -1
  173. package/dist/parser.js +4 -1
  174. package/package.json +1 -1
  175. package/dist/index-DrMHAkyG.d.ts +0 -46
  176. package/dist/parser-CovaPg5T.js +0 -2585
@@ -0,0 +1,69 @@
1
+ import { decodeEntities, parseHtml } from "./html.js";
2
+ import { platforms } from "./platforms/index.js";
3
+ // src/parser/parse-embed.ts
4
+ /**
5
+ * Never throws. `null` unless the input is one embed element plus, at most, the platform's
6
+ * own scripts, `<div id="fb-root">`, one platform-owned extra such as a credit, whitespace and
7
+ * comments (docs/decisions.md D14).
8
+ */
9
+ function parseEmbed(html) {
10
+ if (typeof html !== "string") return null;
11
+ const nodes = parseHtml(html);
12
+ return nodes ? parseNodes(nodes) : parseRepaired(html);
13
+ }
14
+ function parseRepaired(html) {
15
+ for (const platform of platforms) {
16
+ const repaired = platform.repair?.(html);
17
+ const nodes = repaired === void 0 || repaired === null ? null : parseHtml(repaired);
18
+ const embed = nodes && parseNodes(nodes);
19
+ if (embed?.platform === platform.platform) return embed;
20
+ }
21
+ return null;
22
+ }
23
+ function parseNodes(nodes) {
24
+ let root;
25
+ let extra;
26
+ const scripts = [];
27
+ let fbRoot = false;
28
+ for (const node of nodes) {
29
+ if (node.type === "comment") continue;
30
+ if (node.type === "text") {
31
+ if (/^\s*$/.test(decodeEntities(node.raw))) continue;
32
+ return null;
33
+ }
34
+ if (node.tag === "script") scripts.push(node);
35
+ else if (isFbRoot(node)) fbRoot = true;
36
+ else if (root) {
37
+ if (extra) return null;
38
+ extra = node;
39
+ } else root = node;
40
+ }
41
+ if (!root) {
42
+ const [script] = scripts;
43
+ if (fbRoot || scripts.length !== 1 || !script) return null;
44
+ if (script.children.length !== 0) return null;
45
+ for (const platform of platforms) {
46
+ const embed = platform.parseScript?.(script);
47
+ if (embed) return embed;
48
+ }
49
+ return null;
50
+ }
51
+ for (const platform of platforms) {
52
+ const embed = platform.parse(root);
53
+ if (!embed) continue;
54
+ if (fbRoot && !platform.allowsFbRoot) return null;
55
+ if (extra && !platform.isOwnExtra?.(extra, embed)) return null;
56
+ const sources = scripts.map((script) => script.children.length === 0 ? script.attrs.get("src") : void 0);
57
+ if (!sources.every((src) => src !== void 0 && platform.isOwnScript(src))) return null;
58
+ const fromScripts = sources.map((src) => src === void 0 ? {} : platform.scriptOptions?.(src) ?? {});
59
+ return {
60
+ ...embed,
61
+ options: Object.assign({}, ...fromScripts, embed.options)
62
+ };
63
+ }
64
+ return null;
65
+ }
66
+ function isFbRoot(element) {
67
+ return element.tag === "div" && element.attrs.get("id") === "fb-root" && element.attrs.size === 1 && element.children.length === 0;
68
+ }
69
+ export { parseEmbed };
@@ -0,0 +1,60 @@
1
+ import { pixels } from "./url.js";
2
+ // src/parser/platform.ts
3
+ /** `/embed` holds `/embed` and `/embed/…`, never `/embedded`. */
4
+ function underPath(path, folder) {
5
+ return path === folder || path.startsWith(`${folder}/`);
6
+ }
7
+ /** The first attribute present for a key wins. */
8
+ function attributeOptions(element, mapping, coerceValue) {
9
+ const options = {};
10
+ for (const [attribute, key] of mapping) {
11
+ const value = element.attrs.get(attribute);
12
+ if (value !== void 0 && !(key in options)) options[key] = coerceValue(value);
13
+ }
14
+ return options;
15
+ }
16
+ function sizeOf(element) {
17
+ const width = pixels(element.attrs.get("width"));
18
+ const height = pixels(element.attrs.get("height"));
19
+ if (width === void 0 && height === void 0) return void 0;
20
+ return {
21
+ ...width === void 0 ? {} : { width },
22
+ ...height === void 0 ? {} : { height }
23
+ };
24
+ }
25
+ /** Adds `size` only when known, so results stay free of `undefined` properties. */
26
+ function withSize(embed, size) {
27
+ return size ? {
28
+ ...embed,
29
+ size
30
+ } : embed;
31
+ }
32
+ /**
33
+ * A platform's own responsive wrapper: a `div` whose only attribute is a `style` that `pattern`
34
+ * matches, around one iframe. The padding percentage it captures is the video's ratio.
35
+ */
36
+ function unwrapRatio(root, pattern) {
37
+ if (root.tag !== "div" || root.attrs.size !== 1) return null;
38
+ const [, percent] = pattern.exec(root.attrs.get("style") ?? "") ?? [];
39
+ if (percent === void 0) return null;
40
+ let iframe;
41
+ for (const child of root.children) {
42
+ if (child.type === "text" && /^\s*$/.test(child.raw)) continue;
43
+ if (child.type !== "element" || child.tag !== "iframe" || iframe) return null;
44
+ iframe = child;
45
+ }
46
+ const height = Math.round(Number(percent) * 100);
47
+ if (!iframe || !(height > 0 && height <= 1e5)) return null;
48
+ const divisor = gcd(1e4, height);
49
+ return {
50
+ iframe,
51
+ size: {
52
+ width: 1e4 / divisor,
53
+ height: height / divisor
54
+ }
55
+ };
56
+ }
57
+ function gcd(a, b) {
58
+ return b === 0 ? a : gcd(b, a % b);
59
+ }
60
+ export { attributeOptions, sizeOf, underPath, unwrapRatio, withSize };
@@ -0,0 +1,45 @@
1
+ import { sizeOf, withSize } from "../platform.js";
2
+ import { parseUrl } from "../url.js";
3
+ import { APPLE_MUSIC_FRAME_HOST, appleMusicFrame } from "../frames.js";
4
+ // 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
+ const applemusic = {
22
+ platform: "applemusic",
23
+ parse(root) {
24
+ if (root.tag !== "iframe") return null;
25
+ const found = appleContent(parseUrl(root.attrs.get("src") ?? ""), APPLE_MUSIC_FRAME_HOST);
26
+ if (!found) return null;
27
+ const { height } = sizeOf(root) ?? {};
28
+ return withSize({
29
+ platform: "applemusic",
30
+ ...found,
31
+ options: {}
32
+ }, height === void 0 ? void 0 : { height });
33
+ },
34
+ isOwnScript: () => false,
35
+ match(url) {
36
+ const found = appleContent(url, "music.apple.com");
37
+ return found && {
38
+ platform: "applemusic",
39
+ ...found,
40
+ options: {}
41
+ };
42
+ },
43
+ ownsFrame: appleMusicFrame
44
+ };
45
+ export { applemusic };
@@ -0,0 +1,41 @@
1
+ import { sizeOf, withSize } from "../platform.js";
2
+ import { parseUrl } from "../url.js";
3
+ import { APPLE_PODCASTS_FRAME_HOST, applePodcastsFrame } from "../frames.js";
4
+ // 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
+ const applepodcasts = {
22
+ platform: "applepodcasts",
23
+ parse(root) {
24
+ if (root.tag !== "iframe") return null;
25
+ const found = content(parseUrl(root.attrs.get("src") ?? ""), APPLE_PODCASTS_FRAME_HOST);
26
+ return found && withSize({
27
+ ...found,
28
+ options: {}
29
+ }, sizeOf(root));
30
+ },
31
+ isOwnScript: () => false,
32
+ match(url) {
33
+ const found = content(url, "podcasts.apple.com");
34
+ return found && {
35
+ ...found,
36
+ options: {}
37
+ };
38
+ },
39
+ ownsFrame: applePodcastsFrame
40
+ };
41
+ export { applepodcasts };
@@ -0,0 +1,64 @@
1
+ import { descendants, hasClass, rebuild } from "../html.js";
2
+ import { parseUrl } from "../url.js";
3
+ // src/parser/platforms/bluesky.ts
4
+ const DID = "(did:(?:plc:[a-z2-7]{24}|web:[a-z0-9.-]{1,253}))";
5
+ const RKEY = "((?!\\.{1,2}(?:/|$))[A-Za-z0-9._~:-]{1,512})";
6
+ const HOSTS = /* @__PURE__ */ new Set(["bsky.app", "www.bsky.app"]);
7
+ const POST_PATH = new RegExp(`^/profile/${DID}/post/${RKEY}/?$`);
8
+ const AT_URI = new RegExp(`^at://${DID}/app\\.bsky\\.feed\\.post/${RKEY}$`);
9
+ const CID = /^[a-z2-7]{20,100}$/;
10
+ const COLOR_MODES = /* @__PURE__ */ new Set([
11
+ "light",
12
+ "dark",
13
+ "system"
14
+ ]);
15
+ const SCRIPT = /^(?:https?:)?\/\/embed\.bsky\.app\/static\/embed\.js$/;
16
+ const FALLBACK = /* @__PURE__ */ new Map([
17
+ ["p", ["lang"]],
18
+ ["a", ["href"]],
19
+ ["br", []]
20
+ ]);
21
+ function post(did, rkey) {
22
+ return {
23
+ platform: "bluesky",
24
+ kind: "post",
25
+ url: `https://bsky.app/profile/${did}/post/${rkey}`,
26
+ id: `${did}/app.bsky.feed.post/${rkey}`,
27
+ variant: "iframe"
28
+ };
29
+ }
30
+ function fromUrl(url) {
31
+ if (!url || !HOSTS.has(url.host)) return null;
32
+ const [, did, rkey] = POST_PATH.exec(url.path) ?? [];
33
+ return did === void 0 || rkey === void 0 ? null : post(did, rkey);
34
+ }
35
+ const bluesky = {
36
+ platform: "bluesky",
37
+ fallback: FALLBACK,
38
+ parse(root) {
39
+ if (root.tag !== "blockquote" || !hasClass(root, "bluesky-embed")) return null;
40
+ const [, did, rkey] = AT_URI.exec(root.attrs.get("data-bluesky-uri") ?? "") ?? [];
41
+ if (did === void 0 || rkey === void 0) return null;
42
+ const found = post(did, rkey);
43
+ if (descendants(root.children).reverse().map((element) => element.tag === "a" ? fromUrl(parseUrl(element.attrs.get("href") ?? "")) : null).find((linked) => linked !== null)?.id !== found.id) return null;
44
+ const options = {};
45
+ const cid = root.attrs.get("data-bluesky-cid");
46
+ if (cid !== void 0 && CID.test(cid)) options.cid = cid;
47
+ const colorMode = root.attrs.get("data-bluesky-embed-color-mode");
48
+ if (colorMode !== void 0 && COLOR_MODES.has(colorMode)) options.colorMode = colorMode;
49
+ return {
50
+ ...found,
51
+ options,
52
+ fallbackHtml: rebuild(root.children, FALLBACK)
53
+ };
54
+ },
55
+ isOwnScript: (src) => SCRIPT.test(src),
56
+ match(url) {
57
+ const found = fromUrl(url);
58
+ return found && {
59
+ ...found,
60
+ options: {}
61
+ };
62
+ }
63
+ };
64
+ export { bluesky };
@@ -0,0 +1,57 @@
1
+ import { sizeOf, unwrapRatio, withSize } from "../platform.js";
2
+ import { coerce, parseUrl } from "../url.js";
3
+ import { DAILYMOTION_HOSTS, dailymotionFrame } from "../frames.js";
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
+ 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
+ const dailymotion = {
41
+ platform: "dailymotion",
42
+ parse(root) {
43
+ const wrapped = unwrapRatio(root, WRAPPER);
44
+ const iframe = wrapped?.iframe ?? root;
45
+ if (iframe.tag !== "iframe") return null;
46
+ const src = parseUrl(iframe.attrs.get("src") ?? "");
47
+ if (!src) return null;
48
+ const embed = fromPlayer(src) ?? fromPage(src);
49
+ return embed && withSize(embed, wrapped?.size ?? sizeOf(iframe));
50
+ },
51
+ isOwnScript: () => false,
52
+ match(url) {
53
+ return fromPlayer(url) ?? fromPage(url);
54
+ },
55
+ ownsFrame: dailymotionFrame
56
+ };
57
+ export { dailymotion };
@@ -0,0 +1,181 @@
1
+ import { hasClass, rebuild } from "../html.js";
2
+ import { attributeOptions, sizeOf, withSize } from "../platform.js";
3
+ import { coerce, parseUrl, pixels } from "../url.js";
4
+ import { FACEBOOK_PLUGIN_HOSTS, facebookFrame } from "../frames.js";
5
+ // 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
+ const SCRIPT = /^(?:https?:)?\/\/connect\.facebook\.net\/([a-z]{2,3}_[A-Z]{2})\/sdk\.js(?:[?#].*)?$/;
28
+ const OPTION_ATTRIBUTES = [
29
+ ["data-width", "width"],
30
+ ["data-show-text", "showText"],
31
+ ["data-lazy", "lazy"],
32
+ ["data-allowfullscreen", "allowfullscreen"],
33
+ ["data-autoplay", "autoplay"],
34
+ ["data-show-captions", "showCaptions"]
35
+ ];
36
+ const VIDEO_SDK_PARAMETERS = [
37
+ ["show_text", "showText"],
38
+ ["allowfullscreen", "allowfullscreen"],
39
+ ["autoplay", "autoplay"],
40
+ ["show_captions", "showCaptions"]
41
+ ];
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
+ const facebook = {
121
+ platform: "facebook",
122
+ fallback: FALLBACK,
123
+ parse(root) {
124
+ if (root.tag === "div") {
125
+ const kind = hasClass(root, "fb-post") ? "post" : hasClass(root, "fb-video") ? "video" : null;
126
+ const found = target(parseUrl(root.attrs.get("data-href") ?? ""));
127
+ if (!kind || !found) return null;
128
+ return {
129
+ ...embed(found, kind, "sdk", attributeOptions(root, OPTION_ATTRIBUTES, coerce)),
130
+ fallbackHtml: rebuild(root.children, FALLBACK)
131
+ };
132
+ }
133
+ if (root.tag !== "iframe") return null;
134
+ const src = parseUrl(root.attrs.get("src") ?? "");
135
+ if (!src || !FACEBOOK_PLUGIN_HOSTS.has(src.host)) return null;
136
+ const found = target(parseUrl(src.query.get("href") ?? ""));
137
+ if (!found) return null;
138
+ if (src.path === "/plugins/post.php") {
139
+ const options = {};
140
+ const showText = src.query.get("show_text");
141
+ const width = src.query.get("width");
142
+ if (showText !== void 0) options.showText = coerce(showText);
143
+ if (width !== void 0) options.width = coerce(width);
144
+ return embed(found, "post", "sdk", options);
145
+ }
146
+ if (src.path === "/plugins/video.php") {
147
+ const showText = coerce(src.query.get("show_text") ?? "");
148
+ if (showText === true || showText === 1) {
149
+ const options = {};
150
+ for (const [parameter, key] of VIDEO_SDK_PARAMETERS) {
151
+ const value = src.query.get(parameter);
152
+ if (value !== void 0) options[key] = coerce(value);
153
+ }
154
+ const width = pixels(src.query.get("width"));
155
+ const height = pixels(src.query.get("height"));
156
+ return withSize(embed(found, "video", "sdk", {
157
+ ...options,
158
+ showText: true
159
+ }), width === void 0 || height === void 0 ? void 0 : {
160
+ width,
161
+ height
162
+ });
163
+ }
164
+ const options = Object.fromEntries([...src.query].filter(([key]) => key !== "href").map(([key, value]) => [key, coerce(value)]));
165
+ return withSize(embed(found, "video", "iframe", options), sizeOf(root));
166
+ }
167
+ return null;
168
+ },
169
+ isOwnScript: (src) => SCRIPT.test(src),
170
+ scriptOptions(src) {
171
+ const [, locale] = SCRIPT.exec(src) ?? [];
172
+ return locale === void 0 ? {} : { locale };
173
+ },
174
+ allowsFbRoot: true,
175
+ match(url) {
176
+ const found = target(url);
177
+ return found && embed(found, found.kind, "sdk", {});
178
+ },
179
+ ownsFrame: facebookFrame
180
+ };
181
+ export { facebook };
@@ -0,0 +1,134 @@
1
+ import { rebuild } from "../html.js";
2
+ import { sizeOf, withSize } from "../platform.js";
3
+ import { parseUrl } from "../url.js";
4
+ // 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
+ const SCRIPT = /^(?:https?:)?\/\/embedr\.flickr\.com\/assets\/client-code\.js$/;
12
+ const ALPHABET = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
13
+ const OPTIONS = [
14
+ "header",
15
+ "footer",
16
+ "context"
17
+ ];
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
+ function onlyImage(root) {
96
+ let image = null;
97
+ for (const child of root.children) {
98
+ if (child.type === "text" && /^\s*$/.test(child.raw)) continue;
99
+ if (child.type !== "element" || child.tag !== "img" || image) return null;
100
+ image = child;
101
+ }
102
+ return image;
103
+ }
104
+ const flickr = {
105
+ platform: "flickr",
106
+ fallback: FALLBACK,
107
+ parse(root) {
108
+ if (root.tag !== "a" || root.attrs.get("data-flickr-embed") !== "true") return null;
109
+ const found = fromLongUrl(parseUrl(root.attrs.get("href") ?? ""));
110
+ const image = onlyImage(root);
111
+ if (!found || !image) return null;
112
+ const options = {};
113
+ for (const name of OPTIONS) if (root.attrs.get(`data-${name}`) === "true") options[name] = true;
114
+ const size = sizeOf(image);
115
+ const linked = IMAGE_SRC.test(image.attrs.get("src") ?? "") ? root : {
116
+ ...root,
117
+ children: []
118
+ };
119
+ return withSize({
120
+ ...found,
121
+ options,
122
+ fallbackHtml: rebuild([linked], FALLBACK)
123
+ }, size?.width !== void 0 && size.height !== void 0 ? size : void 0);
124
+ },
125
+ isOwnScript: (src) => SCRIPT.test(src),
126
+ match(url) {
127
+ const found = fromLongUrl(url) ?? fromShortUrl(url);
128
+ return found && {
129
+ ...found,
130
+ options: {}
131
+ };
132
+ }
133
+ };
134
+ export { decodeFlickrCode, flickr };