social-link-parser
Version:
Extract usernames, IDs, and metadata from social media URLs across 100+ platforms
64 lines • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.kofi = void 0;
const types_1 = require("../../core/types");
const url_1 = require("../../utils/url");
const url_2 = require("../../utils/url");
const constants_1 = require("../../utils/constants");
const domains = ['ko-fi.com'];
const subdomains = [];
const DOMAIN_PATTERN = (0, url_2.createDomainPattern)(domains, subdomains);
exports.kofi = {
id: types_1.Platforms.KoFi,
name: 'Ko-fi',
domains: domains,
subdomains: subdomains,
patterns: {
profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_-]{2,})(?!/)${constants_1.QUERY_HASH}$`, 'i'),
handle: /^[A-Za-z0-9_-]{2,}$/,
content: {
shop: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_-]+)/shop/?${constants_1.QUERY_HASH}$`, 'i'),
post: new RegExp(`^https?://${DOMAIN_PATTERN}/post/([A-Za-z0-9-]{2,})/?${constants_1.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) => (0, url_1.normalize)(u),
};
//# sourceMappingURL=index.js.map