UNPKG

social-link-parser

Version:

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

99 lines 3.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.medium = void 0; const types_1 = require("../../core/types"); const url_1 = require("../../utils/url"); const url_2 = require("../../utils/url"); const constants_1 = require("../../utils/constants"); const domains = ['medium.com']; const DOMAIN_PATTERN = (0, url_2.createDomainPattern)(domains); const hexId = '[a-f0-9]{12}'; exports.medium = { id: types_1.Platforms.Medium, name: 'Medium', color: '#000000', domains: domains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/@([A-Za-z0-9_-]{2,25})/?${constants_1.QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9_-]{2,25}$/, content: { profileUid: new RegExp(`^https?://${DOMAIN_PATTERN}/u/(${hexId})/?${constants_1.QUERY_HASH}$`, 'i'), profileSubdomain: new RegExp(`^https?://([A-Za-z0-9-]+)\\.medium\\.com/?${constants_1.QUERY_HASH}$`, 'i'), postUser: new RegExp(`^https?://${DOMAIN_PATTERN}/@[A-Za-z0-9_-]+/([a-z0-9-]+-${hexId})/?${constants_1.QUERY_HASH}$`, 'i'), postP: new RegExp(`^https?://${DOMAIN_PATTERN}/p/(${hexId})/?${constants_1.QUERY_HASH}$`, 'i'), postSubdomain: new RegExp(`^https?://([A-Za-z0-9-]+)\\.medium\\.com/([a-z0-9-]+-${hexId})/?${constants_1.QUERY_HASH}$`, 'i'), }, }, detect(url) { if (!url.includes('medium.com')) return false; if (this.patterns.profile.test(url)) return true; if (this.patterns.content) { for (const pattern of Object.values(this.patterns.content)) { if (pattern && pattern.test(url)) return true; } } return false; }, extract(url, result) { const postUserMatch = this.patterns.content?.postUser?.exec(url); if (postUserMatch) { result.ids.postSlug = postUserMatch[1]; result.metadata.isPost = true; result.metadata.contentType = 'post'; return; } const postPMatch = this.patterns.content?.postP?.exec(url); if (postPMatch) { result.ids.postSlug = postPMatch[1]; result.metadata.isPost = true; result.metadata.contentType = 'post'; return; } const postSubdomainMatch = this.patterns.content?.postSubdomain?.exec(url); if (postSubdomainMatch) { result.ids.postSlug = postSubdomainMatch[2]; result.metadata.isPost = true; result.metadata.contentType = 'post'; return; } const profileMatch = this.patterns.profile.exec(url); if (profileMatch) { result.username = profileMatch[1]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; return; } const profileUidMatch = this.patterns.content?.profileUid?.exec(url); if (profileUidMatch) { result.userId = profileUidMatch[1]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; return; } const profileSubdomainMatch = this.patterns.content?.profileSubdomain?.exec(url); if (profileSubdomainMatch) { result.username = profileSubdomainMatch[1]; result.metadata.isProfile = true; result.metadata.contentType = 'profile'; } }, validateHandle(handle) { return this.patterns.handle.test(handle); }, buildProfileUrl(username) { return `https://medium.com/@${username}`; }, buildContentUrl(contentType, id) { if (contentType === 'post') { return `https://medium.com/p/${id}`; } return `https://medium.com/${id}`; }, normalizeUrl(url) { return (0, url_1.normalize)(url.replace(/[?&](source|utm_[^&]+)=[^&]+/g, '')); }, }; //# sourceMappingURL=index.js.map