TinyMCE 7.x版 Aタグにrel=“nofollow”を自動で付けて悪用を防ぐ

tinymce

ユーザーにビジュアルエディタの使用を許可した場合、いくらでもリンクが貼り放題だと、SEOの観点から、悪用されるため、それを防ぐために外部リンクには rel="nofollow" が自動付くようにカスタマイズ。

 

リンクプラグインの読み込み&ツールバーに設置

plugins: 'link',
toolbar: 'link',

 

link_default_target: '_blank',
link_assume_external_targets: false,

リンクプラグインのデフォルトのターゲットを_blank、もしくは、’’ にする。

外部リンクの自動判定を無効化にする。

 

リンクコールバック関数

link_callback: function(data) {
    if (data.href && (data.href.startsWith('https://') || data.href.startsWith('http://')) && !data.href.startsWith(window.location.origin)) {
        data.rel = 'nofollow noopener';
        data.target = '_blank';
    }
    return data;
},

 

httpまたはhttpsで始まる外部リンクにのみnofollowを追加し、target="_blank"を設定

 

コンテンツフィルター

setup: function(editor) {
    editor.on('PreProcess', function(e) {
        var content = e.node;
        tinymce.each(content.getElementsByTagName('a'), function(a) {
            var href = a.getAttribute('href');
            if (href && (href.startsWith('https://') || href.startsWith('http://')) && !href.startsWith(window.location.origin)) {
            a.setAttribute('rel', 'nofollow noopener');
            a.setAttribute('target', '_blank');
            }
        });
    });
},