'How can I add rel = "nofollow" to a link in CKEditor if it's an external link
i want to give rel="nofollow"
to my external links which its content managed by ckeditor.
example.com = my site
externallink.com = any external link
For example:
<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>
This solution:
editor.dataProcessor.htmlFilter.addRules(
{
elements :
{
a : function( element )
{
if ( !element.attributes.rel )
element.attributes.rel = 'nofollow';
}
}
});
from https://stackoverflow.com/a/6930940/1848929 adds nofollow
to all a
elements.
How can i filter only external links?
Also deep doc about CKEditor Data Processor: http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Data_Processor
Note: Stackoverflow's text editor using these question's answer. Check two links' rel attribute in this question.
I'm using <script src="//cdn.ckeditor.com/4.5.10/standard/ckeditor.js"></script>
from cdn on my pages.
Solution 1:[1]
I solved it like;
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';
}
}
}
}
});
})
Solution 2:[2]
This solution also works in Internet Explorer:
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 (IE safe)
var parser = document.createElement('a');
parser.href = url;
var hostname = parser.hostname;
if ( hostname !== window.location.host) {
element.attributes.rel = 'nofollow';
element.attributes.target = '_blank';
}
}
}
}
});
})
Solution 3:[3]
So you need to compare hosts, something like this should work.
a : function( element )
{
if ( element.host !== window.location.host ) {
element.attributes.rel = 'nofollow';
}
}
Solution 4:[4]
Be shure to check hostname for empty value and for relative paths for do not add target='_blank' and rel='nofollow' for links like "tel:+70000000000" or "mailto:[email protected]"
CKEDITOR.on('instanceReady', function (ev) {
var editor = ev.editor;
editor.dataProcessor.htmlFilter.addRules({
elements: {
a: function (element) {
var url = element.attributes.href;
var parser = document.createElement('a');
parser.href = url;
var hostname = parser.hostname;
if (hostname && hostname !== "localhost" && hostname !== window.location.host) { // ???? ??????? ??????
if (!element.attributes.target)
element.attributes.target = '_blank';
if (!element.attributes.rel)
element.attributes.rel = 'nofollow';
}
}
}
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | hakkikonu |
Solution 2 | Tom-Steve Watzke |
Solution 3 | epascarello |
Solution 4 |