social-embeds 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -8
- package/dist/components/apple-embed.d.ts +7 -0
- package/dist/components/apple-embed.js +39 -0
- package/dist/components/bluesky-embed.d.ts +10 -0
- package/dist/components/bluesky-embed.js +27 -0
- package/dist/components/dailymotion-embed.d.ts +9 -0
- package/dist/components/dailymotion-embed.js +30 -0
- package/dist/components/facebook-embed.d.ts +6 -0
- package/dist/components/facebook-embed.js +40 -0
- package/dist/components/flickr-embed.d.ts +6 -0
- package/dist/components/flickr-embed.js +27 -0
- package/dist/components/generic-iframe-embed.d.ts +11 -0
- package/dist/components/generic-iframe-embed.js +26 -0
- package/dist/components/giphy-embed.d.ts +4 -0
- package/dist/components/giphy-embed.js +22 -0
- package/dist/components/hooks.js +36 -0
- package/dist/components/iframe-embed.js +169 -0
- package/dist/components/imgur-embed.d.ts +6 -0
- package/dist/components/imgur-embed.js +23 -0
- package/dist/components/instagram-embed.d.ts +5 -0
- package/dist/components/instagram-embed.js +25 -0
- package/dist/components/kick-embed.d.ts +6 -0
- package/dist/components/kick-embed.js +25 -0
- package/dist/components/linkedin-embed.d.ts +6 -0
- package/dist/components/linkedin-embed.js +23 -0
- package/dist/components/pinterest-embed.d.ts +5 -0
- package/dist/components/pinterest-embed.js +25 -0
- package/dist/components/reddit-embed.d.ts +6 -0
- package/dist/components/reddit-embed.js +29 -0
- package/dist/components/rumble-embed.d.ts +4 -0
- package/dist/components/rumble-embed.js +22 -0
- package/dist/components/script-embed.js +107 -0
- package/dist/components/shared.d.ts +29 -0
- package/dist/components/snapchat-embed.d.ts +4 -0
- package/dist/components/snapchat-embed.js +22 -0
- package/dist/components/social-embed.d.ts +60 -0
- package/dist/components/social-embed.js +221 -0
- package/dist/components/soundcloud-embed.d.ts +6 -0
- package/dist/components/soundcloud-embed.js +33 -0
- package/dist/components/spotify-embed.d.ts +9 -0
- package/dist/components/spotify-embed.js +25 -0
- package/dist/components/strava-embed.d.ts +6 -0
- package/dist/components/strava-embed.js +26 -0
- package/dist/components/streamable-embed.d.ts +6 -0
- package/dist/components/streamable-embed.js +28 -0
- package/dist/components/telegram-embed.d.ts +6 -0
- package/dist/components/telegram-embed.js +27 -0
- package/dist/components/tenor-embed.d.ts +4 -0
- package/dist/components/tenor-embed.js +22 -0
- package/dist/components/threads-embed.d.ts +5 -0
- package/dist/components/threads-embed.js +25 -0
- package/dist/components/tiktok-embed.d.ts +6 -0
- package/dist/components/tiktok-embed.js +37 -0
- package/dist/components/truthsocial-embed.d.ts +4 -0
- package/dist/components/truthsocial-embed.js +22 -0
- package/dist/components/tumblr-embed.d.ts +4 -0
- package/dist/components/tumblr-embed.js +22 -0
- package/dist/components/twitch-embed.d.ts +6 -0
- package/dist/components/twitch-embed.js +26 -0
- package/dist/components/vimeo-embed.d.ts +5 -0
- package/dist/components/vimeo-embed.js +40 -0
- package/dist/components/x-embed.d.ts +5 -0
- package/dist/components/x-embed.js +32 -0
- package/dist/components/youtube-embed.d.ts +5 -0
- package/dist/components/youtube-embed.js +43 -0
- package/dist/core/drivers.js +118 -0
- package/dist/core/layout.js +73 -0
- package/dist/core/markup.js +30 -0
- package/dist/core/params.js +26 -0
- package/dist/core/plans/apple.js +44 -0
- package/dist/core/plans/bluesky.d.ts +3 -0
- package/dist/core/plans/bluesky.js +34 -0
- package/dist/core/plans/dailymotion.d.ts +8 -0
- package/dist/core/plans/dailymotion.js +37 -0
- package/dist/core/plans/facebook.d.ts +15 -0
- package/dist/core/plans/facebook.js +79 -0
- package/dist/core/plans/flickr.d.ts +7 -0
- package/dist/core/plans/flickr.js +37 -0
- package/dist/core/plans/generic.js +44 -0
- package/dist/core/plans/giphy.js +16 -0
- package/dist/core/plans/imgur.d.ts +4 -0
- package/dist/core/plans/imgur.js +34 -0
- package/dist/core/plans/instagram.d.ts +4 -0
- package/dist/core/plans/instagram.js +26 -0
- package/dist/core/plans/kick.d.ts +5 -0
- package/dist/core/plans/kick.js +22 -0
- package/dist/core/plans/linkedin.d.ts +4 -0
- package/dist/core/plans/linkedin.js +23 -0
- package/dist/core/plans/pinterest.d.ts +4 -0
- package/dist/core/plans/pinterest.js +31 -0
- package/dist/core/plans/reddit.d.ts +11 -0
- package/dist/core/plans/reddit.js +46 -0
- package/dist/core/plans/rumble.js +21 -0
- package/dist/core/plans/snapchat.js +20 -0
- package/dist/core/plans/soundcloud.d.ts +14 -0
- package/dist/core/plans/soundcloud.js +37 -0
- package/dist/core/plans/spotify.d.ts +4 -0
- package/dist/core/plans/spotify.js +29 -0
- package/dist/core/plans/strava.d.ts +8 -0
- package/dist/core/plans/strava.js +38 -0
- package/dist/core/plans/streamable.d.ts +8 -0
- package/dist/core/plans/streamable.js +31 -0
- package/dist/core/plans/telegram.d.ts +10 -0
- package/dist/core/plans/telegram.js +59 -0
- package/dist/core/plans/tenor.js +16 -0
- package/dist/core/plans/threads.d.ts +4 -0
- package/dist/core/plans/threads.js +25 -0
- package/dist/core/plans/tiktok.d.ts +17 -0
- package/dist/core/plans/tiktok.js +36 -0
- package/dist/core/plans/truthsocial.js +43 -0
- package/dist/core/plans/tumblr.js +29 -0
- package/dist/core/plans/twitch.d.ts +7 -0
- package/dist/core/plans/twitch.js +38 -0
- package/dist/core/plans/vimeo.d.ts +23 -0
- package/dist/core/plans/vimeo.js +42 -0
- package/dist/core/plans/x.d.ts +12 -0
- package/dist/core/plans/x.js +31 -0
- package/dist/core/plans/youtube.d.ts +25 -0
- package/dist/core/plans/youtube.js +47 -0
- package/dist/core/post-link.js +19 -0
- package/dist/core/resolve.js +57 -0
- package/dist/core/timing.js +7 -0
- package/dist/element.js +1 -1
- package/dist/index.d.ts +53 -344
- package/dist/index.js +30 -2575
- package/dist/loader/load-script.js +114 -0
- package/dist/loader/platform-scripts.js +89 -0
- package/dist/parser/html.js +172 -0
- package/dist/parser/iframe.d.ts +24 -0
- package/dist/parser/iframe.js +151 -0
- package/dist/parser/lookup.js +20 -0
- package/dist/parser/match-url.d.ts +3 -0
- package/dist/parser/match-url.js +22 -0
- package/dist/parser/parse-embed.d.ts +7 -0
- package/dist/parser/parse-embed.js +69 -0
- package/dist/parser/platform.js +60 -0
- package/dist/parser/platforms/applemusic.js +45 -0
- package/dist/parser/platforms/applepodcasts.js +41 -0
- package/dist/parser/platforms/bluesky.js +64 -0
- package/dist/parser/platforms/dailymotion.js +59 -0
- package/dist/parser/platforms/facebook.js +185 -0
- package/dist/parser/platforms/flickr.js +134 -0
- package/dist/parser/platforms/giphy.js +68 -0
- package/dist/parser/platforms/imgur.js +107 -0
- package/dist/parser/platforms/index.js +62 -0
- package/dist/parser/platforms/instagram.js +59 -0
- package/dist/parser/platforms/kick.js +61 -0
- package/dist/parser/platforms/linkedin.js +42 -0
- package/dist/parser/platforms/pinterest.js +59 -0
- package/dist/parser/platforms/reddit.js +68 -0
- package/dist/parser/platforms/rumble.js +43 -0
- package/dist/parser/platforms/snapchat.js +87 -0
- package/dist/parser/platforms/soundcloud.js +128 -0
- package/dist/parser/platforms/spotify.js +51 -0
- package/dist/parser/platforms/strava.js +55 -0
- package/dist/parser/platforms/streamable.js +94 -0
- package/dist/parser/platforms/telegram.js +61 -0
- package/dist/parser/platforms/tenor.js +78 -0
- package/dist/parser/platforms/threads.js +51 -0
- package/dist/parser/platforms/tiktok.js +48 -0
- package/dist/parser/platforms/truthsocial.js +49 -0
- package/dist/parser/platforms/tumblr.js +70 -0
- package/dist/parser/platforms/twitch.js +129 -0
- package/dist/parser/platforms/vimeo.js +76 -0
- package/dist/parser/platforms/x.js +65 -0
- package/dist/parser/platforms/youtube.js +90 -0
- package/dist/parser/types.d.ts +14 -0
- package/dist/parser/url.js +41 -0
- package/dist/parser/verify.js +63 -0
- package/dist/parser.d.ts +4 -1
- package/dist/parser.js +4 -1
- package/package.json +1 -1
- package/dist/index-DrMHAkyG.d.ts +0 -46
- package/dist/parser-CovaPg5T.js +0 -2585
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { sizeOf, underPath, withSize } from "../platform.js";
|
|
2
|
+
import { coerce, parseUrl } from "../url.js";
|
|
3
|
+
// src/parser/platforms/twitch.ts
|
|
4
|
+
const HOSTS = /* @__PURE__ */ new Set([
|
|
5
|
+
"twitch.tv",
|
|
6
|
+
"www.twitch.tv",
|
|
7
|
+
"m.twitch.tv"
|
|
8
|
+
]);
|
|
9
|
+
const PLAYER_HOST = "player.twitch.tv";
|
|
10
|
+
const CLIPS_HOST = "clips.twitch.tv";
|
|
11
|
+
const CHANNEL = /^[A-Za-z0-9_]{3,25}$/;
|
|
12
|
+
const VIDEO = /^v?(\d{1,12})$/;
|
|
13
|
+
const COLLECTION = /^[A-Za-z0-9_-]{6,30}$/;
|
|
14
|
+
const CLIP = /^[A-Za-z0-9_-]{6,100}$/;
|
|
15
|
+
const TIME = /^(?:\d+h)?(?:\d+m)?(?:\d+s)?$/;
|
|
16
|
+
const RESERVED = /* @__PURE__ */ new Set([
|
|
17
|
+
"directory",
|
|
18
|
+
"videos",
|
|
19
|
+
"collections",
|
|
20
|
+
"settings",
|
|
21
|
+
"subscriptions",
|
|
22
|
+
"inventory",
|
|
23
|
+
"wallet",
|
|
24
|
+
"drops",
|
|
25
|
+
"search",
|
|
26
|
+
"downloads",
|
|
27
|
+
"jobs",
|
|
28
|
+
"turbo",
|
|
29
|
+
"p",
|
|
30
|
+
"login",
|
|
31
|
+
"signup"
|
|
32
|
+
]);
|
|
33
|
+
const PLAYER_OPTIONS = [
|
|
34
|
+
"autoplay",
|
|
35
|
+
"muted",
|
|
36
|
+
"time"
|
|
37
|
+
];
|
|
38
|
+
function target(path, id) {
|
|
39
|
+
return {
|
|
40
|
+
platform: "twitch",
|
|
41
|
+
kind: "video",
|
|
42
|
+
url: path,
|
|
43
|
+
id,
|
|
44
|
+
variant: "iframe"
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const channel = (name) => target(`https://www.twitch.tv/${name.toLowerCase()}`, name.toLowerCase());
|
|
48
|
+
const vod = (id) => target(`https://www.twitch.tv/videos/${id}`, id);
|
|
49
|
+
const collection = (id) => target(`https://www.twitch.tv/collections/${id}`, id);
|
|
50
|
+
const clip = (slug) => target(`https://clips.twitch.tv/${slug}`, slug);
|
|
51
|
+
function playerOptions(query) {
|
|
52
|
+
const options = {};
|
|
53
|
+
for (const name of PLAYER_OPTIONS) {
|
|
54
|
+
const value = query.get(name);
|
|
55
|
+
if (value !== void 0) options[name] = coerce(value);
|
|
56
|
+
}
|
|
57
|
+
return options;
|
|
58
|
+
}
|
|
59
|
+
/** The player's own precedence: channel, then video, then collection. */
|
|
60
|
+
function fromPlayer(url) {
|
|
61
|
+
const name = url.query.get("channel");
|
|
62
|
+
if (name !== void 0) return CHANNEL.test(name) ? channel(name) : null;
|
|
63
|
+
const [, id] = VIDEO.exec(url.query.get("video") ?? "") ?? [];
|
|
64
|
+
if (id !== void 0) return vod(id);
|
|
65
|
+
const list = url.query.get("collection");
|
|
66
|
+
return list !== void 0 && COLLECTION.test(list) ? collection(list) : null;
|
|
67
|
+
}
|
|
68
|
+
function fromPage(url) {
|
|
69
|
+
if (url.host === CLIPS_HOST) {
|
|
70
|
+
const slug = url.path.replace(/^\/|\/$/g, "");
|
|
71
|
+
return CLIP.test(slug) ? {
|
|
72
|
+
found: clip(slug),
|
|
73
|
+
options: {}
|
|
74
|
+
} : null;
|
|
75
|
+
}
|
|
76
|
+
if (!HOSTS.has(url.host)) return null;
|
|
77
|
+
const parts = url.path.replace(/^\/|\/$/g, "").split("/");
|
|
78
|
+
const [first = "", second, third] = parts;
|
|
79
|
+
if (parts.length === 2 && first === "videos") {
|
|
80
|
+
const [, id] = VIDEO.exec(second ?? "") ?? [];
|
|
81
|
+
if (id === void 0) return null;
|
|
82
|
+
const time = url.query.get("t");
|
|
83
|
+
return {
|
|
84
|
+
found: vod(id),
|
|
85
|
+
options: time !== void 0 && TIME.test(time) && time ? { time } : {}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (parts.length === 2 && first === "collections") return COLLECTION.test(second ?? "") ? {
|
|
89
|
+
found: collection(second ?? ""),
|
|
90
|
+
options: {}
|
|
91
|
+
} : null;
|
|
92
|
+
if (parts.length === 3 && second === "clip") return CHANNEL.test(first) && CLIP.test(third ?? "") ? {
|
|
93
|
+
found: clip(third ?? ""),
|
|
94
|
+
options: {}
|
|
95
|
+
} : null;
|
|
96
|
+
if (parts.length === 1 && CHANNEL.test(first) && !RESERVED.has(first.toLowerCase())) return {
|
|
97
|
+
found: channel(first),
|
|
98
|
+
options: {}
|
|
99
|
+
};
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const twitch = {
|
|
103
|
+
platform: "twitch",
|
|
104
|
+
parse(root) {
|
|
105
|
+
if (root.tag !== "iframe") return null;
|
|
106
|
+
const src = parseUrl(root.attrs.get("src") ?? "");
|
|
107
|
+
if (!src) return null;
|
|
108
|
+
let found = null;
|
|
109
|
+
if (src.host === PLAYER_HOST && /^\/?$/.test(src.path)) found = fromPlayer(src);
|
|
110
|
+
if (src.host === CLIPS_HOST && src.path === "/embed") {
|
|
111
|
+
const slug = src.query.get("clip") ?? "";
|
|
112
|
+
found = CLIP.test(slug) ? clip(slug) : null;
|
|
113
|
+
}
|
|
114
|
+
return found && withSize({
|
|
115
|
+
...found,
|
|
116
|
+
options: playerOptions(src.query)
|
|
117
|
+
}, sizeOf(root));
|
|
118
|
+
},
|
|
119
|
+
isOwnScript: () => false,
|
|
120
|
+
match(url) {
|
|
121
|
+
const page = fromPage(url);
|
|
122
|
+
return page && {
|
|
123
|
+
...page.found,
|
|
124
|
+
options: page.options
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
ownsFrame: (src) => src.host === PLAYER_HOST || src.host === CLIPS_HOST && underPath(src.path, "/embed")
|
|
128
|
+
};
|
|
129
|
+
export { twitch };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { sizeOf, unwrapRatio, withSize } from "../platform.js";
|
|
2
|
+
import { coerce, parseUrl } from "../url.js";
|
|
3
|
+
// src/parser/platforms/vimeo.ts
|
|
4
|
+
const HOSTS = /* @__PURE__ */ new Set(["vimeo.com", "www.vimeo.com"]);
|
|
5
|
+
const VIDEO = "(\\d{1,12})";
|
|
6
|
+
const HASH = "([0-9a-f]{6,20})";
|
|
7
|
+
const PAGE_PATHS = [
|
|
8
|
+
new RegExp(`^/${VIDEO}(?:/${HASH})?/?$`),
|
|
9
|
+
new RegExp(`^/channels/[A-Za-z0-9_-]{1,64}/${VIDEO}/?$`),
|
|
10
|
+
new RegExp(`^/groups/[A-Za-z0-9_-]{1,64}/videos/${VIDEO}/?$`),
|
|
11
|
+
new RegExp(`^/showcase/\\d{1,12}/video/${VIDEO}/?$`)
|
|
12
|
+
];
|
|
13
|
+
const PLAYER_PATH = new RegExp(`^/video/${VIDEO}/?$`);
|
|
14
|
+
const WRAPPER = /^padding:\s*(\d{1,3}(?:\.\d{1,4})?)% 0 0 0;\s*position:\s*relative;?$/;
|
|
15
|
+
const SCRIPT = /^(?:https?:)?\/\/player\.vimeo\.com\/api\/player\.js$/;
|
|
16
|
+
const DROPPED = /* @__PURE__ */ new Set([
|
|
17
|
+
"h",
|
|
18
|
+
"app_id",
|
|
19
|
+
"player_id"
|
|
20
|
+
]);
|
|
21
|
+
const START = /^t=(\d+(?:\.\d+)?|(?:\d+h)?(?:\d+m)?(?:\d+s)?)$/;
|
|
22
|
+
const PLAYER_HOST = "player.vimeo.com";
|
|
23
|
+
function video(id, hash) {
|
|
24
|
+
return {
|
|
25
|
+
platform: "vimeo",
|
|
26
|
+
kind: "video",
|
|
27
|
+
url: `https://vimeo.com/${id}${hash ? `/${hash}` : ""}`,
|
|
28
|
+
id,
|
|
29
|
+
variant: "iframe"
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function start(url) {
|
|
33
|
+
const [, time] = START.exec(url.fragment ?? "") ?? [];
|
|
34
|
+
return time ? { t: time } : {};
|
|
35
|
+
}
|
|
36
|
+
function fromPlayer(url) {
|
|
37
|
+
if (url?.host !== PLAYER_HOST) return null;
|
|
38
|
+
const [, id] = PLAYER_PATH.exec(url.path) ?? [];
|
|
39
|
+
if (id === void 0) return null;
|
|
40
|
+
const hash = url.query.get("h");
|
|
41
|
+
if (hash !== void 0 && !new RegExp(`^${HASH}$`).test(hash)) return null;
|
|
42
|
+
const options = Object.fromEntries([...url.query].filter(([key]) => !DROPPED.has(key)).map(([key, value]) => [key, coerce(value)]));
|
|
43
|
+
return {
|
|
44
|
+
...video(id, hash),
|
|
45
|
+
options: {
|
|
46
|
+
...options,
|
|
47
|
+
...start(url)
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const vimeo = {
|
|
52
|
+
platform: "vimeo",
|
|
53
|
+
parse(root) {
|
|
54
|
+
const wrapped = unwrapRatio(root, WRAPPER);
|
|
55
|
+
const iframe = wrapped?.iframe ?? root;
|
|
56
|
+
if (iframe.tag !== "iframe") return null;
|
|
57
|
+
const embed = fromPlayer(parseUrl(iframe.attrs.get("src") ?? ""));
|
|
58
|
+
return embed && withSize(embed, wrapped?.size ?? sizeOf(iframe));
|
|
59
|
+
},
|
|
60
|
+
isOwnScript: (src) => SCRIPT.test(src),
|
|
61
|
+
match(url) {
|
|
62
|
+
const player = fromPlayer(url);
|
|
63
|
+
if (player) return player;
|
|
64
|
+
if (!HOSTS.has(url.host)) return null;
|
|
65
|
+
for (const path of PAGE_PATHS) {
|
|
66
|
+
const [, id, hash] = path.exec(url.path) ?? [];
|
|
67
|
+
if (id !== void 0) return {
|
|
68
|
+
...video(id, hash),
|
|
69
|
+
options: start(url)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
},
|
|
74
|
+
ownsFrame: (src) => src.host === PLAYER_HOST
|
|
75
|
+
};
|
|
76
|
+
export { vimeo };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { descendants, hasClass, rebuild } from "../html.js";
|
|
2
|
+
import { attributeOptions } from "../platform.js";
|
|
3
|
+
import { coerce, parseUrl } from "../url.js";
|
|
4
|
+
// src/parser/platforms/x.ts
|
|
5
|
+
const HOSTS = /* @__PURE__ */ new Set([
|
|
6
|
+
"x.com",
|
|
7
|
+
"www.x.com",
|
|
8
|
+
"mobile.x.com",
|
|
9
|
+
"twitter.com",
|
|
10
|
+
"www.twitter.com",
|
|
11
|
+
"mobile.twitter.com"
|
|
12
|
+
]);
|
|
13
|
+
const STATUS_PATH = /^\/([A-Za-z0-9_]{1,15})\/status\/(\d{1,20})\/?$/;
|
|
14
|
+
const LINK_PATH = /^\/(?:([A-Za-z0-9_]{1,15})|i\/web)\/status\/(\d{1,20})(?:\/(?:photo|video)\/[1-4])?\/?$/;
|
|
15
|
+
const SCRIPT = /^(?:https?:)?\/\/platform\.(?:x|twitter)\.com\/widgets\.js$/;
|
|
16
|
+
const OPTION_ATTRIBUTES = [
|
|
17
|
+
["data-theme", "theme"],
|
|
18
|
+
["data-conversation", "conversation"],
|
|
19
|
+
["data-cards", "cards"],
|
|
20
|
+
["data-align", "align"],
|
|
21
|
+
["align", "align"],
|
|
22
|
+
["data-lang", "lang"],
|
|
23
|
+
["data-dnt", "dnt"],
|
|
24
|
+
["data-width", "width"]
|
|
25
|
+
];
|
|
26
|
+
const FALLBACK = /* @__PURE__ */ new Map([
|
|
27
|
+
["p", ["lang", "dir"]],
|
|
28
|
+
["a", ["href"]],
|
|
29
|
+
["br", []]
|
|
30
|
+
]);
|
|
31
|
+
function post(url, path = STATUS_PATH) {
|
|
32
|
+
if (!url || !HOSTS.has(url.host)) return null;
|
|
33
|
+
const [, user = "i", id] = path.exec(url.path) ?? [];
|
|
34
|
+
if (id === void 0) return null;
|
|
35
|
+
return {
|
|
36
|
+
platform: "x",
|
|
37
|
+
kind: "post",
|
|
38
|
+
url: `https://x.com/${user}/status/${id}`,
|
|
39
|
+
id,
|
|
40
|
+
variant: "sdk"
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const x = {
|
|
44
|
+
platform: "x",
|
|
45
|
+
fallback: FALLBACK,
|
|
46
|
+
parse(root) {
|
|
47
|
+
if (root.tag !== "blockquote" || !hasClass(root, "twitter-tweet")) return null;
|
|
48
|
+
const status = descendants(root.children).reverse().map((element) => element.tag === "a" ? post(parseUrl(element.attrs.get("href") ?? "")) : null).find((found) => found !== null);
|
|
49
|
+
if (!status) return null;
|
|
50
|
+
return {
|
|
51
|
+
...status,
|
|
52
|
+
options: attributeOptions(root, OPTION_ATTRIBUTES, coerce),
|
|
53
|
+
fallbackHtml: rebuild(root.children, FALLBACK)
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
isOwnScript: (src) => SCRIPT.test(src),
|
|
57
|
+
match(url) {
|
|
58
|
+
const status = post(url, LINK_PATH);
|
|
59
|
+
return status && {
|
|
60
|
+
...status,
|
|
61
|
+
options: {}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
export { x };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { sizeOf, underPath, withSize } from "../platform.js";
|
|
2
|
+
import { coerce, parseUrl } from "../url.js";
|
|
3
|
+
// src/parser/platforms/youtube.ts
|
|
4
|
+
const YOUTUBE_HOSTS = /* @__PURE__ */ new Set([
|
|
5
|
+
"youtube.com",
|
|
6
|
+
"www.youtube.com",
|
|
7
|
+
"m.youtube.com"
|
|
8
|
+
]);
|
|
9
|
+
const NOCOOKIE_HOSTS = /* @__PURE__ */ new Set(["youtube-nocookie.com", "www.youtube-nocookie.com"]);
|
|
10
|
+
const VIDEO_ID = /^(?!videoseries)[A-Za-z0-9_-]{11}$/;
|
|
11
|
+
const EMBED_PATH = /^\/embed\/((?!videoseries)[A-Za-z0-9_-]{11})\/?$/;
|
|
12
|
+
const URL_PATHS = /^\/(shorts|embed|live)\/((?!videoseries)[A-Za-z0-9_-]{11})\/?$/;
|
|
13
|
+
const SHORT_LINK_PATH = /^\/([A-Za-z0-9_-]{11})\/?$/;
|
|
14
|
+
const DURATION = /^(?:(\d{1,5})h)?(?:(\d{1,6})m)?(?:(\d{1,7})s?)?$/;
|
|
15
|
+
/** `90`, `90s`, `1m30s`, `1h2m3s` → seconds. */
|
|
16
|
+
function seconds(value) {
|
|
17
|
+
if (value === void 0 || value === "") return void 0;
|
|
18
|
+
const match = DURATION.exec(value);
|
|
19
|
+
if (!match) return void 0;
|
|
20
|
+
const [, hours = "0", minutes = "0", secs = "0"] = match;
|
|
21
|
+
return Number(hours) * 3600 + Number(minutes) * 60 + Number(secs);
|
|
22
|
+
}
|
|
23
|
+
/** `start` wins over `t`; `t` is converted, because the embed player only reads `start`. */
|
|
24
|
+
function startOf(query) {
|
|
25
|
+
return seconds(query.get("start")) ?? seconds(query.get("t"));
|
|
26
|
+
}
|
|
27
|
+
function video(id, shorts = false) {
|
|
28
|
+
return {
|
|
29
|
+
platform: "youtube",
|
|
30
|
+
kind: "video",
|
|
31
|
+
url: shorts ? `https://www.youtube.com/shorts/${id}` : `https://www.youtube.com/watch?v=${id}`,
|
|
32
|
+
id,
|
|
33
|
+
variant: "iframe"
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function urlOptions(url, nocookie) {
|
|
37
|
+
const start = startOf(url.query);
|
|
38
|
+
return {
|
|
39
|
+
...start === void 0 ? {} : { start },
|
|
40
|
+
...nocookie ? { nocookie: true } : {}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const youtube = {
|
|
44
|
+
platform: "youtube",
|
|
45
|
+
parse(root) {
|
|
46
|
+
if (root.tag !== "iframe") return null;
|
|
47
|
+
const src = parseUrl(root.attrs.get("src") ?? "");
|
|
48
|
+
const nocookie = src !== null && NOCOOKIE_HOSTS.has(src.host);
|
|
49
|
+
if (!src || !(nocookie || YOUTUBE_HOSTS.has(src.host))) return null;
|
|
50
|
+
const [, id] = EMBED_PATH.exec(src.path) ?? [];
|
|
51
|
+
if (id === void 0) return null;
|
|
52
|
+
const entries = [...src.query].filter(([key]) => key !== "t").map(([key, value]) => [key, coerce(value)]);
|
|
53
|
+
const options = Object.fromEntries(entries);
|
|
54
|
+
const start = startOf(src.query);
|
|
55
|
+
if (start !== void 0) options.start = start;
|
|
56
|
+
if (nocookie) options.nocookie = true;
|
|
57
|
+
return withSize({
|
|
58
|
+
...video(id),
|
|
59
|
+
options
|
|
60
|
+
}, sizeOf(root));
|
|
61
|
+
},
|
|
62
|
+
isOwnScript: () => false,
|
|
63
|
+
match(url) {
|
|
64
|
+
if (url.host === "youtu.be") {
|
|
65
|
+
const [, id] = SHORT_LINK_PATH.exec(url.path) ?? [];
|
|
66
|
+
return id === void 0 ? null : {
|
|
67
|
+
...video(id),
|
|
68
|
+
options: urlOptions(url, false)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const nocookie = NOCOOKIE_HOSTS.has(url.host);
|
|
72
|
+
if (!nocookie && !YOUTUBE_HOSTS.has(url.host)) return null;
|
|
73
|
+
if (!nocookie && /^\/watch\/?$/.test(url.path)) {
|
|
74
|
+
const id = url.query.get("v");
|
|
75
|
+
return id !== void 0 && VIDEO_ID.test(id) ? {
|
|
76
|
+
...video(id),
|
|
77
|
+
options: urlOptions(url, false)
|
|
78
|
+
} : null;
|
|
79
|
+
}
|
|
80
|
+
const [, kind, id] = URL_PATHS.exec(url.path) ?? [];
|
|
81
|
+
if (kind === void 0 || id === void 0) return null;
|
|
82
|
+
if (nocookie && kind !== "embed") return null;
|
|
83
|
+
return {
|
|
84
|
+
...video(id, kind === "shorts"),
|
|
85
|
+
options: urlOptions(url, nocookie)
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
ownsFrame: (src) => NOCOOKIE_HOSTS.has(src.host) || YOUTUBE_HOSTS.has(src.host) && underPath(src.path, "/embed")
|
|
89
|
+
};
|
|
90
|
+
export { youtube };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type Platform = 'x' | 'instagram' | 'facebook' | 'threads' | 'tiktok' | 'youtube' | 'telegram' | 'snapchat' | 'linkedin' | 'reddit' | 'pinterest' | 'spotify' | 'vimeo' | 'twitch' | 'dailymotion' | 'bluesky' | 'soundcloud' | 'applemusic' | 'applepodcasts' | 'tumblr' | 'kick' | 'giphy' | 'truthsocial' | 'imgur' | 'tenor' | 'streamable' | 'strava' | 'flickr' | 'rumble';
|
|
2
|
+
export type ParsedEmbed = {
|
|
3
|
+
platform: Platform;
|
|
4
|
+
kind: 'post' | 'video';
|
|
5
|
+
url: string;
|
|
6
|
+
id: string;
|
|
7
|
+
variant: 'sdk' | 'iframe';
|
|
8
|
+
options: Record<string, string | number | boolean>;
|
|
9
|
+
size?: {
|
|
10
|
+
width?: number;
|
|
11
|
+
height?: number;
|
|
12
|
+
};
|
|
13
|
+
fallbackHtml?: string;
|
|
14
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
export { coerce, parseUrl, pixels };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { parseHtml, rebuild } from "./html.js";
|
|
2
|
+
import { matchWith } from "./match-url.js";
|
|
3
|
+
// src/parser/verify.ts
|
|
4
|
+
const SHAPES = {
|
|
5
|
+
x: ["post sdk"],
|
|
6
|
+
instagram: ["post sdk"],
|
|
7
|
+
threads: ["post sdk"],
|
|
8
|
+
facebook: [
|
|
9
|
+
"post sdk",
|
|
10
|
+
"video sdk",
|
|
11
|
+
"video iframe"
|
|
12
|
+
],
|
|
13
|
+
tiktok: ["video iframe"],
|
|
14
|
+
youtube: ["video iframe"],
|
|
15
|
+
telegram: ["post iframe"],
|
|
16
|
+
snapchat: ["post iframe", "video iframe"],
|
|
17
|
+
linkedin: ["post iframe"],
|
|
18
|
+
reddit: ["post iframe"],
|
|
19
|
+
pinterest: ["post sdk"],
|
|
20
|
+
spotify: ["post iframe", "video iframe"],
|
|
21
|
+
vimeo: ["video iframe"],
|
|
22
|
+
twitch: ["video iframe"],
|
|
23
|
+
dailymotion: ["video iframe"],
|
|
24
|
+
bluesky: ["post iframe"],
|
|
25
|
+
soundcloud: ["post iframe"],
|
|
26
|
+
applemusic: ["post iframe"],
|
|
27
|
+
applepodcasts: ["post iframe"],
|
|
28
|
+
tumblr: ["post iframe"],
|
|
29
|
+
kick: ["video iframe"],
|
|
30
|
+
giphy: ["video iframe"],
|
|
31
|
+
truthsocial: ["post iframe"],
|
|
32
|
+
imgur: ["post iframe"],
|
|
33
|
+
tenor: ["video iframe"],
|
|
34
|
+
streamable: ["video iframe"],
|
|
35
|
+
strava: ["post iframe"],
|
|
36
|
+
flickr: ["post iframe"],
|
|
37
|
+
rumble: ["video iframe"]
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* `verifyEmbed` with only the embed's own parser, so a per-platform component does not
|
|
41
|
+
* bundle the other platforms' (docs/decisions.md D103).
|
|
42
|
+
*/
|
|
43
|
+
function verifyWith(parser, embed) {
|
|
44
|
+
if (embed.platform !== parser.platform) return null;
|
|
45
|
+
const matched = matchWith(parser, embed.url);
|
|
46
|
+
if (matched?.platform !== embed.platform || matched.id !== embed.id) return null;
|
|
47
|
+
if (matched.url !== embed.url) return null;
|
|
48
|
+
if (!SHAPES[embed.platform].includes(`${embed.kind} ${embed.variant}`)) return null;
|
|
49
|
+
if (embed.fallbackHtml === void 0) return embed;
|
|
50
|
+
if (trustedFallback(parser, embed, embed.fallbackHtml)) return embed;
|
|
51
|
+
const withoutFallback = { ...embed };
|
|
52
|
+
delete withoutFallback.fallbackHtml;
|
|
53
|
+
return withoutFallback;
|
|
54
|
+
}
|
|
55
|
+
function trustedFallback(parser, embed, fallbackHtml) {
|
|
56
|
+
const whitelist = parser.fallback;
|
|
57
|
+
const nodes = parseHtml(fallbackHtml);
|
|
58
|
+
if (!whitelist || !nodes || rebuild(nodes, whitelist) !== fallbackHtml) return false;
|
|
59
|
+
if (embed.platform !== "x") return true;
|
|
60
|
+
const [root] = parseHtml(`<blockquote class="twitter-tweet">${fallbackHtml}</blockquote>`) ?? [];
|
|
61
|
+
return root?.type === "element" && parser.parse(root)?.id === embed.id;
|
|
62
|
+
}
|
|
63
|
+
export { verifyWith };
|
package/dist/parser.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ParsedIframe, parseIframe } from "./parser/iframe.js";
|
|
2
|
+
import { matchUrl } from "./parser/match-url.js";
|
|
3
|
+
import { parseEmbed } from "./parser/parse-embed.js";
|
|
4
|
+
import { ParsedEmbed, Platform } from "./parser/types.js";
|
|
2
5
|
export { type ParsedEmbed, type ParsedIframe, type Platform, matchUrl, parseEmbed, parseIframe };
|
package/dist/parser.js
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import { parseIframe } from "./parser/iframe.js";
|
|
2
|
+
import { matchUrl } from "./parser/match-url.js";
|
|
3
|
+
import { parseEmbed } from "./parser/parse-embed.js";
|
|
4
|
+
export { matchUrl, parseEmbed, parseIframe };
|
package/package.json
CHANGED
package/dist/index-DrMHAkyG.d.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
declare const REFERRER_POLICIES: readonly ["no-referrer", "origin", "same-origin", "strict-origin", "strict-origin-when-cross-origin"];
|
|
2
|
-
type ParsedIframe = {
|
|
3
|
-
src: string;
|
|
4
|
-
title?: string;
|
|
5
|
-
allow: string;
|
|
6
|
-
allowFullscreen: boolean;
|
|
7
|
-
referrerPolicy?: (typeof REFERRER_POLICIES)[number];
|
|
8
|
-
sandbox?: string;
|
|
9
|
-
scrolling?: 'no';
|
|
10
|
-
size: {
|
|
11
|
-
width?: number;
|
|
12
|
-
height?: number;
|
|
13
|
-
};
|
|
14
|
-
ratio?: {
|
|
15
|
-
width: number;
|
|
16
|
-
height: number;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* Never throws. `null` unless the code holds exactly one `<iframe>` with an https `src` that no
|
|
21
|
-
* named platform owns; everything around the iframe is dropped.
|
|
22
|
-
*/
|
|
23
|
-
declare function parseIframe(html: string): ParsedIframe | null;
|
|
24
|
-
type Platform = 'x' | 'instagram' | 'facebook' | 'threads' | 'tiktok' | 'youtube' | 'telegram' | 'snapchat' | 'linkedin' | 'reddit' | 'pinterest' | 'spotify' | 'vimeo' | 'twitch' | 'dailymotion' | 'bluesky' | 'soundcloud' | 'applemusic' | 'applepodcasts' | 'tumblr' | 'kick' | 'giphy' | 'truthsocial' | 'imgur' | 'tenor' | 'streamable' | 'strava' | 'flickr' | 'rumble';
|
|
25
|
-
type ParsedEmbed = {
|
|
26
|
-
platform: Platform;
|
|
27
|
-
kind: 'post' | 'video';
|
|
28
|
-
url: string;
|
|
29
|
-
id: string;
|
|
30
|
-
variant: 'sdk' | 'iframe';
|
|
31
|
-
options: Record<string, string | number | boolean>;
|
|
32
|
-
size?: {
|
|
33
|
-
width?: number;
|
|
34
|
-
height?: number;
|
|
35
|
-
};
|
|
36
|
-
fallbackHtml?: string;
|
|
37
|
-
};
|
|
38
|
-
/** Never throws. Options hold only what the URL itself carries (docs/decisions.md D13). */
|
|
39
|
-
declare function matchUrl(url: string): ParsedEmbed | null;
|
|
40
|
-
/**
|
|
41
|
-
* Never throws. `null` unless the input is one embed element plus, at most, the platform's
|
|
42
|
-
* own scripts, `<div id="fb-root">`, one platform-owned extra such as a credit, whitespace and
|
|
43
|
-
* comments (docs/decisions.md D14).
|
|
44
|
-
*/
|
|
45
|
-
declare function parseEmbed(html: string): ParsedEmbed | null;
|
|
46
|
-
export { ParsedIframe as a, Platform as i, matchUrl as n, parseIframe as o, ParsedEmbed as r, parseEmbed as t };
|