Link
Create, edit, and remove hyperlinks.
Import
typescript
import { linkPlugin } from 'halka/plugins/link';Usage
- Select text and call link.toggle to wrap in <a>
- link.toggle with empty href removes the link
- Use link.active state to populate an edit-link dialog
Commands
| Command | Description |
|---|---|
link.toggle | Create, update, or remove link (empty href removes) |
link.update | Update link attributes at cursor |
link.unlink | Remove link, keep text |
State
Read plugin state with editor.getState(name). Listen to formatChange to refresh UI when the selection moves.
| State | Description |
|---|---|
link.active | Active link attributes or null |
typescript
const link = editor.getState('link.active') as {
href: string;
target: string;
rel: string;
} | null;
// null when the caret is not inside a link
if (link) {
console.log(link.href);
}
// Refresh when selection changes (e.g. toolbar)
editor.on('formatChange', () => {
const active = editor.getState('link.active');
});Example
typescript
editor.execCommand('link.toggle', {
href: 'https://example.com',
target: '_blank'
});