UNPKG

social-link-parser

Version:

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

76 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.github = void 0; const types_1 = require("../../core/types"); const url_1 = require("../../utils/url"); const constants_1 = require("../../utils/constants"); const domains = ['github.com', 'gist.github.com', 'raw.githubusercontent.com']; exports.github = { id: types_1.Platforms.GitHub, name: 'GitHub', domains: domains, patterns: { profile: new RegExp(`^https?://github\\.com/([A-Za-z0-9-]{2,39})/?${constants_1.QUERY_HASH}$`, 'i'), handle: /^[A-Za-z0-9-]{1,39}$/, content: { repo: new RegExp(`^https?://github\\.com/([A-Za-z0-9-]{2,39})/([A-Za-z0-9._-]+)/?${constants_1.QUERY_HASH}$`, 'i'), gist: new RegExp(`^https?://gist\\.github\\.com/([A-Za-z0-9-]{2,39})/([a-fA-F0-9]{8,})/?${constants_1.QUERY_HASH}$`, 'i'), raw: new RegExp(`^https?://raw\\.githubusercontent\\.com/([A-Za-z0-9-]{2,39})/([A-Za-z0-9._-]+)/(.+)${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; 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 gistMatch = this.patterns.content?.gist?.exec(url); if (gistMatch) { result.username = gistMatch[1]; result.ids.gistId = gistMatch[2]; result.metadata.isGist = true; result.metadata.contentType = 'gist'; return; } const rawMatch = this.patterns.content?.raw?.exec(url); if (rawMatch) { result.username = rawMatch[1]; result.ids.repoName = rawMatch[2]; result.metadata.isRaw = true; result.metadata.contentType = 'raw'; return; } const repoMatch = this.patterns.content?.repo?.exec(url); if (repoMatch) { result.username = repoMatch[1]; result.ids.repoName = repoMatch[2]; result.metadata.isRepo = true; result.metadata.contentType = 'repo'; 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); }, buildProfileUrl(username) { return `https://github.com/${username}`; }, normalizeUrl(url) { return (0, url_1.normalize)(url.replace(/^http:\/\//, 'https://')); }, }; //# sourceMappingURL=index.js.map