如何添加rel =“nofollow”到CKEditor中的链接(如果它是外部链接) [英] How can I add rel = "nofollow" to a link in CKEditor if it's an external link
问题描述
我想给我的外部链接,其内容由ckeditor管理 rel =nofollow
。
i want to give rel="nofollow"
to my external links which its content managed by ckeditor.
example.com = my site
externallink.com =任何外部链接
externallink.com = any external link
例如:
<p>
Lorem <a href="https://example.com/an-article.html">ipsum</a> dolar
<a href="http://externallink.com/example.html" rel="nofollow">sit</a> amet.
</p>
此解决方案:
editor.dataProcessor.htmlFilter.addRules(
{
elements :
{
a : function( element )
{
if ( !element.attributes.rel )
element.attributes.rel = 'nofollow';
}
}
});
来自 http://stackoverflow.com/a/6930940/1848929 将 nofollow
添加到所有 a
元素。
from http://stackoverflow.com/a/6930940/1848929 adds nofollow
to all a
elements.
如何只过滤外部链接?
还有关于CKEditor数据处理器的深入文档: http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Data_Processor
Also deep doc about CKEditor Data Processor: http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Data_Processor
注意:Stackoverflow的文本编辑器使用这些问题的答案。在此问题中检查两个链接的rel属性。
Note: Stackoverflow's text editor using these question's answer. Check two links' rel attribute in this question.
我使用< script src =// cdn.ckeditor.com/4.5.10/standard/ckeditor.js\"> ;</script>
从我的网页上的cdn。
I'm using <script src="//cdn.ckeditor.com/4.5.10/standard/ckeditor.js"></script>
from cdn on my pages.
推荐答案
我解决了这样的问题:
CKEDITOR.on('instanceReady', function(ev) {
var editor = ev.editor;
editor.dataProcessor.htmlFilter.addRules({
elements : {
a : function( element ) {
if ( !element.attributes.rel ){
//gets content's a href values
var url = element.attributes.href;
//extract host names from URLs
var hostname = (new URL(url)).hostname;
if ( hostname !== window.location.host && hostname !=="youranothersite.com") {
element.attributes.rel = 'nofollow';
}
}
}
}
});
})
这篇关于如何添加rel =“nofollow”到CKEditor中的链接(如果它是外部链接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!