htmlpurifier nofollow 仅适用于生成的链接

htmlpurifier nofollow only working for generated links

我正在使用 HTML 净化器库来设置我网站的评论格式。

不过,我注意到了一些事情。在我的设置中,我将 HTML.nofollow 设置为 true。

'HTML.Nofollow' => true,

但是我写锚点的时候link,nofollow不会加。但是有了生成的 link,它就可以正常工作了。

示例输入:

<a href="www.google.com">google</a>

https://www.yahoo.com

输出:

<p><a href="www.google.com">google</a></p>

<p><a href="https://www.yahoo.com" rel="nofollow">https://www.yahoo.com</a></p>

这是我的设置:

return [
    'encoding'      => 'UTF-8',
    'finalize'      => true,
    'cachePath'     => storage_path('app/purifier'),
    'cacheFileMode' => 0755,
    'settings'      => [
        'default' => [
            'HTML.Doctype'             => 'HTML 4.01 Transitional',
            'HTML.Allowed'             => 'b,i,u,ul,ol,li,p,blockquote,table,tr,th,td,a[href|title],sup,sub,span,code',
            'AutoFormat.AutoParagraph' => true,
            'AutoFormat.RemoveEmpty'   => true,
            'AutoFormat.Linkify' => true,
            'HTML.Nofollow' => true,
        ],

    ],

];

作为允许的 HTML 元素之一,我会很好地删除,但出于某种原因,这意味着生成的 URL 不再有效。

<a href="www.google.com">google</a>,因为缺少方案(http://https://ftp://等),实际上是 link 到 http://example.com/www.google.com 之类的东西(取决于您是否在您网站的文件夹中)。由于 nofollow 通常用于防止 other 网站窃取 link juice,因此 link 这样的亲属可能被豁免。

用这样的东西试试:

<a href="https://www.google.com">google</a>

(作为奖励,link 实际上会以这种方式工作。)