2021-10-14 06:13:50 +08:00
---
description: Link it, link it good, link it real good (and don’ t forget the href).
2021-10-15 03:20:21 +08:00
icon: link
2021-10-14 06:13:50 +08:00
---
2020-08-19 03:39:41 +08:00
# Link
2021-01-25 17:35:52 +08:00
[![Version ](https://img.shields.io/npm/v/@tiptap/extension-link.svg?label=version )](https://www.npmjs.com/package/@tiptap/extension-link)
[![Downloads ](https://img.shields.io/npm/dm/@tiptap/extension-link.svg )](https://npmcharts.com/compare/@tiptap/extension-link?minimal=true)
2020-11-13 18:04:31 +08:00
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.
2020-09-26 01:05:21 +08:00
2020-12-01 19:03:50 +08:00
In a real world application, you would probably add a more sophisticated user interface.
2020-09-26 01:05:21 +08:00
2020-12-01 19:03:50 +08:00
Pasted URLs will be transformed to links automatically.
2020-08-19 03:39:41 +08:00
2020-09-23 15:52:04 +08:00
## Installation
```bash
npm install @tiptap/extension -link
```
2020-09-23 18:03:03 +08:00
## Settings
2021-10-02 07:20:09 +08:00
### HTMLAttributes
Custom HTML attributes that should be added to the rendered HTML tag.
```js
Link.configure({
HTMLAttributes: {
class: 'my-custom-class',
},
})
```
### openOnClick
If enabled, links will be opened on click.
Default: `true`
```js
Link.configure({
openOnClick: false,
})
```
### linkOnPaste
Adds a link to the current selection if the pasted content only contains an url.
Default: `true`
```js
Link.configure({
linkOnPaste: false,
})
```
2020-09-23 18:39:09 +08:00
## Commands
2021-10-02 05:25:07 +08:00
### setLink()
Links the selected text.
```js
editor.commands.setLink({ href: 'https://example.com' })
editor.commands.setLink({ href: 'https://example.com', target: '_blank' })
```
### toggleLink()
Adds or removes a link from the selected text.
```js
editor.commands.toggleLink({ href: 'https://example.com' })
editor.commands.toggleLink({ href: 'https://example.com', target: '_blank' })
```
### unsetLink()
Removes a link.
```js
editor.commands.unsetLink()
```
2020-09-23 18:39:09 +08:00
## Keyboard shortcuts
2020-09-26 01:05:21 +08:00
:::warning Doesn’ t have a keyboard shortcut
2020-11-03 23:43:35 +08:00
This extension doesn’ t bind a specific keyboard shortcut. You would probably open your custom UI on `Mod-k` though.
2020-09-26 01:05:21 +08:00
:::
2020-09-23 18:39:09 +08:00
2021-09-17 02:34:21 +08:00
## Get the current value
2021-10-02 02:51:53 +08:00
Did you know that you can use [`getAttributes` ](/api/editor#get-attributes ) to find out which attributes, for example which href, is currently set? Don’ t confuse it with a [command ](/api/commands ) (which changes the state), it’ s just a method. Here is how that could look like:
2021-09-17 02:34:21 +08:00
```js
this.editor.getAttributes('link').href
```
2020-09-23 18:39:09 +08:00
## Source code
2021-04-21 21:31:11 +08:00
[packages/extension-link/ ](https://github.com/ueberdosis/tiptap/blob/main/packages/extension-link/ )
2020-09-23 18:39:09 +08:00
## Usage
2021-10-19 00:01:47 +08:00
https://embed.tiptap.dev/preview/Marks/Link