UNPKG

social-link-parser

Version:

Extract usernames, IDs, and metadata from social media URLs across 100+ platforms

60 lines 2.37 kB
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 = ['shopmy.us']; const subdomains = []; const DOMAIN_PATTERN = createDomainPattern(domains, subdomains); export const shopmy = { id: Platforms.ShopMy, name: 'ShopMy', domains: domains, subdomains: subdomains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/(?!collections/)(?!p/)([A-Za-z0-9_-]{2,})/?${QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9_-]{2,}$/, content: { collection: new RegExp(`^https?://${DOMAIN_PATTERN}/collections/(\\d+)/?${QUERY_HASH}$`, 'i'), product: new RegExp(`^https?://${DOMAIN_PATTERN}/p/([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, result) { const collectionMatch = this.patterns.content?.collection?.exec(url); if (collectionMatch) { result.ids.collectionId = collectionMatch[1]; result.metadata.isCollection = true; result.metadata.contentType = 'collection'; return; } const productMatch = this.patterns.content?.product?.exec(url); if (productMatch) { result.ids.productId = productMatch[1]; result.metadata.isProduct = true; result.metadata.contentType = 'product'; return; } const profileMatch = this.patterns.profile.exec(url); if (profileMatch) { result.username = profileMatch[1]; result.metadata.isProfile = true; result.metadata.contentType = 'storefront'; } }, validateHandle: (h) => /^[A-Za-z0-9_-]{2,}$/.test(h), buildProfileUrl: (username) => `https://shopmy.us/${username}`, normalizeUrl: (u) => normalize(u), }; //# sourceMappingURL=index.js.map