Giter Club home page Giter Club logo

commonmark-ext-external-link's Introduction

Extension to denote external links for league/commonmark

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

DEPRECATED

This extension has been deprecated. All of its functionality now exists in 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.

Overview

This extension to the league/commonmark PHP Markdown parser can detect links to external sites and adjust the markup accordingly:

  • Adds a rel="noopener noreferrer" attribute
  • Optionally adds any custom HTML classes

Install

Via Composer

$ composer require league/commonmark-ext-external-link

Usage

Configure your Environment as usual and simply add the ExternalLinkExtension provided by this package:

use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment;
use League\CommonMark\Ext\ExternalLink\ExternalLinkExtension;

// Obtain a pre-configured Environment with all the CommonMark parsers/renderers ready-to-go
$environment = Environment::createCommonMarkEnvironment();

// Add this extension
$environment->addExtension(new ExternalLinkExtension());

// Set your configuration
$config = [
    'external_link' => [
        'internal_hosts' => 'www.example.com',
        'open_in_new_window' => true,
        'html_class' => 'external-link',
    ],
];

// Instantiate the converter engine and start converting some Markdown!
$converter = new CommonMarkConverter($config, $environment);
echo $converter->convertToHtml('I successfully installed the https://github.com/thephpleague/commonmark-ext-external-link extension!');

Configuration

This extension supports three configuration options under the external_link configuration:

internal_hosts

This option defines a whitelist of hosts which are considered non-external and should not receive the external link treatment.

This can be a single host name, like 'example.com', which must match exactly.

If you need to match subdomains, use a regular expression like '/(^|\.)example\.com$/'. Note that you must use / characters to delimit your regex.

This configuration option also accepts an array of multiple strings and/or regexes:

$config = [
    'external_link' => [
        'internal_hosts' => ['foo.example.com', 'bar.example.com', '/(^|\.)google\.com$/],
    ],
];

By default, if this option is not provided, all links will be considered external.

open_in_new_window

This option (which defaults to false) determines whether any external links should open in a new tab/window.

html_class

This option allows you to provide a string containing one or more HTML classes that should be added to the external link <a> tags: No classes are added by default.

Advanced Rendering

When 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 which checks this value and behaves accordingly:

class MyCustomLinkRenderer implements InlineRendererInterface
{

    /**
     * @param Link                     $inline
     * @param ElementRendererInterface $htmlRenderer
     *
     * @return HtmlElement
     */
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
    {
        if (!($inline instanceof Link)) {
            throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
        }

        if ($inline->getData('external')) {
            // This is an external link - render it accordingly
        } else {
            // This is an internal link
        }
        
        // ...
    }
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

This library is licensed under the BSD-3 license. See the License File for more information.

commonmark-ext-external-link's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.