# Embed Extension
## Example
```svelte
{#if $editor}
{/if}
```
## Documentation
## Usage
Add `EmbedExtension` via `extraExtensions` with an array of embed definitions. URLs pasted or typed on their own line will automatically convert to rich embeds.
```svelte
```
That's it — paste a YouTube URL and it becomes an embedded player.
## How detection works
1. **Paste** — When a URL is pasted as plain text, it's checked against all embed matchers. If one matches, an embed node is inserted immediately.
2. **Typed URLs** — When the user types a URL and moves to the next line (e.g. presses Enter), the link-only paragraph is detected and converted.
## Built-in YouTube embed
The `youtubeEmbed` definition matches common YouTube URL formats:
- `https://www.youtube.com/watch?v=VIDEO_ID`
- `https://youtu.be/VIDEO_ID`
- `https://www.youtube.com/embed/VIDEO_ID`
- `https://www.youtube.com/shorts/VIDEO_ID`
## Embed modal
Add `EmbedModal` as a child of the editor to let users manually insert embeds via a URL input dialog. It validates the URL against all registered embeds and shows an error if none match.
```svelte
{#if $editor}
{/if}
```
The modal is also available from the slash menu — type `/embed` to open it.
You can also control the modal directly with a bindable `open` prop:
```svelte
```
## Programmatic insertion
Use `insertEmbed` to insert an embed from a URL without the modal. Returns `null` on success or an error string.
```typescript
import { insertEmbed } from '@foxui/text';
const error = insertEmbed($editor, 'https://www.youtube.com/watch?v=dQw4w9WgXcQ');
if (error) console.error(error);
```
## Custom embeds
Create your own embed by defining a `name`, a `match` function, and a Svelte `component`:
```typescript
import type { EmbedDefinition } from '@foxui/text';
import SpotifyPlayer from './SpotifyPlayer.svelte';
const spotifyEmbed: EmbedDefinition = {
name: 'spotify',
match: (url) => {
const m = url.match(/open\.spotify\.com\/track\/(\w+)/);
return m ? { trackId: m[1] } : false;
},
component: SpotifyPlayer
};
```
Your component receives three props:
```svelte
```
Then add it alongside the built-in embeds:
```svelte
```
## Multiple embed providers
Pass as many embed definitions as you like. The first match wins — order matters.
```typescript
EmbedExtension.configure({
embeds: [youtubeEmbed, spotifyEmbed, vimeoEmbed, twitterEmbed]
})
```
## Sources
Based on "TipTap" (https://tiptap.dev/docs/editor/extensions/nodes)
## API Reference
### EmbedExtension
A tiptap Extension that auto-detects URLs in paragraphs and replaces them with rich embeds. Add via extraExtensions.
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| embeds | `EmbedDefinition[]` | `[]` | Array of embed definitions. Each definition has a name, match function, and component. |
### EmbedDefinition
Defines an embed provider with URL matching and a rendering component.
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| name | `string` | `-` | Unique name for this embed type (e.g. "youtube", "spotify"). (required) |
| match | `(url: string) => Record | false | null` | `-` | URL matcher. Return an object with embed-specific data if the URL matches, or false/null to skip. (required) |
| component | `Component` | `-` | Svelte component to render the embed. Receives props: url (string), data (object from match), selected (boolean). (required) |
### EmbedModal
Modal dialog for manually inserting an embed by URL. Add as a child of the editor. Automatically registers with the slash menu.
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| editor | `Editor` | `-` | The tiptap editor instance. (required) |
| open | `boolean` | `false` | Bindable. Controls modal visibility. |
### insertEmbed
Programmatically insert an embed for a URL. Returns null on success or an error string.
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| editor | `Editor` | `-` | The tiptap editor instance. (required) |
| url | `string` | `-` | The URL to embed. (required) |
### openEmbedModal
Opens the EmbedModal if one is mounted. Used by the slash menu and can be called from custom buttons.
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| editor | `Editor` | `-` | The tiptap editor instance. (required) |
### youtubeEmbed
Built-in YouTube embed definition. Matches youtube.com/watch, youtu.be, youtube.com/embed, and youtube.com/shorts URLs.
| Prop | Type | Default | Description |
|------|------|---------|-------------|