Examples
@social-embed/lib unifies URL detection and parsing for various services (YouTube, Vimeo, Spotify, etc.). It offers both:
- Universal – using
convertUrlToEmbedUrl(url)
to handle most providers automatically. - Provider-specific – extracting IDs manually with functions like
getYouTubeIdFromUrl
,getDailyMotionIdFromUrl
, etc.
Quick Overview
Function | Description |
---|---|
convertUrlToEmbedUrl(url) | Converts a recognized media URL into its <iframe> -friendly URL. Returns "" if unrecognized or invalid. |
isValidUrl(url) | Checks if the string is a syntactically valid URL. |
getYouTubeIdFromUrl(url) | Extracts a YouTube video ID (e.g. FTQbiNvZqaY ) from various YouTube link forms. |
getYouTubeEmbedUrlFromId(id) | Builds https://www.youtube.com/embed/<id> |
… (DailyMotion, Spotify, etc.) | Similar patterns for DailyMotion, Spotify, Vimeo, Loom, Wistia, EdPuzzle, etc. See details below. |
1. Universal Conversion
convertUrlToEmbedUrl(url)
-
What it does
- Detects the provider (YouTube, Vimeo, Spotify, Loom, etc.) by testing known URL patterns
- Extracts the necessary ID(s)
- Returns a fully embeddable URL (e.g.
https://www.youtube.com/embed/FTQbiNvZqaY
)
-
Invalid or Unrecognized
- If the URL is valid but from an unknown service,
convertUrlToEmbedUrl
returns the normalized URL or""
depending on the version of the library. - If the string is not a valid URL at all, it returns
""
.
- If the URL is valid but from an unknown service,
Example: Spotify
Example: DailyMotion
Example: Vimeo
Example: EdPuzzle
2. URL Validation
isValidUrl(url)
- What it does
- Uses
new URL()
internally to verify if the string is a syntactically valid URL. - Returns
true
orfalse
.
- Uses
3. Provider-Specific Functions
If you need more control, each provider offers a “get ID” and “build embed URL” approach. For example:
YouTube
getYouTubeIdFromUrl(url: string): string
Extracts a short ID (e.g."FTQbiNvZqaY"
) from YouTube links.getYouTubeEmbedUrlFromId(id: string): string
Builds"https://www.youtube.com/embed/<id>"
.
Example:
DailyMotion
Vimeo
Loom
EdPuzzle
Wistia
4. getProviderFromUrl(url)
If you’d like to detect the provider without immediately converting the URL, call getProviderFromUrl(url)
. It returns an object with .name
, .getIdFromUrl()
, and .getEmbedUrlFromId()
methods:
- If
getProviderFromUrl(url)
returnsundefined
, it means no known provider was detected.
5. Adding a New Provider
If you have a custom service (e.g. MyCustom), you can implement your own detection and embed logic.
-
Implement an object with these fields:
-
Register it in a registry, or if you’re using
convertUrlToEmbedUrl
, add your provider to the default registry (in@social-embed/lib
code or your own code): -
Done. The library will detect “MyCustom” for your custom URL pattern. If you prefer, you can keep a separate registry if you don’t want to modify the default one.
Summary
- Universal approach:
convertUrlToEmbedUrl(url)
. - Manual approach: provider-specific ID extraction + embed link building.
- Custom providers: add them via the registry or use your own logic.
If you run into any issues or want to extend coverage (e.g., new services like Bandcamp
, SoundCloud
), feel free to open a PR or an issue.
Check out the tests on GitHub or the Playground for more real-world usage.