Link
Add a URL to a selection or insert a link at the caret.
Try it
Select “the formatting guide”, click Add link, and use https://example.com. The output includes <a href="https://example.com">the formatting guide</a>. Click inside the link or select its text, then click Remove link.
Live example
Example result
Add it to your editor
Enable Link and call editor.execute('setLink', { href }). This example opens a prompt while preserving the selection and adds a separate remove button.
import { EditorContent, EditorProvider, Link, useEditor, useIsActive } from 'yurich';
export function LinkEditor() {
const editor = useEditor({
extensions: [Link],
initialValue: "<p>Read the formatting guide before clicking every button.</p>",
});
const active = useIsActive(editor, 'link');
function addLink() {
const href = window.prompt("Link URL", 'https://example.com');
if (href) editor.execute('setLink', { href });
}
return (
<EditorProvider editor={editor}>
<div role="group" aria-label="Link">
<button
type="button"
onMouseDown={event => event.preventDefault()}
onClick={addLink}
>
Add link
</button>
<button
type="button"
disabled={!active}
onMouseDown={event => event.preventDefault()}
onClick={() => editor.execute('removeLink')}
>
Remove link
</button>
</div>
<EditorContent ariaLabel="Editor" />
</EditorProvider>
);
}
Behavior to account for
The supported URL prefixes are http://, https://, and mailto:. At a caret, adding a link inserts its URL as the visible text. removeLink can remove a whole link at the caret.