social-link-parser
Version:
Extract usernames, IDs, and metadata from social media URLs across 100+ platforms
51 lines • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = void 0;
const platforms_1 = require("../platforms");
function parse(url) {
let processedUrl = url.trim();
if (!processedUrl.match(/^https?:\/\//i) && !processedUrl.startsWith('mailto:') && !processedUrl.startsWith('tel:')) {
const isEmail = /^[^@]+@[^@]+\.[^@]+$/.test(processedUrl);
if (processedUrl.includes('.') && !isEmail) {
processedUrl = `https://${processedUrl}`;
}
}
const result = {
isValid: false,
originalUrl: url,
normalizedUrl: processedUrl,
platform: null,
ids: {},
metadata: {}
};
for (const [_, module] of platforms_1.registry) {
if (module.detect(processedUrl)) {
result.platform = module.id;
result.platformName = module.name;
module.extract(processedUrl, result);
result.normalizedUrl = module.normalizeUrl(processedUrl);
if (result.username || Object.keys(result.ids).length > 0 || (result.metadata && Object.keys(result.metadata).length > 0)) {
result.isValid = true;
}
if (module.getEmbedInfo) {
const embedInfo = module.getEmbedInfo(processedUrl, result);
if (embedInfo) {
result.embedData = {
platform: module.id,
type: embedInfo.type || 'iframe',
contentId: result.ids.videoId || result.ids.postId || result.ids.trackId || '',
embedUrl: embedInfo.embedUrl,
options: embedInfo.options
};
if (embedInfo.isEmbedAlready) {
result.metadata.isEmbed = true;
}
}
}
break;
}
}
return result;
}
exports.parse = parse;
//# sourceMappingURL=parser.js.map