我想给rel="nofollow"
到我的外部链接,其内容由 ckeditor 管理。
example.com = 我的网站
externallink.com = 任何外部链接
例如:
<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';
}
}
});
from https://stackoverflow.com/a/6930940/1848929 https://stackoverflow.com/a/6930940/1848929 adds nofollow
to all a
元素。
如何仅过滤外部链接?
还有关于 CKEditor 数据处理器的深度文档:http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Data_Processor http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Data_Processor
注意:Stackoverflow 的文本编辑器使用这些问题的答案。检查此问题中两个链接的 rel 属性。
我在用着<script src="//cdn.ckeditor.com/4.5.10/standard/ckeditor.js"></script>
来自我的页面上的 CDN。
我解决了它像;
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';
}
}
}
}
});
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)