UNPKG

social-link-parser

Version:

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

64 lines 2.35 kB
import { Platforms } from '../../core/types.js'; import { normalize } from '../../utils/url.js'; import { QUERY_HASH } from '../../utils/constants.js'; const handlePattern = /^[A-Za-z0-9_-]{3,40}$/; const profileA = new RegExp(`^https?://([A-Za-z0-9.-]+)/a/([A-Za-z0-9_-]{3,40})/?${QUERY_HASH}$`, 'i'); const profileChannel = new RegExp(`^https?://([A-Za-z0-9.-]+)/video-channels/([A-Za-z0-9_-]{3,40})/?${QUERY_HASH}$`, 'i'); const videoWatch = new RegExp(`^https?://([A-Za-z0-9.-]+)/videos/watch/([A-Za-z0-9_-]{8,})/?${QUERY_HASH}$`, 'i'); const videoEmbed = new RegExp(`^https?://([A-Za-z0-9.-]+)/videos/embed/([A-Za-z0-9_-]{8,})/?${QUERY_HASH}$`, 'i'); export const peertube = { id: Platforms.PeerTube, name: 'PeerTube', color: '#F1680D', domains: [], patterns: { profile: profileA, handle: handlePattern, content: { videoWatch, videoEmbed, }, }, detect(url) { if (!/\/videos\//i.test(url) && !/\/a\//i.test(url) && !/\/video-channels\//i.test(url)) return false; return profileA.test(url) || profileChannel.test(url) || videoWatch.test(url) || videoEmbed.test(url); }, extract(url, result) { const v = videoWatch.exec(url) || videoEmbed.exec(url); if (v) { result.ids.videoId = v[2]; result.metadata.isVideo = true; result.metadata.contentType = 'video'; return; } const pA = profileA.exec(url); if (pA) { result.username = pA[2]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; return; } const pC = profileChannel.exec(url); if (pC) { result.username = pC[2]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; } }, validateHandle(handle) { return handlePattern.test(handle); }, buildProfileUrl(username) { return `https://peertube.social/a/${username}`; }, buildContentUrl(type, id) { if (type === 'video') return `https://peertube.social/videos/watch/${id}`; return `https://peertube.social/${id}`; }, normalizeUrl(url) { return normalize(url); }, }; //# sourceMappingURL=index.js.map