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.
- package/README.md +63 -51
- 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 +38 -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 +108 -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 +125 -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 +59 -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.d.ts +1 -1
- 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 +116 -0
- package/dist/loader/platform-scripts.js +89 -0
- package/dist/parser/frames.js +70 -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 +57 -0
- package/dist/parser/platforms/facebook.js +181 -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 +69 -0
- package/dist/parser/platforms/rumble.js +44 -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 +95 -0
- package/dist/parser/platforms/telegram.js +61 -0
- package/dist/parser/platforms/tenor.js +79 -0
- package/dist/parser/platforms/threads.js +51 -0
- package/dist/parser/platforms/tiktok.js +48 -0
- package/dist/parser/platforms/truthsocial.js +50 -0
- package/dist/parser/platforms/tumblr.js +72 -0
- package/dist/parser/platforms/twitch.js +128 -0
- package/dist/parser/platforms/vimeo.js +95 -0
- package/dist/parser/platforms/x.js +65 -0
- package/dist/parser/platforms/youtube.js +140 -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,72 @@
|
|
|
1
|
+
import { hasClass } from "../html.js";
|
|
2
|
+
import { parseUrl } from "../url.js";
|
|
3
|
+
// src/parser/platforms/tumblr.ts
|
|
4
|
+
const BLOG = "([a-z0-9-]{1,32})";
|
|
5
|
+
const POST = "(\\d{1,20})";
|
|
6
|
+
const WWW_HOSTS = /* @__PURE__ */ new Set(["www.tumblr.com", "tumblr.com"]);
|
|
7
|
+
const WWW_PATH = new RegExp(`^/${BLOG}/${POST}(?:/[^/]{1,200})?/?$`);
|
|
8
|
+
const BLOG_HOST = new RegExp(`^${BLOG}\\.tumblr\\.com$`);
|
|
9
|
+
const BLOG_PATH = new RegExp(`^/post/${POST}(?:/[^/]{1,200})?/?$`);
|
|
10
|
+
const FRAME_PATH = new RegExp(`^/embed/post/(?:t:[A-Za-z0-9_-]{1,64}|${BLOG})/${POST}/v2$`);
|
|
11
|
+
const OLD_FRAME_PATH = new RegExp(`^/embed/post/(?:t:)?[A-Za-z0-9_-]{1,64}/${POST}$`);
|
|
12
|
+
const NOT_BLOGS = /* @__PURE__ */ new Set([
|
|
13
|
+
"dashboard",
|
|
14
|
+
"explore",
|
|
15
|
+
"tagged",
|
|
16
|
+
"search",
|
|
17
|
+
"settings",
|
|
18
|
+
"blog",
|
|
19
|
+
"www",
|
|
20
|
+
"assets",
|
|
21
|
+
"embed",
|
|
22
|
+
"api",
|
|
23
|
+
"help"
|
|
24
|
+
]);
|
|
25
|
+
const SCRIPT = /^(?:https?:)?\/\/(?:secure\.)?assets\.tumblr\.com\/post\.js(?:\?[^#]*)?$/;
|
|
26
|
+
function fromUrl(url) {
|
|
27
|
+
if (!url) return null;
|
|
28
|
+
let blog;
|
|
29
|
+
let id;
|
|
30
|
+
if (WWW_HOSTS.has(url.host)) [, blog, id] = WWW_PATH.exec(url.path) ?? [];
|
|
31
|
+
else {
|
|
32
|
+
[, blog] = BLOG_HOST.exec(url.host) ?? [];
|
|
33
|
+
[, id] = BLOG_PATH.exec(url.path) ?? [];
|
|
34
|
+
}
|
|
35
|
+
if (blog === void 0 || id === void 0 || NOT_BLOGS.has(blog)) return null;
|
|
36
|
+
return {
|
|
37
|
+
platform: "tumblr",
|
|
38
|
+
kind: "post",
|
|
39
|
+
url: `https://www.tumblr.com/${blog}/${id}`,
|
|
40
|
+
id,
|
|
41
|
+
variant: "iframe"
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const tumblr = {
|
|
45
|
+
platform: "tumblr",
|
|
46
|
+
parse(root) {
|
|
47
|
+
if (root.tag !== "div" || !hasClass(root, "tumblr-post")) return null;
|
|
48
|
+
const frame = parseUrl(root.attrs.get("data-href") ?? "");
|
|
49
|
+
if (frame?.host !== "embed.tumblr.com") return null;
|
|
50
|
+
const [, frameBlog, v2Post] = FRAME_PATH.exec(frame.path) ?? [];
|
|
51
|
+
const framePost = v2Post ?? OLD_FRAME_PATH.exec(frame.path)?.[1];
|
|
52
|
+
if (framePost === void 0) return null;
|
|
53
|
+
const [link, ...rest] = root.children.filter((node) => node.type !== "text" || node.raw.trim() !== "");
|
|
54
|
+
if (rest.length > 0 || link?.type !== "element" || link.tag !== "a") return null;
|
|
55
|
+
const found = fromUrl(parseUrl(link.attrs.get("href") ?? ""));
|
|
56
|
+
if (found?.id !== framePost) return null;
|
|
57
|
+
if (frameBlog !== void 0 && found.url !== `https://www.tumblr.com/${frameBlog}/${framePost}`) return null;
|
|
58
|
+
return {
|
|
59
|
+
...found,
|
|
60
|
+
options: {}
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
isOwnScript: (src) => SCRIPT.test(src),
|
|
64
|
+
match(url) {
|
|
65
|
+
const found = fromUrl(url);
|
|
66
|
+
return found && {
|
|
67
|
+
...found,
|
|
68
|
+
options: {}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
export { tumblr };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { sizeOf, withSize } from "../platform.js";
|
|
2
|
+
import { coerce, parseUrl } from "../url.js";
|
|
3
|
+
import { twitchFrame } from "../frames.js";
|
|
4
|
+
// src/parser/platforms/twitch.ts
|
|
5
|
+
const HOSTS = /* @__PURE__ */ new Set([
|
|
6
|
+
"twitch.tv",
|
|
7
|
+
"www.twitch.tv",
|
|
8
|
+
"m.twitch.tv"
|
|
9
|
+
]);
|
|
10
|
+
const CHANNEL = /^[A-Za-z0-9_]{3,25}$/;
|
|
11
|
+
const VIDEO = /^v?(\d{1,12})$/;
|
|
12
|
+
const COLLECTION = /^[A-Za-z0-9_-]{6,30}$/;
|
|
13
|
+
const CLIP = /^[A-Za-z0-9_-]{6,100}$/;
|
|
14
|
+
const TIME = /^(?:\d+h)?(?:\d+m)?(?:\d+s)?$/;
|
|
15
|
+
const RESERVED = /* @__PURE__ */ new Set([
|
|
16
|
+
"directory",
|
|
17
|
+
"videos",
|
|
18
|
+
"collections",
|
|
19
|
+
"settings",
|
|
20
|
+
"subscriptions",
|
|
21
|
+
"inventory",
|
|
22
|
+
"wallet",
|
|
23
|
+
"drops",
|
|
24
|
+
"search",
|
|
25
|
+
"downloads",
|
|
26
|
+
"jobs",
|
|
27
|
+
"turbo",
|
|
28
|
+
"p",
|
|
29
|
+
"login",
|
|
30
|
+
"signup"
|
|
31
|
+
]);
|
|
32
|
+
const PLAYER_OPTIONS = [
|
|
33
|
+
"autoplay",
|
|
34
|
+
"muted",
|
|
35
|
+
"time"
|
|
36
|
+
];
|
|
37
|
+
function target(path, id) {
|
|
38
|
+
return {
|
|
39
|
+
platform: "twitch",
|
|
40
|
+
kind: "video",
|
|
41
|
+
url: path,
|
|
42
|
+
id,
|
|
43
|
+
variant: "iframe"
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const channel = (name) => target(`https://www.twitch.tv/${name.toLowerCase()}`, name.toLowerCase());
|
|
47
|
+
const vod = (id) => target(`https://www.twitch.tv/videos/${id}`, id);
|
|
48
|
+
const collection = (id) => target(`https://www.twitch.tv/collections/${id}`, id);
|
|
49
|
+
const clip = (slug) => target(`https://clips.twitch.tv/${slug}`, slug);
|
|
50
|
+
function playerOptions(query) {
|
|
51
|
+
const options = {};
|
|
52
|
+
for (const name of PLAYER_OPTIONS) {
|
|
53
|
+
const value = query.get(name);
|
|
54
|
+
if (value !== void 0) options[name] = coerce(value);
|
|
55
|
+
}
|
|
56
|
+
return options;
|
|
57
|
+
}
|
|
58
|
+
/** The player's own precedence: channel, then video, then collection. */
|
|
59
|
+
function fromPlayer(url) {
|
|
60
|
+
const name = url.query.get("channel");
|
|
61
|
+
if (name !== void 0) return CHANNEL.test(name) ? channel(name) : null;
|
|
62
|
+
const [, id] = VIDEO.exec(url.query.get("video") ?? "") ?? [];
|
|
63
|
+
if (id !== void 0) return vod(id);
|
|
64
|
+
const list = url.query.get("collection");
|
|
65
|
+
return list !== void 0 && COLLECTION.test(list) ? collection(list) : null;
|
|
66
|
+
}
|
|
67
|
+
function fromPage(url) {
|
|
68
|
+
if (url.host === "clips.twitch.tv") {
|
|
69
|
+
const slug = url.path.replace(/^\/|\/$/g, "");
|
|
70
|
+
return CLIP.test(slug) ? {
|
|
71
|
+
found: clip(slug),
|
|
72
|
+
options: {}
|
|
73
|
+
} : null;
|
|
74
|
+
}
|
|
75
|
+
if (!HOSTS.has(url.host)) return null;
|
|
76
|
+
const parts = url.path.replace(/^\/|\/$/g, "").split("/");
|
|
77
|
+
const [first = "", second, third] = parts;
|
|
78
|
+
if (parts.length === 2 && first === "videos") {
|
|
79
|
+
const [, id] = VIDEO.exec(second ?? "") ?? [];
|
|
80
|
+
if (id === void 0) return null;
|
|
81
|
+
const time = url.query.get("t");
|
|
82
|
+
return {
|
|
83
|
+
found: vod(id),
|
|
84
|
+
options: time !== void 0 && TIME.test(time) && time ? { time } : {}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (parts.length === 2 && first === "collections") return COLLECTION.test(second ?? "") ? {
|
|
88
|
+
found: collection(second ?? ""),
|
|
89
|
+
options: {}
|
|
90
|
+
} : null;
|
|
91
|
+
if (parts.length === 3 && second === "clip") return CHANNEL.test(first) && CLIP.test(third ?? "") ? {
|
|
92
|
+
found: clip(third ?? ""),
|
|
93
|
+
options: {}
|
|
94
|
+
} : null;
|
|
95
|
+
if (parts.length === 1 && CHANNEL.test(first) && !RESERVED.has(first.toLowerCase())) return {
|
|
96
|
+
found: channel(first),
|
|
97
|
+
options: {}
|
|
98
|
+
};
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
const twitch = {
|
|
102
|
+
platform: "twitch",
|
|
103
|
+
parse(root) {
|
|
104
|
+
if (root.tag !== "iframe") return null;
|
|
105
|
+
const src = parseUrl(root.attrs.get("src") ?? "");
|
|
106
|
+
if (!src) return null;
|
|
107
|
+
let found = null;
|
|
108
|
+
if (src.host === "player.twitch.tv" && /^\/?$/.test(src.path)) found = fromPlayer(src);
|
|
109
|
+
if (src.host === "clips.twitch.tv" && src.path === "/embed") {
|
|
110
|
+
const slug = src.query.get("clip") ?? "";
|
|
111
|
+
found = CLIP.test(slug) ? clip(slug) : null;
|
|
112
|
+
}
|
|
113
|
+
return found && withSize({
|
|
114
|
+
...found,
|
|
115
|
+
options: playerOptions(src.query)
|
|
116
|
+
}, sizeOf(root));
|
|
117
|
+
},
|
|
118
|
+
isOwnScript: () => false,
|
|
119
|
+
match(url) {
|
|
120
|
+
const page = fromPage(url);
|
|
121
|
+
return page && {
|
|
122
|
+
...page.found,
|
|
123
|
+
options: page.options
|
|
124
|
+
};
|
|
125
|
+
},
|
|
126
|
+
ownsFrame: twitchFrame
|
|
127
|
+
};
|
|
128
|
+
export { twitch };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { sizeOf, unwrapRatio, withSize } from "../platform.js";
|
|
2
|
+
import { coerce, parseUrl } from "../url.js";
|
|
3
|
+
import { vimeoFrame } from "../frames.js";
|
|
4
|
+
// src/parser/platforms/vimeo.ts
|
|
5
|
+
const HOSTS = /* @__PURE__ */ new Set(["vimeo.com", "www.vimeo.com"]);
|
|
6
|
+
const VIDEO = "(\\d{1,12})";
|
|
7
|
+
const HASH = "([0-9a-f]{6,20})";
|
|
8
|
+
const PAGE_PATHS = [
|
|
9
|
+
new RegExp(`^/${VIDEO}(?:/${HASH})?/?$`),
|
|
10
|
+
new RegExp(`^/channels/[A-Za-z0-9_-]{1,64}/${VIDEO}/?$`),
|
|
11
|
+
new RegExp(`^/groups/[A-Za-z0-9_-]{1,64}/videos/${VIDEO}/?$`),
|
|
12
|
+
new RegExp(`^/showcase/\\d{1,12}/video/${VIDEO}/?$`)
|
|
13
|
+
];
|
|
14
|
+
const PLAYER_PATH = new RegExp(`^/video/${VIDEO}/?$`);
|
|
15
|
+
const WRAPPER = /^padding:\s*(\d{1,3}(?:\.\d{1,4})?)% 0 0 0;\s*position:\s*relative;?$/;
|
|
16
|
+
const SCRIPT = /^(?:https?:)?\/\/player\.vimeo\.com\/api\/player\.js$/;
|
|
17
|
+
const DROPPED = /* @__PURE__ */ new Set([
|
|
18
|
+
"h",
|
|
19
|
+
"app_id",
|
|
20
|
+
"player_id"
|
|
21
|
+
]);
|
|
22
|
+
const START = /^t=(\d+(?:\.\d+)?|(?:\d+h)?(?:\d+m)?(?:\d+s)?)$/;
|
|
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 fromPage(url) {
|
|
37
|
+
if (!url || !HOSTS.has(url.host)) return null;
|
|
38
|
+
for (const path of PAGE_PATHS) {
|
|
39
|
+
const [, id, hash] = path.exec(url.path) ?? [];
|
|
40
|
+
if (id !== void 0) return video(id, hash);
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
function isLink(node, test) {
|
|
45
|
+
if (node?.type !== "element" || node.tag !== "a" || node.attrs.size !== 1) return false;
|
|
46
|
+
const href = parseUrl(node.attrs.get("href") ?? "");
|
|
47
|
+
return href !== null && HOSTS.has(href.host) && test(href) && node.children.every((child) => child.type === "text");
|
|
48
|
+
}
|
|
49
|
+
function isText(node, text) {
|
|
50
|
+
return node?.type === "text" && node.raw.trim() === text;
|
|
51
|
+
}
|
|
52
|
+
/** Older embed code's credit: the video, its author and Vimeo, linked; it is dropped. */
|
|
53
|
+
function isCredit(element, embed) {
|
|
54
|
+
if (element.tag !== "p" || element.attrs.size !== 0) return false;
|
|
55
|
+
const [title, from, author, on, home, stop, ...rest] = element.children;
|
|
56
|
+
return rest.length === 0 && isLink(title, (url) => fromPage(url)?.id === embed.id) && isText(from, "from") && isLink(author, () => true) && isText(on, "on") && isLink(home, (url) => url.path === "" || url.path === "/") && isText(stop, ".");
|
|
57
|
+
}
|
|
58
|
+
function fromPlayer(url) {
|
|
59
|
+
if (url?.host !== "player.vimeo.com") return null;
|
|
60
|
+
const [, id] = PLAYER_PATH.exec(url.path) ?? [];
|
|
61
|
+
if (id === void 0) return null;
|
|
62
|
+
const hash = url.query.get("h");
|
|
63
|
+
if (hash !== void 0 && !new RegExp(`^${HASH}$`).test(hash)) return null;
|
|
64
|
+
const options = Object.fromEntries([...url.query].filter(([key]) => !DROPPED.has(key)).map(([key, value]) => [key, coerce(value)]));
|
|
65
|
+
return {
|
|
66
|
+
...video(id, hash),
|
|
67
|
+
options: {
|
|
68
|
+
...options,
|
|
69
|
+
...start(url)
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const vimeo = {
|
|
74
|
+
platform: "vimeo",
|
|
75
|
+
parse(root) {
|
|
76
|
+
const wrapped = unwrapRatio(root, WRAPPER);
|
|
77
|
+
const iframe = wrapped?.iframe ?? root;
|
|
78
|
+
if (iframe.tag !== "iframe") return null;
|
|
79
|
+
const embed = fromPlayer(parseUrl(iframe.attrs.get("src") ?? ""));
|
|
80
|
+
return embed && withSize(embed, wrapped?.size ?? sizeOf(iframe));
|
|
81
|
+
},
|
|
82
|
+
isOwnScript: (src) => SCRIPT.test(src),
|
|
83
|
+
isOwnExtra: isCredit,
|
|
84
|
+
match(url) {
|
|
85
|
+
const player = fromPlayer(url);
|
|
86
|
+
if (player) return player;
|
|
87
|
+
const page = fromPage(url);
|
|
88
|
+
return page && {
|
|
89
|
+
...page,
|
|
90
|
+
options: start(url)
|
|
91
|
+
};
|
|
92
|
+
},
|
|
93
|
+
ownsFrame: vimeoFrame
|
|
94
|
+
};
|
|
95
|
+
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") || hasClass(root, "twitter-video"))) 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,140 @@
|
|
|
1
|
+
import { sizeOf, underPath, withSize } from "../platform.js";
|
|
2
|
+
import { coerce, parseUrl } from "../url.js";
|
|
3
|
+
import { YOUTUBE_HOSTS, YOUTUBE_NOCOOKIE_HOSTS, youtubeFrame } from "../frames.js";
|
|
4
|
+
// src/parser/platforms/youtube.ts
|
|
5
|
+
const BASE = "https://www.youtube.com";
|
|
6
|
+
const VIDEO_ID = /^(?!videoseries|live_stream)[A-Za-z0-9_-]{11}$/;
|
|
7
|
+
const EMBED_PATH = /^\/embed\/(?:(videoseries|live_stream)|([A-Za-z0-9_-]{11}))\/?$/;
|
|
8
|
+
const URL_PATHS = /^\/(shorts|live)\/((?!videoseries|live_stream)[A-Za-z0-9_-]{11})\/?$/;
|
|
9
|
+
const SHORT_LINK_PATH = /^\/([A-Za-z0-9_-]{11})\/?$/;
|
|
10
|
+
const PLAYLIST_ID = /^[A-Za-z0-9_-]{10,64}$/;
|
|
11
|
+
const CHANNEL_ID = /^UC[A-Za-z0-9_-]{22}$/;
|
|
12
|
+
const CHANNEL_LIVE_PATH = /^\/channel\/(UC[A-Za-z0-9_-]{22})\/live\/?$/;
|
|
13
|
+
const DURATION = /^(?:(\d{1,5})h)?(?:(\d{1,6})m)?(?:(\d{1,7})s?)?$/;
|
|
14
|
+
const FRAGMENT_START = /^t=(.+)$/;
|
|
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 found(id, url) {
|
|
28
|
+
return {
|
|
29
|
+
platform: "youtube",
|
|
30
|
+
kind: "video",
|
|
31
|
+
url,
|
|
32
|
+
id,
|
|
33
|
+
variant: "iframe"
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function video(id, shorts = false) {
|
|
37
|
+
return found(id, shorts ? `${BASE}/shorts/${id}` : `${BASE}/watch?v=${id}`);
|
|
38
|
+
}
|
|
39
|
+
function playlist(id) {
|
|
40
|
+
return id !== void 0 && PLAYLIST_ID.test(id) ? found(id, `${BASE}/playlist?list=${id}`) : null;
|
|
41
|
+
}
|
|
42
|
+
function live(id) {
|
|
43
|
+
return id !== void 0 && CHANNEL_ID.test(id) ? found(id, `${BASE}/channel/${id}/live`) : null;
|
|
44
|
+
}
|
|
45
|
+
/** `key` is the query parameter that names the content, for a playlist or a channel's stream. */
|
|
46
|
+
function fromEmbed(url) {
|
|
47
|
+
const [, series, id] = EMBED_PATH.exec(url.path) ?? [];
|
|
48
|
+
if (id !== void 0) return { found: video(id) };
|
|
49
|
+
if (series === "videoseries") {
|
|
50
|
+
const list = playlist(url.query.get("list"));
|
|
51
|
+
return list && {
|
|
52
|
+
found: list,
|
|
53
|
+
key: "list"
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (series === "live_stream") {
|
|
57
|
+
const stream = live(url.query.get("channel"));
|
|
58
|
+
return stream && {
|
|
59
|
+
found: stream,
|
|
60
|
+
key: "channel"
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
function urlOptions(url, nocookie) {
|
|
66
|
+
const start = startOf(url.query) ?? seconds(FRAGMENT_START.exec(url.fragment ?? "")?.[1]);
|
|
67
|
+
return {
|
|
68
|
+
...start === void 0 ? {} : { start },
|
|
69
|
+
...nocookie ? { nocookie: true } : {}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const youtube = {
|
|
73
|
+
platform: "youtube",
|
|
74
|
+
parse(root) {
|
|
75
|
+
if (root.tag !== "iframe") return null;
|
|
76
|
+
const src = parseUrl(root.attrs.get("src") ?? "");
|
|
77
|
+
const nocookie = src !== null && YOUTUBE_NOCOOKIE_HOSTS.has(src.host);
|
|
78
|
+
if (!src || !(nocookie || YOUTUBE_HOSTS.has(src.host))) return null;
|
|
79
|
+
const player = fromEmbed(src);
|
|
80
|
+
if (!player) return null;
|
|
81
|
+
const entries = [...src.query].filter(([key]) => key !== "t" && key !== player.key).map(([key, value]) => [key, coerce(value)]);
|
|
82
|
+
const options = Object.fromEntries(entries);
|
|
83
|
+
const start = startOf(src.query);
|
|
84
|
+
if (start !== void 0) options.start = start;
|
|
85
|
+
if (nocookie) options.nocookie = true;
|
|
86
|
+
return withSize({
|
|
87
|
+
...player.found,
|
|
88
|
+
options
|
|
89
|
+
}, sizeOf(root));
|
|
90
|
+
},
|
|
91
|
+
isOwnScript: () => false,
|
|
92
|
+
match(url) {
|
|
93
|
+
if (url.host === "youtu.be") {
|
|
94
|
+
const [, id] = SHORT_LINK_PATH.exec(url.path) ?? [];
|
|
95
|
+
return id === void 0 ? null : {
|
|
96
|
+
...video(id),
|
|
97
|
+
options: urlOptions(url, false)
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const nocookie = YOUTUBE_NOCOOKIE_HOSTS.has(url.host);
|
|
101
|
+
if (!nocookie && !YOUTUBE_HOSTS.has(url.host)) return null;
|
|
102
|
+
const options = urlOptions(url, nocookie);
|
|
103
|
+
if (underPath(url.path, "/embed")) {
|
|
104
|
+
const player = fromEmbed(url);
|
|
105
|
+
return player && {
|
|
106
|
+
...player.found,
|
|
107
|
+
options
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (nocookie) return null;
|
|
111
|
+
if (/^\/watch\/?$/.test(url.path)) {
|
|
112
|
+
const id = url.query.get("v");
|
|
113
|
+
return id !== void 0 && VIDEO_ID.test(id) ? {
|
|
114
|
+
...video(id),
|
|
115
|
+
options
|
|
116
|
+
} : null;
|
|
117
|
+
}
|
|
118
|
+
if (/^\/playlist\/?$/.test(url.path)) {
|
|
119
|
+
const list = playlist(url.query.get("list"));
|
|
120
|
+
return list && {
|
|
121
|
+
...list,
|
|
122
|
+
options
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
const [, channel] = CHANNEL_LIVE_PATH.exec(url.path) ?? [];
|
|
126
|
+
const stream = live(channel);
|
|
127
|
+
if (stream) return {
|
|
128
|
+
...stream,
|
|
129
|
+
options
|
|
130
|
+
};
|
|
131
|
+
const [, kind, id] = URL_PATHS.exec(url.path) ?? [];
|
|
132
|
+
if (kind === void 0 || id === void 0) return null;
|
|
133
|
+
return {
|
|
134
|
+
...video(id, kind === "shorts"),
|
|
135
|
+
options
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
ownsFrame: youtubeFrame
|
|
139
|
+
};
|
|
140
|
+
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.replaceAll("&", "&").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 };
|