social-link-parser
Version:
Extract usernames, IDs, and metadata from social media URLs across 100+ platforms
61 lines • 2.27 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 = ['ko-fi.com'];
const subdomains = [];
const DOMAIN_PATTERN = createDomainPattern(domains, subdomains);
export const kofi = {
id: Platforms.KoFi,
name: 'Ko-fi',
domains: domains,
subdomains: subdomains,
patterns: {
profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_-]{2,})(?!/)${QUERY_HASH}$`, 'i'),
handle: /^[A-Za-z0-9_-]{2,}$/,
content: {
shop: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_-]+)/shop/?${QUERY_HASH}$`, 'i'),
post: new RegExp(`^https?://${DOMAIN_PATTERN}/post/([A-Za-z0-9-]{2,})/?${QUERY_HASH}$`, 'i'),
},
},
detect(url) {
if (!this.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.ids.postId = postMatch[1];
res.metadata.isPost = true;
res.metadata.contentType = 'post';
return;
}
const shopMatch = this.patterns.content?.shop?.exec(url);
if (shopMatch) {
res.username = shopMatch[1];
res.ids.shop = 'shop';
res.metadata.isShop = true;
res.metadata.contentType = 'shop';
return;
}
const profileMatch = this.patterns.profile.exec(url);
if (profileMatch) {
res.username = profileMatch[1];
res.metadata.isProfile = true;
res.metadata.contentType = 'profile';
}
},
validateHandle: (h) => /^[A-Za-z0-9_-]{2,}$/.test(h),
buildProfileUrl: (u) => `https://ko-fi.com/${u}`,
normalizeUrl: (u) => normalize(u),
};
//# sourceMappingURL=index.js.map