@social-embed/lib

@social-embed/lib is a zero-dependency JavaScript library for detecting media platforms, extracting IDs from URLs, and generating embeddable URLs. It runs in any JavaScript environment — browsers, Node.js, edge workers.

import { convertUrlToEmbedUrl } from "@social-embed/lib";
convertUrlToEmbedUrl("https://youtu.be/Bd8_vO5zrjo");
// "https://www.youtube.com/embed/Bd8_vO5zrjo"

Key features

  • Universal conversionconvertUrlToEmbedUrl() handles all providers in one call
  • Provider detectiongetProviderFromUrl() identifies which platform a URL belongs to
  • Extensible registry — Add custom providers with defaultRegistry.register()
  • Zero dependencies — No npm dependencies, no DOM requirement
  • Type-safe — Full TypeScript with exported EmbedProvider type and EmbedProviderRegistry class
  • Installation — npm, CDN, and bundler setup
  • Examples — Universal conversion, per-provider functions, custom providers
  • API reference — All exports, types, and function signatures
  • Providers — The EmbedProvider interface and registry system

How it fits

This library handles steps 1-3 of the social-embed pipeline (detect → extract → build). It is used internally by the <o-embed> web component, but you can use it standalone for:

  • Server-side URL validation — Check if a URL is from a recognized provider before storing it
  • Custom renderers — Build your own embed UI in React, Vue, Svelte, or any framework
  • Content pipelines — Transform URLs in batch processing, API endpoints, or build steps