Skip to content

Commit

Permalink
feat!: CommonMark v2 and PHP 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
trovster committed Apr 13, 2022
1 parent 75798c6 commit 59c6ac1
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 199 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-20.04]
php: [7.4]
php: [8.0]

steps:
- name: Checkout repository
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2022-04-13

### Added

- Update to PHP 8.0 and CommonMark v2.

## [1.0.0] - 2022-04-13

### Added
Expand Down
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# YouTube iframe extension

An extension for [league/commonmark](https://github.com/thephpleague/commonmark)
version 1 built using PHP 7.4. This replaces YouTube links with the embed iframe.
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.
Expand All @@ -21,25 +21,24 @@ composer require surface/commonmark-ext-youtube-iframe
Configure your CommonMark `Environment` and add the extension.

```php
use League\CommonMark\CommonMarkConverter as Converter;
// use League\CommonMark\GithubFlavoredMarkdownConverter as Converter;
use League\CommonMark\Environment;
use League\CommonMark\Environment\Environment;
use League\CommonMark\MarkdownConverter as Converter;
use Surface\CommonMark\Ext\YouTubeIframe\Extension as YouTubeExtension;

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

$config = [
$options = [
'youtube_width' => '800',
'youtube_height' => '600',
];

$converter = new Converter($config, $environment);
$environment = new Environment($options);
$environment->addExtension(new YouTubeExtension());

$converter = new Converter($environment);

echo $converter->convertToHtml('[](https://youtu.be/xxx)');
echo $converter->convertToHtml('[](https://www.youtube.com/watch?v=xxx)');
echo $converter->convertToHtml('[](https://youtu.be/xxx?height=480)');
echo $converter->convertToHtml('[](https://www.youtube.com/watch?v=xxx&width=640)');
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
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "surface/commonmark-ext-youtube-iframe",
"type": "commonmark-extension",
"description": "Extension for league/commonmark to replace YouTube link with an iframe.",
"version": "1.0.0",
"version": "2.0.0",
"keywords": [
"markdown",
"commonmark",
Expand All @@ -23,8 +23,8 @@
}
],
"require": {
"php" : "^7.4|^8.0",
"league/commonmark": "^1.6"
"php" : "^8.0",
"league/commonmark": "^2.3"
},
"require-dev": {
"phpmd/phpmd": "^2.12",
Expand Down
2 changes: 1 addition & 1 deletion grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ grumphp:
with_dependencies: false
strict: false
phpversion:
project: '7.4'
project: '8.0'
phpstan:
configuration: phpstan.neon
memory_limit: "2G"
Expand Down
6 changes: 2 additions & 4 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@

<rule ref="SlevomatCodingStandard.Functions">
<exclude name="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall.MissingTrailingComma"/>
<exclude name="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration.MissingTrailingComma"/>
</rule>

<rule ref="SlevomatCodingStandard.Classes">
<exclude name="SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces"/>
<exclude name="SlevomatCodingStandard.Classes.DisallowConstructorPropertyPromotion.DisallowedConstructorPropertyPromotion"/>
</rule>

<rule ref="SlevomatCodingStandard.Namespaces">
Expand Down Expand Up @@ -67,8 +69,4 @@
<rule ref="SlevomatCodingStandard.Files.LineLength">
<exclude-pattern>/tests/TestCase.php</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint">
<exclude-pattern>/tests/Unit/Url/ParserTest.php</exclude-pattern>
</rule>
</ruleset>
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ parameters:
- src
- tests
ignoreErrors:
-
message: "#^Parameter \\#2 \\$attributes of class League\\\\CommonMark\\\\HtmlElement constructor expects array\\<string\\>, array\\<string, bool\\|int\\|string\\> given\\.$#"
count: 1
path: src/Renderer.php
-
message: "#^Cannot call method (getType|getUuid|getTimestamp|getWidth|getHeight)\\(\\) on [A-Za-z\\\\]+\\\\Url\\\\Url\\|null\\.$#"
path: tests/Unit/Url
Expand Down
40 changes: 33 additions & 7 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Surface\CommonMark\Ext\YouTubeIframe;

use League\CommonMark\ConfigurableEnvironmentInterface;
use League\CommonMark\Environment\EnvironmentBuilderInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\ExtensionInterface;
use Surface\CommonMark\Ext\YouTubeIframe\Iframe;
Expand All @@ -14,24 +14,50 @@

final class Extension implements ExtensionInterface
{
/** @return void */
public function register(ConfigurableEnvironmentInterface $environment)
public function register(EnvironmentBuilderInterface $environment): void
{
$width = (int) $environment->getConfig('youtube_width', 800);
$height = (int) $environment->getConfig('youtube_height', 600);
$fullScreen = (bool) $environment->getConfig('youtube_allowfullscreen', true);
$width = $this->getWidth($environment);
$height = $this->getHeight($environment);
$fullScreen = $this->allowFullscreen($environment);

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

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

return 800;
}

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

return 600;
}

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

return true;
}
}
6 changes: 2 additions & 4 deletions src/Iframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

namespace Surface\CommonMark\Ext\YouTubeIframe;

use League\CommonMark\Inline\Element\AbstractInline;
use League\CommonMark\Node\Inline\AbstractInline;
use Surface\CommonMark\Ext\YouTubeIframe\Url\Contracts\Url;

final class Iframe extends AbstractInline
{
protected Url $url;

public function __construct(Url $url)
public function __construct(protected Url $url)
{
$this->url = $url;
}
Expand Down
14 changes: 2 additions & 12 deletions src/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
namespace Surface\CommonMark\Ext\YouTubeIframe;

use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Inline\Element\Link;
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
use Surface\CommonMark\Ext\YouTubeIframe\Iframe;
use Surface\CommonMark\Ext\YouTubeIframe\Url\Contracts\Parser;
use TypeError;

final class Processor
{
protected array $parsers;

public function __construct(array $parsers)
public function __construct(protected array $parsers)
{
foreach ($parsers as $parser) {
if (! ($parser instanceof Parser)) {
Expand Down Expand Up @@ -43,14 +41,6 @@ public function __invoke(DocumentParsedEvent $event): void
$walker = $event->getDocument()->walker();

while ($item = $walker->next()) {
if (! ($item->getNode() instanceof Link)) {
continue;
}

if ($item->isEntering()) {
continue;
}

$link = $item->getNode();

if (! ($link instanceof Link)) {
Expand Down
31 changes: 12 additions & 19 deletions src/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,24 @@
namespace Surface\CommonMark\Ext\YouTubeIframe;

use InvalidArgumentException;
use League\CommonMark\ElementRendererInterface;
use League\CommonMark\HtmlElement;
use League\CommonMark\Inline\Element\AbstractInline;
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;
use League\CommonMark\Util\HtmlElement;
use Surface\CommonMark\Ext\YouTubeIframe\Iframe;

final class Renderer implements InlineRendererInterface
final class Renderer implements NodeRendererInterface
{
protected int $height;
protected int $width;
protected bool $allowFullScreen;

public function __construct(int $width, int $height, bool $allowFullScreen = false)
public function __construct(protected int $width, protected int $height, protected bool $allowFullScreen = false)
{
$this->width = $width;
$this->height = $height;
$this->allowFullScreen = $allowFullScreen;
}

/** @return \League\CommonMark\HtmlElement|string|null */
/** @return \League\CommonMark\Util\HtmlElement|string|null */
// phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
public function render(Node $inline, ChildNodeRendererInterface $htmlRenderer)
{
if (! ($inline instanceof Iframe)) {
throw new InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
throw new InvalidArgumentException('Incompatible inline type: ' . $inline::class);
}

$src = "https://www.youtube.com/embed/{$inline->getUrl()->getUuid()}";
Expand All @@ -40,10 +33,10 @@ public function render(AbstractInline $inline, ElementRendererInterface $htmlRen
}

return new HtmlElement('iframe', [
'width' => $width ?: $this->width,
'height' => $height ?: $this->height,
'width' => (string) ($width ?: $this->width),
'height' => (string) ($height ?: $this->height),
'src' => $src,
'frameborder' => 0,
'frameborder' => '0',
'allowfullscreen' => $this->allowFullScreen,
'allow' => 'autoplay; fullscreen; picture-in-picture',
]);
Expand Down
6 changes: 1 addition & 5 deletions src/Url/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ abstract class Parser implements Contract
{
protected string $host = '';
protected string $type;
protected int $defaultWidth;
protected int $defaultHeight;

abstract protected function getUuid(string $url): ?string;

public function __construct(int $defaultWidth = 800, int $defaultHeight = 600)
public function __construct(protected int $defaultWidth = 800, protected int $defaultHeight = 600)
{
$this->setType();
$this->defaultWidth = $defaultWidth;
$this->defaultHeight = $defaultHeight;
}

public function parse(string $url): ?Url
Expand Down
20 changes: 7 additions & 13 deletions src/Url/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@

final class Url implements Contract
{
protected string $type;
protected string $uuid;
protected ?int $width;
protected ?int $height;
protected ?int $timestamp;

public function __construct(string $type, string $uuid, ?int $width, ?int $height, ?int $timestamp)
{
$this->type = $type;
$this->uuid = $uuid;
$this->width = $width;
$this->height = $height;
$this->timestamp = $timestamp;
public function __construct(
protected string $type,
protected string $uuid,
protected ?int $width,
protected ?int $height,
protected ?int $timestamp
) {
}

public function getType(): string
Expand Down
Loading

0 comments on commit 59c6ac1

Please sign in to comment.