Sommerregen / grav-plugin-external-links

This plugin adds small icons to external and mailto links, informing users the link will take them to a new site or open their email client.
Other
16 stars 14 forks source link

Feature : options to remove "noreferral" and "noopener" #33

Open aleclerc7 opened 4 years ago

aleclerc7 commented 4 years ago

Hi,

In some cases, especially in specific twig cases, it would be useful to allow the referral mechanism. While at it, I would also give the same option for noopener, even if there are few chances it be used, but it could when not using the _blank target. (Only useful in that case.) But one could desire this behaviour.

So here is a proposal to add three config options. This is fully backward compatible because they are "opt in" options. It asks the plugin to actually "do referrer" and "do opener". There is an additionnal option to even control the "do referrer" while using _blank target which would be a very voluntary risk. Usually, it is better to add "noreferrer" when using _blank target.

Here is the code for ExternalLink.php

Starting at the actual code beginning with:

...
                    // Add no-follow.
                    $nofollow = $options->get('no_follow');
                    if ($nofollow) {
                        $rel = array_filter(explode(' ', $a->getAttribute('rel')));
                        if (!in_array('nofollow', $rel)) {
                            $rel[] = 'nofollow';
                            $a->setAttribute('rel', implode(' ', $rel));
                        }
                    }

But from that point, change/update the code for the following:

                    // Set rel="noopener"
                    $doopener = $options->get('do_opener');
                    if ($doopener == false) {
                        $rel = array_filter(explode(' ', $a->getAttribute('rel')));
                        if (!in_array('noopener', $rel)) {
                            $rel[] = 'noopener';
                            $a->setAttribute('rel', implode(' ', $rel));
                        }
                    }

                    // Add no-referrer.
                    $doreferrer = $options->get('do_referrer');
                    $doreferrerevenonblanktarget = $options->get('do_referrer_even_on_blank_target');
                    if ($doreferrer == false || ($target == '_blank' and $doreferrerevenonblanktarget == false)) {
                        $rel = array_filter(explode(' ', $a->getAttribute('rel')));
                        if (!in_array('noreferrer', $rel)) {
                            $rel[] = 'noreferrer';
                            $a->setAttribute('rel', implode(' ', $rel));
                        }
                    }

                    // Add image class to <a> if it has at least one <img> child element
...

Thank you and best regards!