Angular - 如何在 Quill 编辑器中将 http 或 https 附加到超链接?
Angular - How can I append http or https to hyperlink in a Quill editor?
在使用 Quill 编辑器添加 link 时,我必须包含协议,否则 link 将被视为亲戚 link。
当有人在没有指定任何协议的情况下添加 link 时,我想默认附加 https:// 所以当用户键入 google.com 时,它将创建一个 link 到 https://google.com instead of https://example.com/google.com
这个解决方案对我有用:
import Quill from 'quill';
const Link = Quill.import('formats/link');
// Override the existing property on the Quill global object and add custom protocols
Link.PROTOCOL_WHITELIST = ['http', 'https', 'mailto', 'tel', 'radar', 'rdar', 'smb', 'sms'];
class CustomLinkSanitizer extends Link {
static sanitize(url: string) {
// Run default sanitize method from Quill
const sanitizedUrl = super.sanitize(url);
// Not whitelisted URL based on protocol so, let's return `blank`
if (!sanitizedUrl || sanitizedUrl === 'about:blank') return sanitizedUrl;
// Verify if the URL already have a whitelisted protocol
const hasWhitelistedProtocol = this.PROTOCOL_WHITELIST.some(function(protocol: string) {
return sanitizedUrl.startsWith(protocol);
});
if (hasWhitelistedProtocol) return sanitizedUrl;
// if not, then append only 'https' to not to be a relative URL
return `https://${sanitizedUrl}`;
}
}
Quill.register(CustomLinkSanitizer, true);
在使用 Quill 编辑器添加 link 时,我必须包含协议,否则 link 将被视为亲戚 link。
当有人在没有指定任何协议的情况下添加 link 时,我想默认附加 https:// 所以当用户键入 google.com 时,它将创建一个 link 到 https://google.com instead of https://example.com/google.com
这个解决方案对我有用:
import Quill from 'quill';
const Link = Quill.import('formats/link');
// Override the existing property on the Quill global object and add custom protocols
Link.PROTOCOL_WHITELIST = ['http', 'https', 'mailto', 'tel', 'radar', 'rdar', 'smb', 'sms'];
class CustomLinkSanitizer extends Link {
static sanitize(url: string) {
// Run default sanitize method from Quill
const sanitizedUrl = super.sanitize(url);
// Not whitelisted URL based on protocol so, let's return `blank`
if (!sanitizedUrl || sanitizedUrl === 'about:blank') return sanitizedUrl;
// Verify if the URL already have a whitelisted protocol
const hasWhitelistedProtocol = this.PROTOCOL_WHITELIST.some(function(protocol: string) {
return sanitizedUrl.startsWith(protocol);
});
if (hasWhitelistedProtocol) return sanitizedUrl;
// if not, then append only 'https' to not to be a relative URL
return `https://${sanitizedUrl}`;
}
}
Quill.register(CustomLinkSanitizer, true);