{"id":13828277,"url":"https://github.com/thephpleague/commonmark-ext-external-link","last_synced_at":"2025-07-09T06:31:26.445Z","repository":{"id":57013258,"uuid":"187487453","full_name":"thephpleague/commonmark-ext-external-link","owner":"thephpleague","description":"Extension for league/commonmark which adds extra classes and HTML attributes to external links","archived":true,"fork":false,"pushed_at":"2020-04-04T14:22:10.000Z","size":31,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-08-04T09:08:02.702Z","etag":null,"topics":["commonmark","commonmark-extension","external-links","markdown","php"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thephpleague.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"colinodell","patreon":"colinodell"}},"created_at":"2019-05-19T14:30:14.000Z","updated_at":"2023-10-17T20:40:17.000Z","dependencies_parsed_at":"2022-08-21T14:50:29.698Z","dependency_job_id":null,"html_url":"https://github.com/thephpleague/commonmark-ext-external-link","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thephpleague%2Fcommonmark-ext-external-link","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thephpleague%2Fcommonmark-ext-external-link/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thephpleague%2Fcommonmark-ext-external-link/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thephpleague%2Fcommonmark-ext-external-link/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thephpleague","download_url":"https://codeload.github.com/thephpleague/commonmark-ext-external-link/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225492420,"owners_count":17482869,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["commonmark","commonmark-extension","external-links","markdown","php"],"created_at":"2024-08-04T09:02:39.480Z","updated_at":"2024-11-20T08:30:22.512Z","avatar_url":"https://github.com/thephpleague.png","language":"PHP","funding_links":["https://github.com/sponsors/colinodell","https://patreon.com/colinodell"],"categories":["PHP"],"sub_categories":[],"readme":"# Extension to denote external links for `league/commonmark`\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE.md)\n[![Build Status][ico-travis]][link-travis]\n[![Coverage Status][ico-scrutinizer]][link-scrutinizer]\n[![Quality Score][ico-code-quality]][link-code-quality]\n[![Total Downloads][ico-downloads]][link-downloads]\n\n## DEPRECATED\n\n**This extension has been deprecated**.  All of its functionality now exists in [`league/commonmark`][link-league-commonmark] 1.3+ under the `League\\CommonMark\\Extension\\ExternalLink` namespace, so you should upgrade to that version and use that bundled extension instead of this one.\n\n## Overview\n\nThis extension to the [`league/commonmark` PHP Markdown parser][link-league-commonmark] can detect links to external sites and adjust the markup accordingly:\n\n - Adds a `rel=\"noopener noreferrer\"` attribute\n - Optionally adds any custom HTML classes\n\n## Install\n\nVia Composer\n\n``` bash\n$ composer require league/commonmark-ext-external-link\n```\n\n## Usage\n\nConfigure your `Environment` as usual and simply add the `ExternalLinkExtension` provided by this package:\n\n```php\nuse League\\CommonMark\\CommonMarkConverter;\nuse League\\CommonMark\\Environment;\nuse League\\CommonMark\\Ext\\ExternalLink\\ExternalLinkExtension;\n\n// Obtain a pre-configured Environment with all the CommonMark parsers/renderers ready-to-go\n$environment = Environment::createCommonMarkEnvironment();\n\n// Add this extension\n$environment-\u003eaddExtension(new ExternalLinkExtension());\n\n// Set your configuration\n$config = [\n    'external_link' =\u003e [\n        'internal_hosts' =\u003e 'www.example.com',\n        'open_in_new_window' =\u003e true,\n        'html_class' =\u003e 'external-link',\n    ],\n];\n\n// Instantiate the converter engine and start converting some Markdown!\n$converter = new CommonMarkConverter($config, $environment);\necho $converter-\u003econvertToHtml('I successfully installed the https://github.com/thephpleague/commonmark-ext-external-link extension!');\n```\n\n## Configuration\n\nThis extension supports three configuration options under the `external_link` configuration:\n\n### `internal_hosts`\n\nThis option defines a whitelist of hosts which are considered non-external and should not receive the external link treatment.\n\nThis can be a single host name, like `'example.com'`, which must match exactly.\n\nIf you need to match subdomains, use a regular expression like `'/(^|\\.)example\\.com$/'`.  Note that you must use `/` characters to delimit your regex.\n\nThis configuration option also accepts an array of multiple strings and/or regexes:\n\n```php\n$config = [\n    'external_link' =\u003e [\n        'internal_hosts' =\u003e ['foo.example.com', 'bar.example.com', '/(^|\\.)google\\.com$/],\n    ],\n];\n```\n\nBy default, if this option is not provided, all links will be considered external.\n\n### `open_in_new_window`\n\nThis option (which defaults to `false`) determines whether any external links should open in a new tab/window.\n\n### `html_class`\n\nThis option allows you to provide a `string` containing one or more HTML classes that should be added to the external link `\u003ca\u003e` tags:  No classes are added by default.\n\n## Advanced Rendering\n\nWhen an external link is detected, the `ExternalLinkProcessor` will set the `external` data option on the `Link` node to either `true` or `false`.  You can therefore create a [custom link renderer](https://commonmark.thephpleague.com/customization/inline-rendering/) which checks this value and behaves accordingly: \n\n```php\nclass MyCustomLinkRenderer implements InlineRendererInterface\n{\n\n    /**\n     * @param Link                     $inline\n     * @param ElementRendererInterface $htmlRenderer\n     *\n     * @return HtmlElement\n     */\n    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)\n    {\n        if (!($inline instanceof Link)) {\n            throw new \\InvalidArgumentException('Incompatible inline type: ' . \\get_class($inline));\n        }\n\n        if ($inline-\u003egetData('external')) {\n            // This is an external link - render it accordingly\n        } else {\n            // This is an internal link\n        }\n        \n        // ...\n    }\n}\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Security\n\nIf you discover any security related issues, please email colinodell@gmail.com instead of using the issue tracker.\n\n## Credits\n\n- [Colin O'Dell][link-author]\n- [All Contributors][link-contributors]\n\n## License\n\nThis library is licensed under the BSD-3 license.  See the [License File](LICENSE) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/league/commonmark-ext-external-link.svg?style=flat-square\n[ico-license]: http://img.shields.io/badge/License-BSD--3-brightgreen.svg?style=flat-square\n[ico-travis]: https://img.shields.io/travis/thephpleague/commonmark-ext-external-link/master.svg?style=flat-square\n[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/thephpleague/commonmark-ext-external-link.svg?style=flat-square\n[ico-code-quality]: https://img.shields.io/scrutinizer/g/thephpleague/commonmark-ext-external-link.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/league/commonmark-ext-external-link.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/league/commonmark-ext-external-link\n[link-travis]: https://travis-ci.org/thephpleague/commonmark-ext-external-link\n[link-scrutinizer]: https://scrutinizer-ci.com/g/thephpleague/commonmark-ext-external-link/code-structure\n[link-code-quality]: https://scrutinizer-ci.com/g/thephpleague/commonmark-ext-external-link\n[link-downloads]: https://packagist.org/packages/league/commonmark-ext-external-link\n[link-author]: https://github.com/colinodell\n[link-contributors]: ../../contributors\n[link-league-commonmark]: https://github.com/thephpleague/commonmark\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthephpleague%2Fcommonmark-ext-external-link","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthephpleague%2Fcommonmark-ext-external-link","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthephpleague%2Fcommonmark-ext-external-link/lists"}