UNPKG

social-link-parser

Version:

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

81 lines 3.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pinterest = 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 = ['pinterest.com', 'pin.it']; const DOMAIN_PATTERN = (0, url_2.createDomainPattern)(domains); exports.pinterest = { id: types_1.Platforms.Pinterest, name: 'Pinterest', color: '#BD081C', domains: domains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_]{3,15})${constants_1.QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9_]{3,15}$/, content: { pin: new RegExp(`^https?://${DOMAIN_PATTERN}/pin/(\\d+)/?${constants_1.QUERY_HASH}$`, 'i'), board: new RegExp(`^https?://${DOMAIN_PATTERN}/(?!pin/)([A-Za-z0-9_]{3,15})/([A-Za-z0-9_-]{2,})/?${constants_1.QUERY_HASH}$`, 'i'), short: new RegExp(`^https?://(?:www\\.)?pin\\.it/([A-Za-z0-9]+)/?${constants_1.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 (0, url_1.normalize)(url.replace(/[?&]utm_[^&]+/g, '')); }, }; //# sourceMappingURL=index.js.map