UNPKG

social-link-parser

Version:

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

61 lines 2.35 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 = ['gitlab.com']; const subdomains = []; const DOMAIN_PATTERN = createDomainPattern(domains, subdomains); export const gitlab = { id: Platforms.GitLab, name: 'GitLab', domains: domains, subdomains: subdomains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9-]{2,255})/?${QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9-]{1,255}$/, content: { project: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9-]{2,255})/([A-Za-z0-9._-]+)/?${QUERY_HASH}$`, 'i'), snippet: new RegExp(`^https?://${DOMAIN_PATTERN}/[A-Za-z0-9-]+/[A-Za-z0-9._-]+/-/snippets/(\\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?.project?.test(url)) || !!(this.patterns.content?.snippet?.test(url)); }, extract(url, result) { const snippetMatch = this.patterns.content?.snippet?.exec(url); if (snippetMatch) { result.ids.snippetId = snippetMatch[1]; result.metadata.isSnippet = true; result.metadata.contentType = 'snippet'; return; } const projectMatch = this.patterns.content?.project?.exec(url); if (projectMatch) { result.username = projectMatch[1]; result.ids.projectName = projectMatch[2]; result.metadata.isProject = true; result.metadata.contentType = 'project'; 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://gitlab.com/${username}`; }, normalizeUrl(url) { return normalize(url); }, }; //# sourceMappingURL=index.js.map