social-link-parser
Version:
Extract usernames, IDs, and metadata from social media URLs across 100+ platforms
51 lines • 1.81 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 = ['patreon.com'];
const subdomains = [];
const DOMAIN_PATTERN = createDomainPattern(domains, subdomains);
export const patreon = {
id: Platforms.Patreon,
name: 'Patreon',
color: '#F96854',
domains: domains,
subdomains: subdomains,
patterns: {
profile: new RegExp(`^https?://${DOMAIN_PATTERN}/(?:c/)?([A-Za-z0-9_-]{3,50})/?${QUERY_HASH}$`, 'i'),
handle: /^[A-Za-z0-9_-]{3,50}$/,
content: {
post: new RegExp(`^https?://${DOMAIN_PATTERN}/posts/([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, result) {
const postMatch = this.patterns.content?.post?.exec(url);
if (postMatch) {
result.ids.postId = postMatch[1];
result.metadata.isPost = true;
result.metadata.contentType = 'post';
return;
}
const profileMatch = this.patterns.profile.exec(url);
if (profileMatch) {
result.username = profileMatch[1];
result.metadata.isProfile = true;
result.metadata.contentType = 'profile';
}
},
validateHandle(handle) {
return this.patterns.handle.test(handle);
},
buildProfileUrl(username) {
return `https://patreon.com/${username}`;
},
normalizeUrl(url) {
return normalize(url);
},
};
//# sourceMappingURL=index.js.map