surface/commonmark-ext-youtube-iframe

Extension for league/commonmark to replace YouTube link with an iframe.

2.1.0 2022-12-06 13:55 UTC

This package is auto-updated.

Last update: 2024-04-06 17:02:58 UTC


README

An extension for league/commonmark version 2 built using PHP 8.0. This replaces YouTube links with the embed iframe.

The extension supports for the primary YouTube URL, with and without prefixed with the www. It also supports the short shareable URL using the youtu.be domain.

Initially based on the YouTube extension.

Installation

The project should be installed via Composer:

composer require surface/commonmark-ext-youtube-iframe

Usage

Configure your CommonMark Environment and add the extension.

use League\CommonMark\Environment\Environment;
use League\CommonMark\MarkdownConverter as Converter;
use Surface\CommonMark\Ext\YouTubeIframe\Extension as YouTubeExtension;

$options = [
    'youtube_iframe' => [
        'width' => 800,
        'height' => 600,
    ]
];

$environment = new Environment($options);
$environment->addExtension(new YouTubeExtension());

$converter = new Converter($environment);

echo $converter->convert('[](https://youtu.be/xxx)');
echo $converter->convert('[](https://www.youtube.com/watch?v=xxx)');
echo $converter->convert('[](https://youtu.be/xxx?height=480)');
echo $converter->convert('[](https://www.youtube.com/watch?v=xxx&width=640)');

Dimensions

You can control the dimensions of the videos by using the width and height configuration options.

You can also configure the dimensions using query parameters on the embed URL. You can provide the height or width or both.

?width=640
?height=480
?width=640&height=480

Full URL

You can disable the parsing of full YouTube URLs (with or without the www) by using the following option. If this option is disabled, only URLs under the ‘short’ domain will be used (youtu.be).

$options = [
    'youtube_iframe' => [
        'full_url' => false,
    ]
];

Testing

There are Unit and Integration tests for the project. These can be run using the following commands:

composer test
composer run test
composer run test-unit
composer run test-integration

There are also scripts to run code sniffer, mess detector and static analysis:

composer run sniff
composer run mess
composer run stan

Changelog

Please refer to the CHANGELOG for more information on what has changed recently.

License

This library is licensed under the MIT license. See the License File for more information.