UNPKG

social-link-parser

Version:

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

62 lines 2.39 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 = ['bitbucket.org']; const subdomains = []; const DOMAIN_PATTERN = createDomainPattern(domains, subdomains); export const bitbucket = { id: Platforms.Bitbucket, name: 'Bitbucket', domains: domains, subdomains: subdomains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_-]{2,30})/?${QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9_-]{2,30}$/, content: { repo: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_-]{2,30})/([A-Za-z0-9._-]+)/?${QUERY_HASH}$`, 'i'), snippet: new RegExp(`^https?://${DOMAIN_PATTERN}/snippets/([A-Za-z0-9_-]{2,30})/([A-Za-z0-9]{2,})/?${QUERY_HASH}$`, 'i'), }, }, detect(url) { if (!this.domains.some(domain => url.includes(domain))) return false; return this.patterns.profile.test(url) || !!(this.patterns.content?.repo?.test(url)) || !!(this.patterns.content?.snippet?.test(url)); }, extract(url, result) { const snippetMatch = this.patterns.content?.snippet?.exec(url); if (snippetMatch) { result.username = snippetMatch[1]; result.ids.snippetId = snippetMatch[2]; result.metadata.isSnippet = true; result.metadata.contentType = 'snippet'; return; } const repoMatch = this.patterns.content?.repo?.exec(url); if (repoMatch) { result.username = repoMatch[1]; result.ids.repoName = repoMatch[2]; result.metadata.isRepo = true; result.metadata.contentType = 'repo'; return; } const profileMatch = this.patterns.profile.exec(url); if (profileMatch) { result.username = profileMatch[1]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; } }, validateHandle(handle) { return this.patterns.handle.test(handle); }, buildProfileUrl(username) { return `https://bitbucket.org/${username}`; }, normalizeUrl(url) { return normalize(url); }, }; //# sourceMappingURL=index.js.map