UNPKG

social-link-parser

Version:

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

78 lines 2.95 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 = ['pinterest.com', 'pin.it']; const DOMAIN_PATTERN = createDomainPattern(domains); export const pinterest = { id: Platforms.Pinterest, name: 'Pinterest', color: '#BD081C', domains: domains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_]{3,15})${QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9_]{3,15}$/, content: { pin: new RegExp(`^https?://${DOMAIN_PATTERN}/pin/(\\d+)/?${QUERY_HASH}$`, 'i'), board: new RegExp(`^https?://${DOMAIN_PATTERN}/(?!pin/)([A-Za-z0-9_]{3,15})/([A-Za-z0-9_-]{2,})/?${QUERY_HASH}$`, 'i'), short: new RegExp(`^https?://(?:www\\.)?pin\\.it/([A-Za-z0-9]+)/?${QUERY_HASH}$`, 'i'), }, }, detect(url) { if (!this.domains.some(domain => url.includes(domain))) return false; if (this.patterns.content) { for (const pattern of Object.values(this.patterns.content)) { if (pattern && pattern.test(url)) return true; } } if (this.patterns.profile.test(url)) return true; return false; }, extract(url, result) { const pinMatch = this.patterns.content?.pin?.exec(url); if (pinMatch) { result.ids.pinId = pinMatch[1]; result.metadata.isPin = true; result.metadata.contentType = 'pin'; return; } const boardMatch = this.patterns.content?.board?.exec(url); if (boardMatch && boardMatch[2]) { result.ids.boardName = boardMatch[2]; result.username = boardMatch[1]; result.metadata.isBoard = true; result.metadata.contentType = 'board'; return; } const profileMatch = this.patterns.profile.exec(url); if (profileMatch) { const cleanUrl = url.split('?')[0].split('#')[0]; if (cleanUrl.endsWith('/')) { return; } result.username = profileMatch[1]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; return; } const shortMatch = this.patterns.content?.short?.exec(url); if (shortMatch) { result.ids.shortId = shortMatch[1]; result.metadata.isShort = true; result.metadata.contentType = 'short'; } }, validateHandle(handle) { return this.patterns.handle.test(handle); }, buildProfileUrl(username) { return `https://pinterest.com/${username}`; }, normalizeUrl(url) { return normalize(url.replace(/[?&]utm_[^&]+/g, '')); }, }; //# sourceMappingURL=index.js.map