social-link-parser
Version:
Extract usernames, IDs, and metadata from social media URLs across 100+ platforms
50 lines • 2.01 kB
JavaScript
import { Platforms } from '../../core/types.js';
import { normalize } from '../../utils/url.js';
import { createDomainPattern } from '../../utils/url.js';
import { QUERY_HASH } from '../../utils/constants.js';
const domains = ['bsky.app'];
const subdomains = [];
const DOMAIN_PATTERN = createDomainPattern(domains, subdomains);
export const bluesky = {
id: Platforms.Bluesky,
name: 'Bluesky',
domains: domains,
subdomains: subdomains,
patterns: {
profile: new RegExp(`^https?://${DOMAIN_PATTERN}/profile/([a-zA-Z0-9.-]+\\.[a-zA-Z]+|did:[a-z]+:[a-zA-Z0-9]+)/?${QUERY_HASH}$`, 'i'),
handle: /^[a-zA-Z0-9.-]+\.[a-zA-Z]+$/,
content: {
post: new RegExp(`^https?://${DOMAIN_PATTERN}/profile/([a-zA-Z0-9.-]+\\.[a-zA-Z]+|did:[a-z]+:[a-zA-Z0-9]+)/post/([a-zA-Z0-9]{2,})/?${QUERY_HASH}$`, 'i'),
},
},
detect(url) {
if (!this.domains.some(domain => url.includes(domain)))
return false;
return this.patterns.profile.test(url) || !!(this.patterns.content?.post?.test(url));
},
extract(url, res) {
const postMatch = this.patterns.content?.post?.exec(url);
if (postMatch) {
res.username = postMatch[1];
res.ids.postId = postMatch[2];
res.metadata.isPost = true;
res.metadata.contentType = 'post';
return;
}
const profileMatch = this.patterns.profile.exec(url);
if (profileMatch) {
res.username = profileMatch[1];
res.metadata.isProfile = true;
res.metadata.contentType = 'profile';
}
},
validateHandle(h) {
return /^[a-zA-Z0-9.-]+\.[a-zA-Z]+$/.test(h) || /^did:[a-z]+:[a-zA-Z0-9]+$/.test(h);
},
buildProfileUrl(u) {
const handle = u.includes('.') || u.startsWith('did:') ? u : `${u}.bsky.social`;
return `https://bsky.app/profile/${handle}`;
},
normalizeUrl(u) { return normalize(u); },
};
//# sourceMappingURL=index.js.map