UNPKG

social-link-parser

Version:

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

75 lines 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.reddit = 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 = ['reddit.com', 'redd.it']; const DOMAIN_PATTERN = (0, url_2.createDomainPattern)(domains); exports.reddit = { id: types_1.Platforms.Reddit, name: 'Reddit', color: '#FF4500', domains: domains, patterns: { profile: new RegExp(`^https?://${DOMAIN_PATTERN}/(?:user|u)/([A-Za-z0-9_-]{3,20})/?${constants_1.QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9_-]{3,20}$/, content: { subreddit: new RegExp(`^https?://${DOMAIN_PATTERN}/r/([A-Za-z0-9_]{3,21})/?${constants_1.QUERY_HASH}$`, 'i'), post: new RegExp(`^https?://${DOMAIN_PATTERN}/r/[A-Za-z0-9_]+/comments/([a-z0-9]{2,})(?:/[^?#]+)?/?${constants_1.QUERY_HASH}$`, 'i'), shortPost: 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; if (this.patterns.profile.test(url)) return true; for (const p of Object.values(this.patterns.content || {})) { if (p && p.test(url)) return true; } return false; }, extract(url, result) { const subredditMatch = this.patterns.content?.subreddit?.exec(url); if (subredditMatch) { result.ids.subreddit = subredditMatch[1]; result.metadata.isSubreddit = true; result.metadata.contentType = 'subreddit'; return; } const postMatch = this.patterns.content?.post?.exec(url); if (postMatch) { result.ids.postId = postMatch[1]; result.metadata.isPost = true; result.metadata.contentType = 'post'; return; } const shortPostMatch = this.patterns.content?.shortPost?.exec(url); if (shortPostMatch) { result.ids.postId = shortPostMatch[1]; 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) { const cleaned = handle.replace(/^u\//, ''); return /^[A-Za-z0-9_-]{3,20}$/.test(cleaned); }, buildProfileUrl(username) { return `https://reddit.com/user/${username}`; }, normalizeUrl(url) { return (0, url_1.normalize)(url.replace(/[?&]utm_[^&]+/g, '')); }, }; //# sourceMappingURL=index.js.map