social-link-parser
Version:
Extract usernames, IDs, and metadata from social media URLs across 100+ platforms
59 lines • 2.04 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 = ['threads.net'];
const subdomains = [];
const DOMAIN_PATTERN = createDomainPattern(domains, subdomains);
export const threads = {
id: Platforms.Threads,
name: 'Threads',
domains: domains,
subdomains: subdomains,
patterns: {
profile: new RegExp(`^https?://${DOMAIN_PATTERN}/@([a-zA-Z0-9._]{2,30})/?(?!post/)${QUERY_HASH}$`, 'i'),
handle: /^@?[a-zA-Z0-9._]{2,30}$/,
content: {
post: new RegExp(`^https?://${DOMAIN_PATTERN}/@([a-zA-Z0-9._]{2,30})/post/([A-Za-z0-9]{2,})/?${QUERY_HASH}$`, 'i'),
},
},
detect(url) {
if (!domains.some((domain) => url.includes(domain)))
return false;
if (this.patterns.profile.test(url))
return true;
if (this.patterns.content) {
for (const pattern of Object.values(this.patterns.content)) {
if (pattern && pattern.test(url))
return true;
}
}
return false;
},
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._]{2,30}$/i.test(h);
},
buildProfileUrl(u) {
return `https://threads.net/@${u.replace('@', '')}`;
},
normalizeUrl(u) {
return normalize(u);
},
};
//# sourceMappingURL=index.js.map