tiptap/docs/api/marks/link.md
2021-10-01 23:25:07 +02:00

2.8 KiB
Raw Blame History

Link

Version Downloads

The Link extension adds support for <a> tags to the editor. The extension is headless too, there is no actual UI to add, modify or delete links. The usage example below uses the native JavaScript prompt to show you how that could work.

In a real world application, you would probably add a more sophisticated user interface.

Pasted URLs will be transformed to links automatically.

Installation

# with npm
npm install @tiptap/extension-link

# with Yarn
yarn add @tiptap/extension-link

Settings

Option Type Default Description
HTMLAttributes Object { target: '_blank', rel: 'noopener noreferrer nofollow' } Custom HTML attributes that should be added to the rendered HTML tag.
openOnClick Boolean true If enabled, links will be opened on click.
linkOnPaste Boolean true Adds a link to the current selection if the pasted content only contains an url.

Commands

Links the selected text.

editor.commands.setLink({ href: 'https://example.com' })
editor.commands.setLink({ href: 'https://example.com', target: '_blank' })

Adds or removes a link from the selected text.

editor.commands.toggleLink({ href: 'https://example.com' })
editor.commands.toggleLink({ href: 'https://example.com', target: '_blank' })

Removes a link.

editor.commands.unsetLink()

Keyboard shortcuts

:::warning Doesnt have a keyboard shortcut This extension doesnt bind a specific keyboard shortcut. You would probably open your custom UI on Mod-k though. :::

Get the current value

Did you know that you can use getAttributes to find out which attributes, for example which href, is currently set? Dont confuse it with a command (which changes the state), its just a method. Here is how that could look like:

this.editor.getAttributes('link').href

Source code

packages/extension-link/

Usage