UNPKG

social-link-parser

Version:

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

58 lines 2 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 = ['dev.to']; const subdomains = []; const DOMAIN_PATTERN = createDomainPattern(domains, subdomains); const usernamePattern = /^[A-Za-z0-9_-]{2,32}$/; export const devto = { id: Platforms.DevTo, name: 'Dev.to', color: '#0A0A0A', domains: domains, subdomains: subdomains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_-]{2,32})/?${QUERY_HASH}$`, 'i'), handle: usernamePattern, content: { post: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_-]{2,32})/([A-Za-z0-9-]+)/?${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)); }, extract(url, result) { const post = this.patterns.content?.post?.exec(url); if (post) { result.username = post[1]; result.ids.postSlug = post[2]; result.metadata.isPost = true; result.metadata.contentType = 'post'; return; } const prof = this.patterns.profile.exec(url); if (prof) { result.username = prof[1]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; } }, validateHandle(handle) { return usernamePattern.test(handle); }, buildProfileUrl(username) { return `https://dev.to/${username}`; }, buildContentUrl(contentType, id) { if (contentType === 'post') return `https://dev.to/${id}`; return `https://dev.to/${id}`; }, normalizeUrl(url) { return normalize(url); }, }; //# sourceMappingURL=index.js.map