Contribute to this guide

指南链接

链接功能允许您在内容中插入超链接,并提供 UI 来创建和编辑它们。由于 自动链接 插件,您键入或粘贴的 URL 和电子邮件地址会自动转换为有效的链接。

# 演示

使用链接工具栏按钮 链接 或按 Ctrl/Cmd+K 创建新链接。单击链接会打开上下文工具栏。工具栏允许您通过单击来编辑现有链接 编辑链接 或取消链接它们 取消链接

此演示展示了一组有限的功能。请访问 功能丰富的编辑器示例 以查看更多实际应用。

CKEditor 5 允许在链接的内部和外部边界处进行输入,使编辑对用户来说更容易。

要在链接内输入,将光标移动到其(开始或结束)边界。只要链接保持突出显示(默认情况下:蓝色),输入和应用格式将在其边界内进行

The animation shows typing inside the link in CKEditor 5 rich text editor.

要在链接之前或之后输入,将光标移动到其边界,然后按一次箭头键 () 离开链接。链接不再突出显示,并且您键入的任何文本或应用的任何格式都不会在链接内

The animation shows typing before the link in CKEditor 5 rich text editor.

# 安装

⚠️ 新的导入路径

42.0.0 版本 开始,我们更改了导入路径的格式。本指南使用新的、更短的格式。如果您使用的是旧版本的 CKEditor 5,请参阅 旧版设置中的包 指南。

安装编辑器 后,将功能添加到您的插件列表和工具栏配置中

import { ClassicEditor, AutoLink, Link } from 'ckeditor5';

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        plugins: [ Link, AutoLink, /* ... */ ],
        toolbar: [ 'link', /* ... */ ],
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

默认情况下,在编辑器中创建的所有链接都在 编辑器数据 中的 编辑器数据 中具有 href="..." 属性。如果您希望您的链接具有其他链接属性,链接装饰器 提供了一种简单的方法来配置和管理它们。

您可以使用两种类型的链接装饰器

  • 自动 – 它们将链接与预定义规则进行匹配,并根据结果管理其属性。
  • 手动 – 它们允许用户使用编辑器 UI 单独控制链接属性。

链接装饰器默认情况下处于关闭状态,需要正确的 配置 才能在您的富文本编辑器中启用它们。

# 演示

在下面的编辑器中,所有外部链接都获得 target="_blank"rel="noopener noreferrer" 属性 (自动装饰器)。单击一个链接并对其进行编辑 编辑链接 以查看您可以使用编辑气泡中的切换按钮来控制特定链接的 download 属性 (手动装饰器)。查看下面的编辑器数据(实时更新)以查看额外的链接属性。

以下代码运行此编辑器。了解更多关于该功能的配置

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        toolbar: {
            items: [
                'link',
                // More toolbar items.
                // ...
            ],
        },
        link: {
            // Automatically add target="_blank" and rel="noopener noreferrer" to all external links.
            addTargetToExternalLinks: true,

            // Let the users control the "download" attribute of each link.
            decorators: [
                {
                    mode: 'manual',
                    label: 'Downloadable',
                    attributes: {
                        download: 'download'
                    }
                }
            ]
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

# 配置

您可以通过在 config.link.decorators 配置选项中提供的定义来配置装饰器。

每个装饰器定义必须具有唯一的名称。对于手动装饰器,名称也代表文档模型中的装饰器。

链接装饰器相互独立工作,并且不存在任何冲突解决机制。例如,同时使用自动和手动装饰器配置target属性可能会导致奇怪的结果。如果您为同一属性定义了更多手动或自动装饰器,也是如此。

(自动)链接装饰器的常见用例是向文档中的所有外部链接添加target="_blank"rel="noopener noreferrer" 属性。专门的config.link.addTargetToExternalLinks 配置适用于此目的。当您将此选项设置为true 时,所有以http://https://// 开头的链接都将使用targetrel 属性“装饰”。

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        link: {
            addTargetToExternalLinks: true
        }
        // More of the editor's configuration.
            // ...
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

在内部,此配置对应于一个自动装饰器,其具有以下定义

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        link: {
            decorators: {
                addTargetToExternalLinks: {
                    mode: 'automatic',
                    callback: url => /^(https?:)?\/\//.test( url ),
                    attributes: {
                        target: '_blank',
                        rel: 'noopener noreferrer'
                    }
                }
            }
        }
        // More of the editor's configuration.
            // ...
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

如果您希望将是否在新标签页中打开链接的决定留给用户,请不要使用config.link.addTargetToExternalLinks 配置。而是定义一个新的手动装饰器,其具有以下定义

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        link: {
            decorators: {
                openInNewTab: {
                    mode: 'manual',
                    label: 'Open in a new tab',
                    attributes: {
                        target: '_blank',
                        rel: 'noopener noreferrer'
                    }
                }
            }
        }
        // More of the editor's configuration.
            // ...
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

当用户忘记键入到外部源或网站的完整 URL 地址时,默认链接协议可能很有用。有时复制文本(例如ckeditor.com)并将其转换为链接可能会导致一些问题。因此,创建的链接会将您定向到yourdomain.com/ckeditor.com,因为缺少协议。这使得链接相对于其出现的位置的站点是相对的。

在您启用config.link.defaultProtocol 配置选项后,链接功能将能够为您处理此问题。默认情况下,它不会修复传递的链接值,但是当您将config.link.defaultProtocol 设置为(例如)http:// 时,插件将向可能需要它的每个链接(如ckeditor.comexample.com 等,其中缺少[protocol://]example.com)添加给定的协议。

查看基本的配置示例

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        link: {
            defaultProtocol: 'http://'
        }
        // More of the editor's configuration.
            // ...
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

启用config.link.defaultProtocol 选项后,您仍然可以使用#/ 在本地链接内容。协议不会添加到这些链接中。

启用后,此功能还提供电子邮件地址自动检测功能。当您在内容中提交hello@example.com 时,插件会自动将其更改为mailto:hello@example.com

默认情况下,允许在链接中使用最少的协议集。任何带有无法识别的协议的 URL 将被更改为“#”。您可以使用config.link.allowedProtocols 覆盖协议列表。

查看配置示例

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        link: {
            // You can use `s?` suffix like below to allow both `http` and `https` protocols at the same time.
            allowedProtocols: [ 'https?', 'tel', 'sms', 'sftp', 'smb', 'slack' ]
        }
        // More of the editor's configuration.
        // ...
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

请记住,您需要自行承担自定义此列表的风险 - 添加不安全的协议(如javascript)可能会导致严重的安全性漏洞!

自动链接装饰器将编辑器内容中的所有链接与一个函数进行匹配,该函数决定链接是否应该接收某些属性集,同时考虑链接的 URL(href)。这些装饰器会静默地工作,编辑器会在数据向下转换期间应用它们。

例如,要创建一个自动装饰器,该装饰器将download="file.pdf" 属性添加到所有以".pdf" 扩展名结尾的链接,您应该将以下定义添加到config.link.decorators

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        link: {
            decorators: {
                detectDownloadable: {
                    mode: 'automatic',
                    callback: url => url.endsWith( '.pdf' ),
                    attributes: {
                        download: 'file.pdf'
                    }
                }
            }
        }
        // More of the editor's configuration.
        // ...
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

如果您想将targetrel 属性添加到内容中的所有外部链接,我们为此目的准备了一个专用配置。因此,您无需自己定义自动装饰器。

手动链接装饰器在链接编辑气球中以切换按钮的形式表示。用户可以使用它们来控制特定链接属性的存在(查看演示了解更多信息)。每个手动装饰器定义都有一个易于理解的标签,显示在链接编辑气球中切换按钮旁边。确保它简洁明了,方便用户使用。

要在链接编辑气球中配置一个“可下载”切换按钮,该按钮在打开时将download="file" 属性添加到链接,请将以下定义添加到config.link.decorators

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        link: {
            decorators: {
                toggleDownloadable: {
                    mode: 'manual',
                    label: 'Downloadable',
                    attributes: {
                        download: 'file'
                    }
                },
                openInNewTab: {
                    mode: 'manual',
                    label: 'Open in a new tab',
                    defaultValue: true,			// This option will be selected by default.
                    attributes: {
                        target: '_blank',
                        rel: 'noopener noreferrer'
                    }
                }
            }
        }
        // More of the editor's configuration.
        // ...
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

The AutoLink 功能会自动将 URL 或电子邮件地址转换为工作链接。

要使用自动链接功能,请在链接后按空格键回车键Shift+回车键

您可以随时使用撤消功能(Ctrl/Cmd+Z)恢复自动链接。

# 通用 API

The Link 插件注册 UI 按钮组件('link')和以下命令

您可以使用editor.execute() 方法执行命令

// Applies the link to the selected content.
// When the selection is collapsed, it creates new text wrapped in a link.
editor.execute( 'link', 'http://example.com' );

// If there are decorators configured, the command can set their state.
editor.execute( 'link', 'http://example.com', { linkIsExternal: true } );

// Removes the link from the selection (and all decorators if present).
editor.execute( 'unlink' );

该包为链接图像 提供了一个插件。请参阅链接图像 指南,该指南位于图像部分中。

链接在模型 中使用linkHref 属性表示。手动链接装饰器 在模型中使用对应于其名称的文本属性表示,如在config.link.decorators 中配置的那样。

我们建议您在开发和调试过程中使用官方的CKEditor 5 检查器。它将为您提供有关编辑器状态的大量有用信息,例如内部数据结构、选择、命令以及更多信息。

# 贡献

该功能的源代码在 GitHub 上可用,地址为https://github.com/ckeditor/ckeditor5/tree/master/packages/ckeditor5-link