UNPKG

social-link-parser

Version:

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

61 lines 2.3 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 = ['vk.com']; const subdomains = ['m', 'new']; const DOMAIN_PATTERN = createDomainPattern(domains, subdomains); const usernamePattern = /^[a-zA-Z0-9_.]{3,32}$/; const numericIdPattern = /^id\d{1,15}$/; export const vk = { id: Platforms.VKontakte, name: 'VK', color: '#4C75A3', domains: domains, subdomains: subdomains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/(?:([a-zA-Z0-9_.]{3,32})|(id\\d{1,15}))(?:/)?/?${QUERY_HASH}$`, 'i'), handle: usernamePattern, content: { post: new RegExp(`^https?://${DOMAIN_PATTERN}/(wall-?\\d+_\\d+)/?${QUERY_HASH}$`, 'i'), postQuery: new RegExp(`^https?://${DOMAIN_PATTERN}/[^?]+\\?w=(wall-?\\d+_\\d+)${QUERY_HASH}`, 'i'), }, }, detect(url) { if (!this.domains.some(domain => url.includes(domain))) return false; return this.patterns.profile.test(url) || !!(this.patterns.content?.post?.test(url)) || !!(this.patterns.content?.postQuery?.test(url)); }, extract(url, result) { const pMatch = this.patterns.content?.post?.exec(url) || this.patterns.content?.postQuery?.exec(url); if (pMatch) { result.ids.postId = pMatch[1]; result.metadata.isPost = true; result.metadata.contentType = 'post'; return; } const prof = this.patterns.profile.exec(url); if (prof) { result.username = prof[1] || prof[2]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; } }, validateHandle(handle) { return usernamePattern.test(handle) || numericIdPattern.test(handle); }, buildProfileUrl(handle) { return `https://vk.com/${handle}`; }, buildContentUrl(contentType, id) { if (contentType === 'post') return `https://vk.com/wall${id}`; return `https://vk.com/${id}`; }, normalizeUrl(url) { return normalize(url); }, }; //# sourceMappingURL=index.js.map