social-link-parser
Version:
Extract usernames, IDs, and metadata from social media URLs across 100+ platforms
71 lines • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.substack = 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 = ['substack.com'];
const subdomains = [];
const DOMAIN_PATTERN = (0, url_2.createDomainPattern)(domains, subdomains);
exports.substack = {
id: types_1.Platforms.Substack,
name: 'Substack',
color: '#FF6719',
domains: domains,
subdomains: subdomains,
patterns: {
profile: new RegExp(`^https?://([a-z0-9-]{2,})\\.substack\\.com/?${constants_1.QUERY_HASH}$`, 'i'),
handle: /^[a-z0-9-]{2,}$/i,
content: {
post: new RegExp(`^https?://([a-z0-9-]{2,})\\.substack\\.com/p/([a-z0-9-]+)/?${constants_1.QUERY_HASH}$`, 'i'),
profileNew: new RegExp(`^https?://${DOMAIN_PATTERN}/@([a-z0-9-]{2,})/?${constants_1.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)) ||
!!(this.patterns.content?.profileNew?.test(url));
},
extract(url, result) {
const profileNewMatch = this.patterns.content?.profileNew?.exec(url);
if (profileNewMatch) {
result.username = profileNewMatch[1];
result.metadata.isProfile = true;
result.metadata.contentType = 'newsletter';
return;
}
const postMatch = this.patterns.content?.post?.exec(url);
if (postMatch) {
result.username = postMatch[1];
result.ids.postSlug = postMatch[2];
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 = 'newsletter';
return;
}
},
validateHandle(handle) {
return this.patterns.handle.test(handle);
},
buildProfileUrl(username) {
return `https://${username}.substack.com`;
},
buildContentUrl(contentType, id) {
if (contentType === 'post') {
return `https://substack.com/p/${id}`;
}
return `https://substack.com/${contentType}/${id}`;
},
normalizeUrl(url) {
return (0, url_1.normalize)(url);
},
};
//# sourceMappingURL=index.js.map