Twig icon indicating copy to clipboard operation
Twig copied to clipboard

[markdown_to_html] option to open external links in another tab

Open tacman opened this issue 1 year ago • 4 comments

Is there a way to add an option so that external link have a target="blank" added to the a tag?

For example,
        [Cleveland Museum of Art](https://github.com/ClevelandMuseumArt/openaccess), 
        [Metropolitan Museum of Art](https://github.com/metmuseum/openaccess) and 

I know I can simply embed html instead, but this seems like a handy feature.

Extending that idea, is there a way I can intercept and render any link tag? For styling, adding an icon, etc.?

tacman avatar Sep 08 '24 14:09 tacman

The conversion of markdown to HTML is not performed by Twig itself.

The MarkdownExtension relies on an external markdown engine (see the MarkdownInterface and its different implementations for the supported engines). Some of those engines may be providing such extension points.

stof avatar Sep 08 '24 18:09 stof

If you use commonmark you can use the ExternalLinkExtension extension. https://commonmark.thephpleague.com/2.5/extensions/external-links/

You have to create your own factory:

final class YourLeagueCommonMarkConverterFactory
{
    private $extensions;

    /**
     * @param ExtensionInterface[] $extensions
     */
    public function __construct(
        #[TaggedIterator(tag: 'twig.markdown.league_extension')]
        iterable $extensions
    ) {
        $this->extensions = $extensions;
    }

    public function __invoke(): CommonMarkConverter
    {
        $config = [
            'external_link' => [
                'internal_hosts' => 'www.internal.host',
                'open_in_new_window' => true,
            ],
        ];
        $converter = new CommonMarkConverter($config);

        foreach ($this->extensions as $extension) {
            $converter->getEnvironment()->addExtension($extension);
        }

        return $converter;
    }
}

service.yaml

    League\CommonMark\Extension\ExternalLink\ExternalLinkExtension:
        tags:
            - { name: 'twig.markdown.league_extension' }
    
    twig.markdown.league_common_mark_converter_factory:
        class: App\YourLeagueCommonMarkConverterFactory

I haven't tried it, but the principle looks something like this. What I don't like about the solution is that you need your own factory to configure the CommonMark environment. This could be improved. The config argument should be a param to configure the environment.

connorhu avatar Oct 15 '24 06:10 connorhu

I found the PR that fixes the complicated setup: https://github.com/twigphp/Twig/pull/3737

connorhu avatar Oct 15 '24 08:10 connorhu

Thanks, @connorhu!

tacman avatar Oct 15 '24 09:10 tacman

#3737 has been merged now.

fabpot avatar Feb 08 '25 09:02 fabpot

Thanks! I don't see external_links as a option under commonmark, so I'm not sure how to configure it.

https://commonmark.thephpleague.com/2.3/extensions/external-links/#usage

Image

Looking at the PR: https://github.com/twigphp/Twig/commit/3cdf26fe76f18fb5ea9da8fa0e87f50ea11adcf6

Also, is there a way to pass arguments to the renderer in twig?

    {% if item.description ?? false %}
        <p>{{ item.description|default()|markdown_to_html(external_link: 'open_in_new_window')  }}</p>
    {% endif %}

tacman avatar Feb 08 '25 10:02 tacman