Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[markdown_to_html] option to open external links in another tab #4288

Open
tacman opened this issue Sep 8, 2024 · 4 comments
Open

[markdown_to_html] option to open external links in another tab #4288

tacman opened this issue Sep 8, 2024 · 4 comments
Labels

Comments

@tacman
Copy link

tacman commented Sep 8, 2024

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.?

@stof
Copy link
Member

stof commented Sep 8, 2024

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.

@connorhu
Copy link

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
Copy link

I found the PR that fixes the complicated setup: #3737

@tacman
Copy link
Author

tacman commented Oct 15, 2024

Thanks, @connorhu!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants