CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tiptap--extension-link

Link extension for tiptap rich text editor providing automatic link detection, paste handling, click behavior, and XSS protection.

88

1.23x
Overview
Eval results
Files

link-commands.mddocs/

Link Commands

Editor commands for programmatic link manipulation with chainable API integration. These commands extend the Tiptap Commands interface to provide link-specific functionality.

Capabilities

Set Link Command

Sets a link mark on the current selection with specified attributes.

/**
 * Set a link mark on the current selection
 * @param attributes - Link attributes including href and optional target, rel, class
 * @returns Chainable command result
 */
setLink: (attributes: {
  href: string;
  target?: string | null;
  rel?: string | null;
  class?: string | null;
}) => ReturnType;

Usage Examples:

import { Editor } from "@tiptap/core";
import { Link } from "@tiptap/extension-link";

const editor = new Editor({
  extensions: [Link],
});

// Basic link setting
editor.commands.setLink({ href: 'https://tiptap.dev' });

// Link with custom attributes
editor.commands.setLink({
  href: 'https://example.com',
  target: '_blank',
  rel: 'noopener noreferrer',
  class: 'external-link',
});

// Chainable with other commands
editor
  .chain()
  .focus()
  .selectAll()
  .setLink({ href: 'https://tiptap.dev' })
  .run();

Toggle Link Command

Toggles a link mark on the current selection. If a link exists, it removes it; if no link exists, it creates one with the provided attributes.

/**
 * Toggle a link mark on the current selection
 * @param attributes - Optional link attributes. If not provided and link exists, removes the link
 * @returns Chainable command result
 */
toggleLink: (attributes?: {
  href: string;
  target?: string | null;
  rel?: string | null;
  class?: string | null;
}) => ReturnType;

Usage Examples:

// Toggle link with URL
editor.commands.toggleLink({ href: 'https://tiptap.dev' });

// Toggle with custom attributes
editor.commands.toggleLink({
  href: 'https://example.com',
  target: '_blank',
  class: 'toggled-link',
});

// Toggle without attributes (removes existing link)
editor.commands.toggleLink();

// Chainable toggle
editor
  .chain()
  .focus()
  .toggleLink({ href: 'https://tiptap.dev' })
  .run();

Unset Link Command

Removes the link mark from the current selection, converting linked text back to plain text.

/**
 * Remove link mark from current selection
 * @returns Chainable command result
 */
unsetLink: () => ReturnType;

Usage Examples:

// Remove link from selection
editor.commands.unsetLink();

// Chainable unset
editor
  .chain()
  .focus()
  .selectAll()
  .unsetLink()
  .run();

// Conditional link removal
if (editor.isActive('link')) {
  editor.commands.unsetLink();
}

Commands Interface Extension

The Link extension extends the Tiptap Commands interface with link-specific methods.

declare module '@tiptap/core' {
  interface Commands<ReturnType> {
    link: {
      /**
       * Set a link mark
       * @param attributes The link attributes
       */
      setLink: (attributes: {
        href: string;
        target?: string | null;
        rel?: string | null;
        class?: string | null;
      }) => ReturnType;

      /**
       * Toggle a link mark
       * @param attributes The link attributes
       */
      toggleLink: (attributes?: {
        href: string;
        target?: string | null;
        rel?: string | null;
        class?: string | null;
      }) => ReturnType;

      /**
       * Unset a link mark
       */
      unsetLink: () => ReturnType;
    };
  }
}

Command Validation

All link commands include built-in URL validation to prevent XSS attacks and ensure only allowed URIs are processed.

Validation Behavior:

// Commands automatically validate URLs using the configured isAllowedUri function
editor.commands.setLink({ href: 'javascript:alert("xss")' }); // Returns false, link not set

editor.commands.setLink({ href: 'https://safe-url.com' }); // Returns true, link set

// Custom validation context is used
const isValid = editor.extensionManager.extensions
  .find(ext => ext.name === 'link')
  .options.isAllowedUri('https://example.com', {
    defaultValidate: (url) => /* default validation */,
    protocols: ['http', 'https', 'ftp'],
    defaultProtocol: 'https'
  });

Command Chaining

All link commands support Tiptap's chainable command API for complex operations.

Usage Examples:

// Complex command chain
editor
  .chain()
  .focus()
  .selectAll()
  .unsetLink() // Remove any existing links
  .setTextSelection({ from: 10, to: 20 })
  .setLink({ href: 'https://tiptap.dev' })
  .setTextSelection({ from: 30, to: 40 })
  .toggleLink({ href: 'https://example.com' })
  .run();

// Conditional chaining
const chain = editor.chain().focus();

if (shouldAddLink) {
  chain.setLink({ href: 'https://tiptap.dev' });
} else {
  chain.unsetLink();
}

chain.run();

Command State Checking

Use Tiptap's state checking methods to determine link status before executing commands.

Usage Examples:

// Check if selection has a link
const hasLink = editor.isActive('link');

// Get current link attributes
const linkAttrs = editor.getAttributes('link');
console.log(linkAttrs.href); // Current link URL

// Conditional command execution
if (editor.isActive('link')) {
  editor.commands.unsetLink();
} else {
  editor.commands.setLink({ href: 'https://tiptap.dev' });
}

Types

/** Link command attribute interface */
interface LinkCommandAttributes {
  /** Link URL (required) */
  href: string;
  /** Link target attribute */
  target?: string | null;
  /** Link relationship attribute */
  rel?: string | null;
  /** CSS class attribute */
  class?: string | null;
}

Install with Tessl CLI

npx tessl i tessl/npm-tiptap--extension-link

docs

autolink.md

click-handling.md

index.md

link-commands.md

link-configuration.md

paste-processing.md

url-validation.md

tile.json