Link plugin for Plate rich text editor providing hyperlink functionality with URL validation, keyboard shortcuts, and UI components
npx @tessl/cli install tessl/npm-udecode--plate-link@49.0.0The Plate Link Plugin provides comprehensive hyperlink functionality for the Plate rich text editor framework. It enables users to insert, edit, and manage hyperlinks within rich text content with advanced features including URL validation, automatic link detection, keyboard shortcuts, and floating UI components.
npm install @udecode/plate-linkimport { BaseLinkPlugin } from "@udecode/plate-link";
import { LinkPlugin } from "@udecode/plate-link/react";For individual utilities and transforms:
import {
upsertLink,
validateUrl,
encodeUrlIfNeeded
} from "@udecode/plate-link";For React components and hooks:
import {
useLink,
useLinkToolbarButton,
FloatingLinkUrlInput
} from "@udecode/plate-link/react";import { createPlateEditor } from "@udecode/plate";
import { BaseLinkPlugin } from "@udecode/plate-link";
// Basic plugin setup
const editor = createPlateEditor({
plugins: [
BaseLinkPlugin.configure({
options: {
allowedSchemes: ['http', 'https', 'mailto', 'tel'],
keepSelectedTextOnPaste: true,
triggerFloatingLinkHotkeys: 'meta+k, ctrl+k'
}
})
]
});
// Insert a link programmatically
import { insertLink } from "@udecode/plate-link";
insertLink(editor, {
url: "https://example.com",
text: "Example Link"
});The Plate Link Plugin is built around several key components:
Foundation plugin providing basic link functionality, HTML serialization, and editor integration.
const BaseLinkPlugin: PlatePlugin<BaseLinkConfig>;
type BaseLinkConfig = PluginConfig<'a', {
allowedSchemes?: string[];
dangerouslySkipSanitization?: boolean;
defaultLinkAttributes?: React.AnchorHTMLAttributes<HTMLAnchorElement>;
forceSubmit?: boolean;
keepSelectedTextOnPaste?: boolean;
rangeBeforeOptions?: EditorBeforeOptions;
triggerFloatingLinkHotkeys?: string[] | string;
getLinkUrl?: (prevUrl: string | null) => Promise<string | null>;
getUrlHref?: (url: string) => string | undefined;
isUrl?: (text: string) => boolean;
transformInput?: (url: string) => string | undefined;
}>;React-enhanced plugin with floating UI, state management, and component integration.
const LinkPlugin: PlatePlugin<LinkConfig>;
type LinkConfig = ExtendConfig<BaseLinkConfig, {
isEditing: boolean;
mode: FloatingLinkMode;
mouseDown: boolean;
newTab: boolean;
openEditorId: string | null;
text: string;
updated: boolean;
url: string;
triggerFloatingLinkHotkeys?: string;
}>;
type FloatingLinkMode = '' | 'edit' | 'insert';Programmatic functions for creating, modifying, and removing links within the editor.
function insertLink(
editor: SlateEditor,
options: CreateLinkNodeOptions,
insertOptions?: InsertNodesOptions
): void;
function upsertLink(
editor: SlateEditor,
options: UpsertLinkOptions
): boolean | undefined;
function unwrapLink(
editor: SlateEditor,
options?: { split?: boolean } & UnwrapNodesOptions
): boolean;Utility functions for URL processing, validation, and encoding/decoding.
function validateUrl(editor: SlateEditor, url: string): boolean;
function encodeUrlIfNeeded(url: string): string;
function safeDecodeUrl(url: string): string;
function getLinkAttributes(
editor: SlateEditor,
link: TLinkElement
): React.AnchorHTMLAttributes<HTMLAnchorElement>;Pre-built React components and hooks for building link editing interfaces.
function useLink({ element }: { element: TLinkElement }): {
props: React.AnchorHTMLAttributes<HTMLAnchorElement>;
};
function useLinkToolbarButton(): {
props: {
pressed: boolean;
onClick: () => void;
onMouseDown: (e: React.MouseEvent) => void;
};
};
const FloatingLinkUrlInput: React.FC;
const FloatingLinkNewTabInput: React.FC;Complete floating UI system for inserting and editing links with keyboard shortcuts and positioning.
function triggerFloatingLink(
editor: SlateEditor,
options?: { focused?: boolean }
): void;
function useFloatingLinkInsert(state: LinkFloatingToolbarState): {
hidden: boolean;
props: object;
ref: RefObject;
textInputProps: object;
};
function useFloatingLinkEdit(state: LinkFloatingToolbarState): {
editButtonProps: object;
props: object;
ref: RefObject;
unlinkButtonProps: object;
};// Core types (imported from @udecode/plate)
type TLinkElement = {
type: 'a';
url: string;
target?: string;
children: TText[];
}
interface CreateLinkNodeOptions {
url: string;
children?: TText[];
target?: string;
text?: string;
}
interface UpsertLinkOptions extends CreateLinkNodeOptions {
insertNodesOptions?: InsertNodesOptions;
insertTextInLink?: boolean;
skipValidation?: boolean;
unwrapNodesOptions?: UnwrapNodesOptions;
wrapNodesOptions?: WrapNodesOptions;
}
interface LinkFloatingToolbarState {
floatingOptions?: UseVirtualFloatingOptions;
}