social-link-parser
Version:
Extract usernames, IDs, and metadata from social media URLs across 100+ platforms
74 lines • 2.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.telegram = 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 = ['t.me', 'telegram.me'];
const subdomains = [];
const DOMAIN_PATTERN = (0, url_2.createDomainPattern)(domains, subdomains);
exports.telegram = {
id: types_1.Platforms.Telegram,
name: 'Telegram',
color: '#0088CC',
domains: domains,
subdomains: subdomains,
patterns: {
profile: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_]{5,32})/?${constants_1.QUERY_HASH}$`, 'i'),
handle: /^[A-Za-z0-9_]{5,32}$/,
content: {
channel: new RegExp(`^https?://${DOMAIN_PATTERN}/s/([A-Za-z0-9_]{5,32})/?${constants_1.QUERY_HASH}$`, 'i'),
post: new RegExp(`^https?://${DOMAIN_PATTERN}/([A-Za-z0-9_]{5,32})/(\\d+)/?${constants_1.QUERY_HASH}$`, 'i'),
join: new RegExp(`^https?://${DOMAIN_PATTERN}/joinchat/([A-Za-z0-9_-]{10,})/?${constants_1.QUERY_HASH}$`, 'i'),
},
},
detect(url) {
if (!this.domains.some(d => url.includes(d)))
return false;
return this.patterns.profile.test(url) ||
!!(this.patterns.content?.channel?.test(url)) ||
!!(this.patterns.content?.post?.test(url)) ||
!!(this.patterns.content?.join?.test(url));
},
extract(url, result) {
const joinMatch = this.patterns.content?.join?.exec(url);
if (joinMatch) {
result.ids.joinCode = joinMatch[1];
result.metadata.isJoin = true;
result.metadata.contentType = 'join';
return;
}
const channelMatch = this.patterns.content?.channel?.exec(url);
if (channelMatch) {
result.ids.channelName = channelMatch[1];
result.metadata.isChannel = true;
result.metadata.contentType = 'channel';
return;
}
const postMatch = this.patterns.content?.post?.exec(url);
if (postMatch) {
result.ids.channelName = postMatch[1];
result.ids.postId = postMatch[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';
}
},
validateHandle(handle) {
return this.patterns.handle.test(handle.replace('@', ''));
},
buildProfileUrl(username) {
return `https://t.me/${username}`;
},
normalizeUrl(url) {
return (0, url_1.normalize)(url);
},
};
//# sourceMappingURL=index.js.map