# Link Extension ## Example ```svelte {#if $editor} {/if} ``` ## Documentation ## Usage `CoreRichTextEditor` includes `LinkExtension` by default. Pasted and typed URLs are automatically converted to links, and Markdown syntax `[text](url)` is supported out of the box. ```svelte ``` `FormattingBubbleMenu` automatically includes a `LinkPopover` when the link extension is detected — no extra setup needed. ```svelte ``` ## Standalone link popover `LinkPopover` is a toggle button with a popover for editing URLs. It follows the same pattern as `BoldToggle` / `ItalicToggle` — drop it into any toolbar. ```svelte ``` ## Reacting to new links Use the `onlinkadded` callback to run logic whenever a link is added — whether by typing, pasting, autolink, or the link popover. Via `CoreRichTextEditor` (uses default extensions): ```svelte { console.log(`Link added: ${text} → ${href}`); }} /> ``` Or configure it directly on the extension: ```svelte { // fetch preview, validate, transform, etc. } }) ]} /> ``` ## Programmatic usage ```ts import { setLink, removeLink } from '@foxui/text'; // Set a link on the current selection setLink(editor, 'https://example.com'); // Remove link from the current selection removeLink(editor); ``` ## How it works `LinkExtension` wraps `@tiptap/extension-link` with: - **Autolink** — URLs typed or pasted are automatically detected and linked - **Markdown syntax** — `[text](url "title")` input and paste rules, with `title` attribute support - **Configurable** — toggle autolink, markdown, open-on-click behavior, default protocol `LinkPopover` tracks the current selection via editor transactions. When the selection is inside a link, the toggle is pressed and clicking it opens the popover pre-filled with the current URL. ## Sources Based on "TipTap" (https://tiptap.dev/docs/editor/extensions/marks/link) ## API Reference ### LinkExtension A tiptap Extension that bundles link detection, autolink, and optional Markdown syntax. Add via extraExtensions. | Prop | Type | Default | Description | |------|------|---------|-------------| | openOnClick | `boolean | 'whenNotEditable'` | `'whenNotEditable'` | Whether clicking a link opens it. | | autolink | `boolean` | `true` | Automatically detect and linkify URLs as you type. | | defaultProtocol | `string` | `'https'` | Protocol added to URLs without one. | | markdown | `boolean` | `true` | Enable Markdown link syntax [text](url "title"). | | HTMLAttributes | `Record` | `{}` | HTML attributes applied to link elements. | | onlinkadded | `(event: LinkAddedEvent) => void` | `-` | Called whenever a link is added to the editor (via typing, pasting, autolink, or UI). Receives { href, text, editor }. | ### LinkPopover A toggle button with a popover for setting, editing, or removing links on the current selection. Works standalone, in a bubble menu, or in any toolbar. | Prop | Type | Default | Description | |------|------|---------|-------------| | editor | `Readable` | `-` | The editor store. (required) | | class | `string` | `-` | CSS classes for the popover content. | ### setLink Helper function to set a link on the current selection. Unsets the link if URL is empty. | Prop | Type | Default | Description | |------|------|---------|-------------| | editor | `Editor` | `-` | The tiptap editor instance. (required) | | url | `string` | `-` | The URL to set. (required) | ### removeLink Helper function to remove the link from the current selection. | Prop | Type | Default | Description | |------|------|---------|-------------| | editor | `Editor` | `-` | The tiptap editor instance. (required) |