Skip to content

Commit

Permalink
feat: support configuration options & 'full url' option
Browse files Browse the repository at this point in the history
  • Loading branch information
trovster committed Dec 6, 2022
1 parent 59c6ac1 commit c3a607d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ use League\CommonMark\MarkdownConverter as Converter;
use Surface\CommonMark\Ext\YouTubeIframe\Extension as YouTubeExtension;

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

$environment = new Environment($options);
Expand All @@ -43,8 +45,8 @@ echo $converter->convert('[](https://www.youtube.com/watch?v=xxx&width=640)');

### Dimensions

You can control the dimensions of the videos by using the `youtube_width` and
`youtube_height` configuration options.
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*.
Expand All @@ -55,6 +57,20 @@ You can provide the `height` or `width` or *both*.
?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`).

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

## Testing

There are Unit and Integration tests for the project. These can be run using
Expand Down
61 changes: 47 additions & 14 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,91 @@

use League\CommonMark\Environment\EnvironmentBuilderInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\ConfigurableExtensionInterface;
use League\CommonMark\Extension\ExtensionInterface;
use League\Config\ConfigurationBuilderInterface;
use Nette\Schema\Expect;
use Surface\CommonMark\Ext\YouTubeIframe\Iframe;
use Surface\CommonMark\Ext\YouTubeIframe\Processor;
use Surface\CommonMark\Ext\YouTubeIframe\Renderer;
use Surface\CommonMark\Ext\YouTubeIframe\Url\Parsers\Full;
use Surface\CommonMark\Ext\YouTubeIframe\Url\Parsers\Short;
use Surface\CommonMark\Ext\YouTubeIframe\Url\Parsers\WithoutWww;

final class Extension implements ExtensionInterface
final class Extension implements ExtensionInterface, ConfigurableExtensionInterface
{
public function register(EnvironmentBuilderInterface $environment): void
{
$width = $this->getWidth($environment);
$height = $this->getHeight($environment);
$fullScreen = $this->allowFullscreen($environment);
$processors = $this->getProcessors($environment, $width, $height);

$environment
->addEventListener(DocumentParsedEvent::class, new Processor([
new Short($width, $height),
new Full($width, $height),
new WithoutWww($width, $height),
]))
->addEventListener(DocumentParsedEvent::class, new Processor($processors))
->addRenderer(Iframe::class, new Renderer(
$width,
$height,
$fullScreen
))
;
));
}

public function configureSchema(ConfigurationBuilderInterface $builder): void
{
$builder->addSchema('youtube_iframe', Expect::structure([
'width' => Expect::int(800),
'height' => Expect::int(600),
'allowfullscreen' => Expect::bool(true),
'full_url' => Expect::bool(true),
]));
}

protected function getProcessors(EnvironmentBuilderInterface $environment, int $width, int $height): array
{
if (! $this->supportFullUrl($environment)) {
return [
new Short($width, $height),
];
}

return [
new Short($width, $height),
new Full($width, $height),
new WithoutWww($width, $height),
];
}

protected function supportFullUrl(EnvironmentBuilderInterface $environment): bool
{
if ($environment->getConfiguration()->exists('youtube_iframe.full_url')) {
return (bool) $environment->getConfiguration()->get('youtube_iframe.full_url');
}

return true;
}

protected function getWidth(EnvironmentBuilderInterface $environment): int
{
if ($environment->getConfiguration()->exists('youtube_width')) {
return (int) $environment->getConfiguration()->get('youtube_width');
if ($environment->getConfiguration()->exists('youtube_iframe.width')) {
return (int) $environment->getConfiguration()->get('youtube_iframe.width');
}

return 800;
}

protected function getHeight(EnvironmentBuilderInterface $environment): int
{
if ($environment->getConfiguration()->exists('youtube_height')) {
return (int) $environment->getConfiguration()->get('youtube_height');
if ($environment->getConfiguration()->exists('youtube_iframe.height')) {
return (int) $environment->getConfiguration()->get('youtube_iframe.height');
}

return 600;
}

protected function allowFullscreen(EnvironmentBuilderInterface $environment): bool
{
if ($environment->getConfiguration()->exists('youtube_allowfullscreen')) {
return (bool) $environment->getConfiguration()->get('youtube_allowfullscreen');
if ($environment->getConfiguration()->exists('youtube_iframe.allowfullscreen')) {
return (bool) $environment->getConfiguration()->get('youtube_iframe.allowfullscreen');
}

return true;
Expand Down

0 comments on commit c3a607d

Please sign in to comment.