This repository has no description
1export const getDomain = (url: string) => {
2 return new URL(url).hostname;
3};
4
5export const getUrlFromSlug = (slug: string[]) => {
6 const decoded = slug.map(decodeURIComponent);
7 const url = decoded.join('/');
8
9 // only normalize if the scheme has a single slash after it (i.e. malformed)
10 const normalizedUrl = /^([a-zA-Z]+:)\/[^/]/.test(url)
11 ? url.replace(/^([a-zA-Z]+:)\//, '$1//')
12 : url;
13
14 return normalizedUrl;
15};
16
17export const isCollectionPage = (url: string = window.location.pathname) => {
18 try {
19 const { pathname } = new URL(url);
20 // expect /profile/:handle/collections/:id
21 const pattern = /^\/profile\/[^/]+\/collections\/[^/]+\/?$/;
22 return pattern.test(pathname);
23 } catch {
24 // invalid URL
25 return false;
26 }
27};
28
29export enum SupportedPlatform {
30 BLUESKY_POST = 'bluesky post',
31 SEMBLE_COLLECTION = 'semble collection',
32}
33
34export const detectUrlPlatform = (url: string): SupportedPlatform | null => {
35 if (isCollectionPage(url)) {
36 return SupportedPlatform.SEMBLE_COLLECTION;
37 }
38
39 try {
40 const parsedUrl = new URL(url);
41
42 // bluesky posts
43 // https://bsky.app/profile/handle/post/id
44 if (
45 parsedUrl.hostname === 'bsky.app' &&
46 parsedUrl.pathname.includes('/post/')
47 ) {
48 return SupportedPlatform.BLUESKY_POST;
49 }
50
51 return null; // no supported service detected
52 } catch (e) {
53 // invalid url
54 return null;
55 }
56};