thephpleague / commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.
https://commonmark.thephpleague.com
BSD 3-Clause "New" or "Revised" License
2.75k stars 194 forks source link

LinkRenderer always adding `rel="noopener noreferrer"` when opening in new window #992

Closed joelclermont closed 1 year ago

joelclermont commented 1 year ago

Version(s) affected

2.4.0

Description

When an external link is set to open in a new window, the noopener and noreferrer values are being added, even though my config has disabled them.

How to reproduce

Here is my setup

        $config = [
            'external_link' => [
                'internal_hosts' => ['my-internal-domain.com'],
                'open_in_new_window' => true,
                'nofollow' => '',
                'noopener' => '',
                'noreferrer' => '',
            ],
        ];

        $converter = new GithubFlavoredMarkdownConverter($config);
        $converter->getEnvironment()->addExtension(new ExternalLinkExtension());

        $converter->getEnvironment()->addRenderer(FencedCode::class, new FencedCodeRenderer());
        $converter->getEnvironment()->addRenderer(IndentedCode::class, new IndentedCodeRenderer());

Markdown input:

This is an external link [Google](https://google.com/).

HTML output:

<a target="_blank" href="https://google.com/" rel="noopener noreferrer">Google</a>

Expected output:

<a target="_blank" href="https://goggle.com/">Google</a>
joelclermont commented 1 year ago

And if I change the config for open_in_new_window to false, the rel attribute is not rendered (as expected).

colinodell commented 1 year ago

It looks like this is happening because LinkRenderer adds a default rel attribute if none is set:

https://github.com/thephpleague/commonmark/blob/564525f54da2824c99126fd517ce87934fd80a34/src/Extension/CommonMark/Renderer/Inline/LinkRenderer.php#L56-L58

Making ExternalLinkProcessor set the rel attribute to false should prevent this. I'll get a PR together for this shortly :)

colinodell commented 1 year ago

Fixed in v2.4.1 :)

joelclermont commented 1 year ago

Beautiful!! Thank you!