social-link-parser
Version:
Extract usernames, IDs, and metadata from social media URLs across 100+ platforms
96 lines • 3.82 kB
JavaScript
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 = ['medium.com'];
const DOMAIN_PATTERN = createDomainPattern(domains);
const hexId = '[a-f0-9]{12}';
export const medium = {
id: Platforms.Medium,
name: 'Medium',
color: '#000000',
domains: domains,
patterns: {
profile: new RegExp(`^https?://${DOMAIN_PATTERN}/@([A-Za-z0-9_-]{2,25})/?${QUERY_HASH}$`, 'i'),
handle: /^[A-Za-z0-9_-]{2,25}$/,
content: {
profileUid: new RegExp(`^https?://${DOMAIN_PATTERN}/u/(${hexId})/?${QUERY_HASH}$`, 'i'),
profileSubdomain: new RegExp(`^https?://([A-Za-z0-9-]+)\\.medium\\.com/?${QUERY_HASH}$`, 'i'),
postUser: new RegExp(`^https?://${DOMAIN_PATTERN}/@[A-Za-z0-9_-]+/([a-z0-9-]+-${hexId})/?${QUERY_HASH}$`, 'i'),
postP: new RegExp(`^https?://${DOMAIN_PATTERN}/p/(${hexId})/?${QUERY_HASH}$`, 'i'),
postSubdomain: new RegExp(`^https?://([A-Za-z0-9-]+)\\.medium\\.com/([a-z0-9-]+-${hexId})/?${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 normalize(url.replace(/[?&](source|utm_[^&]+)=[^&]+/g, ''));
},
};
//# sourceMappingURL=index.js.map