Skip to content

Commit 2c1c8c5

Browse files
authored
Merge pull request #6 from HardCoderNET/commonmark-2.2
Commonmark 2.2
2 parents 9af3802 + 355ec03 commit 2c1c8c5

14 files changed

+228
-137
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.idea/
22
/composer.lock
33
/vendor/
4-
.DS_Store
4+
.DS_Store
5+
.phpunit.result.cache

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,29 @@ Extension for [league/commonmark](https://github.com/thephpleague/commonmark) to
88
composer require zoon/commonmark-ext-youtube-iframe
99
```
1010

11+
## Test
12+
```
13+
./vendor/bin/phpunit
14+
```
15+
1116
## Example
1217

1318
``` php
14-
$environment = \League\CommonMark\Environment::createCommonMarkEnvironment();
15-
$environment->addExtension(new \Zoon\CommonMark\Ext\YouTubeIframe\YouTubeIframeExtension());
19+
use League\CommonMark\CommonMarkConverter;
20+
use Zoon\CommonMark\Ext\YouTubeIframe\YouTubeIframeExtension;
21+
22+
$converter = new CommonMarkConverter([
23+
'youtube_iframe' => [
24+
'width' => '600',
25+
'height' => '300',
26+
'allow_full_screen' => true,
27+
],
28+
]);
1629

17-
$converter = new \League\CommonMark\CommonMarkConverter([
18-
'youtube_iframe_width' => 600,
19-
'youtube_iframe_height' => 300,
20-
'youtube_iframe_allowfullscreen' => true,
21-
], $environment);
30+
$converter->getEnvironment()->addExtension(new YouTubeIframeExtension());
2231

23-
echo $converter->convertToHtml('Check this: [](https://youtu.be/mVnSpPMgoWM?t=10)');
24-
echo $converter->convertToHtml('Check this: [](https://www.youtube.com/watch?v=mVnSpPMgoWM&t=10)');
32+
echo $converter->convert('Check this: [](https://youtu.be/mVnSpPMgoWM?t=10)')->getContent();
33+
echo $converter->convert('Check this: [](https://www.youtube.com/watch?v=mVnSpPMgoWM&t=10)')->getContent();
2534
```
2635

2736
``` bash

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"homepage": "https://github.com/zoonru/commonmark-ext-youtube-iframe",
1313
"license": "MIT",
1414
"require": {
15-
"php" : "^7.1 || ^8.0",
16-
"league/commonmark": "^1.0"
15+
"php": "^7.4 || ^8.0",
16+
"league/commonmark": "^2.2"
1717
},
1818
"autoload": {
1919
"psr-4": {

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.0phpunit.xsd" colors="true" failOnRisky="true" failOnWarning="true" verbose="true">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.0phpunit.xsd" colors="true" failOnRisky="true" failOnWarning="true" verbose="true" cacheResult="false">
33
<coverage>
44
<include>
55
<directory suffix=".php">src</directory>

src/YouTubeIframe.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22

33
namespace Zoon\CommonMark\Ext\YouTubeIframe;
44

5-
use League\CommonMark\Inline\Element\AbstractInline;
5+
use League\CommonMark\Node\Node;
66

7-
final class YouTubeIframe extends AbstractInline {
7+
final class YouTubeIframe extends Node
8+
{
9+
private YouTubeUrlInterface $url;
810

9-
private $url;
11+
/**
12+
* @param YouTubeUrlInterface $url
13+
* @return $this
14+
*/
15+
public function setUrl(YouTubeUrlInterface $url): YouTubeIframe
16+
{
17+
$this->url = $url;
1018

11-
/**
12-
* YouTubeIframe constructor.
13-
* @param YouTubeUrlInterface $youTubeUrl
14-
*/
15-
public function __construct(YouTubeUrlInterface $youTubeUrl) {
16-
$this->url = $youTubeUrl;
17-
}
19+
return $this;
20+
}
1821

1922
/**
2023
* @return YouTubeUrlInterface
2124
*/
22-
public function getUrl(): YouTubeUrlInterface {
25+
public function getUrl(): YouTubeUrlInterface
26+
{
2327
return $this->url;
2428
}
25-
2629
}

src/YouTubeIframeExtension.php

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,46 @@
22

33
namespace Zoon\CommonMark\Ext\YouTubeIframe;
44

5-
use League\CommonMark\ConfigurableEnvironmentInterface;
5+
use League\CommonMark\Environment\EnvironmentBuilderInterface;
66
use League\CommonMark\Event\DocumentParsedEvent;
7-
use League\CommonMark\Extension\ExtensionInterface;
7+
use League\CommonMark\Extension\ConfigurableExtensionInterface;
8+
use League\Config\ConfigurationBuilderInterface;
9+
use Nette\Schema\Expect;
810

9-
final class YouTubeIframeExtension implements ExtensionInterface {
11+
class YouTubeIframeExtension implements ConfigurableExtensionInterface
12+
{
13+
/**
14+
* @param ConfigurationBuilderInterface $builder
15+
* @return void
16+
*/
17+
public function configureSchema(ConfigurationBuilderInterface $builder): void
18+
{
19+
$builder->addSchema('youtube_iframe', Expect::structure([
20+
'width' => Expect::string('640'),
21+
'height' => Expect::string('480'),
22+
'wrapper_class' => Expect::string()->nullable(),
23+
'allow_full_screen' => Expect::bool(true),
24+
]));
25+
}
1026

11-
/**
12-
* @param ConfigurableEnvironmentInterface $environment
13-
*/
14-
public function register(ConfigurableEnvironmentInterface $environment) {
15-
$environment
16-
->addEventListener(DocumentParsedEvent::class, new YouTubeIframeProcessor([
17-
new YouTubeLongUrlParser(),
18-
new YouTubeShortUrlParser(),
19-
]))
20-
->addInlineRenderer(YouTubeIframe::class, new YouTubeIframeRenderer(
21-
(string) $environment->getConfig('youtube_iframe_width', 640),
22-
(string) $environment->getConfig('youtube_iframe_height', 480),
23-
(bool) $environment->getConfig('youtube_iframe_allowfullscreen', true)
24-
))
25-
;
26-
}
27+
/**
28+
* @param EnvironmentBuilderInterface $environment
29+
* @return void
30+
*/
31+
public function register(EnvironmentBuilderInterface $environment): void
32+
{
33+
$configuration = $environment->getConfiguration();
2734

35+
$environment->addEventListener(DocumentParsedEvent::class, new YouTubeIframeProcessor([
36+
new YouTubeLongUrlParser(),
37+
new YouTubeShortUrlParser(),
38+
]));
39+
40+
$environment->addRenderer(YouTubeIframe::class, new YouTubeIframeRenderer([
41+
'width' => $configuration->get('youtube_iframe.width'),
42+
'height' => $configuration->get('youtube_iframe.height'),
43+
'wrapper_class' => $configuration->get('youtube_iframe.wrapper_class'),
44+
'allow_full_screen' => $configuration->get('youtube_iframe.allow_full_screen'),
45+
]));
46+
}
2847
}

src/YouTubeIframeProcessor.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,51 @@
33
namespace Zoon\CommonMark\Ext\YouTubeIframe;
44

55
use League\CommonMark\Event\DocumentParsedEvent;
6-
use League\CommonMark\Inline\Element\Link;
6+
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
7+
use TypeError;
78

8-
final class YouTubeIframeProcessor {
9-
10-
private $youTubeUrlParsers = [];
9+
final class YouTubeIframeProcessor
10+
{
11+
private array $youTubeUrlParsers = [];
1112

1213
/**
1314
* YouTubeIframeProcessor constructor.
1415
* @param YouTubeUrlParserInterface[] $youTubeUrlParsers
1516
*/
16-
public function __construct(array $youTubeUrlParsers) {
17+
public function __construct(array $youTubeUrlParsers)
18+
{
1719
foreach ($youTubeUrlParsers as $parser) {
1820
if (!($parser instanceof YouTubeUrlParserInterface)) {
19-
throw new \TypeError();
21+
throw new TypeError();
2022
}
2123
}
24+
2225
$this->youTubeUrlParsers = $youTubeUrlParsers;
2326
}
2427

2528
/**
2629
* @param DocumentParsedEvent $e
2730
*/
28-
public function __invoke(DocumentParsedEvent $e) {
31+
public function __invoke(DocumentParsedEvent $e)
32+
{
2933
$walker = $e->getDocument()->walker();
34+
3035
while ($event = $walker->next()) {
31-
if ($event->getNode() instanceof Link && !$event->isEntering()) {
36+
if ($event->getNode() instanceof Link && $event->isEntering()) {
3237
/** @var Link $link */
3338
$link = $event->getNode();
3439

3540
/** @var YouTubeUrlParserInterface $parser */
3641
foreach ($this->youTubeUrlParsers as $youTubeParser) {
3742
$youTubeUrl = $youTubeParser->parse($link->getUrl());
43+
3844
if ($youTubeUrl === null) {
3945
continue;
4046
}
41-
$link->replaceWith(new YouTubeIframe($youTubeUrl));
47+
48+
$link->replaceWith((new YouTubeIframe())->setUrl($youTubeUrl));
4249
}
4350
}
4451
}
4552
}
46-
4753
}

src/YouTubeIframeRenderer.php

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,58 @@
22

33
namespace Zoon\CommonMark\Ext\YouTubeIframe;
44

5-
use League\CommonMark\ElementRendererInterface;
6-
use League\CommonMark\HtmlElement;
7-
use League\CommonMark\Inline\Element\AbstractInline;
8-
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
9-
10-
final class YouTubeIframeRenderer implements InlineRendererInterface {
11-
12-
private $width;
13-
private $height;
14-
private $allowFullScreen;
15-
16-
/**
17-
* YouTubeIframeRenderer constructor.
18-
* @param string $width
19-
* @param string $height
20-
* @param bool $allowFullScreen
21-
*/
22-
public function __construct(string $width, string $height, bool $allowFullScreen) {
23-
$this->width = $width;
24-
$this->height = $height;
25-
$this->allowFullScreen = $allowFullScreen;
26-
}
27-
28-
/**
29-
* @inheritDoc
30-
*/
31-
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) {
32-
if (!($inline instanceof YouTubeIframe)) {
33-
throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
34-
}
35-
$src = "https://www.youtube.com/embed/{$inline->getUrl()->getVideoId()}";
36-
$startTimestamp = $inline->getUrl()->getStartTimestamp();
37-
if ($startTimestamp !== null) {
38-
$src .= "?start={$startTimestamp}";
39-
}
40-
return new HtmlElement('iframe', [
41-
'width' => $this->width,
42-
'height' => $this->height,
43-
'src' => $src,
44-
'frameborder' => 0,
45-
'allowfullscreen' => $this->allowFullScreen,
46-
]);
47-
}
5+
use InvalidArgumentException;
6+
use League\CommonMark\Node\Node;
7+
use League\CommonMark\Renderer\ChildNodeRendererInterface;
8+
use League\CommonMark\Renderer\NodeRendererInterface;
9+
use League\CommonMark\Util\HtmlElement;
10+
11+
final class YouTubeIframeRenderer implements NodeRendererInterface
12+
{
13+
private string $width;
14+
private string $height;
15+
private ?string $wrapperClass;
16+
private bool $allowFullScreen;
17+
18+
/**
19+
* YouTubeIframeRenderer constructor.
20+
* @param array $props
21+
*/
22+
public function __construct(array $props = [])
23+
{
24+
$this->width = $props['width'] ?? '';
25+
$this->height = $props['height'] ?? '';
26+
$this->wrapperClass = $props['wrapper_class'] ?? null;
27+
$this->allowFullScreen = $props['allow_full_screen'] ?? true;
28+
}
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
public function render(Node $node, ChildNodeRendererInterface $childRenderer): HtmlElement
34+
{
35+
if (!($node instanceof YouTubeIframe)) {
36+
throw new InvalidArgumentException('Incompatible inline type: ' . get_class($node));
37+
}
38+
39+
$src = "https://www.youtube.com/embed/{$node->getUrl()->getVideoId()}";
40+
$startTimestamp = $node->getUrl()->getStartTimestamp();
41+
42+
if ($startTimestamp !== null) {
43+
$src .= "?start=$startTimestamp";
44+
}
45+
46+
$iframeElement = new HtmlElement('iframe', array_merge([
47+
'width' => $this->width,
48+
'height' => $this->height,
49+
'src' => $src,
50+
'frameborder' => "0",
51+
], $this->allowFullScreen ? [
52+
'allowfullscreen' => "1",
53+
] : []));
54+
55+
return is_null($this->wrapperClass) ? $iframeElement : new HtmlElement('div', [
56+
'class' => $this->wrapperClass,
57+
], $iframeElement);
58+
}
4859
}

src/YouTubeLongUrlParser.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
namespace Zoon\CommonMark\Ext\YouTubeIframe;
44

5-
final class YouTubeLongUrlParser implements YouTubeUrlParserInterface {
6-
5+
final class YouTubeLongUrlParser implements YouTubeUrlParserInterface
6+
{
77
private const HOST = 'www.youtube.com';
88
private const PATH = '/watch';
99
private const TIMESTAMP_GET = [
1010
't',
1111
'time_continue',
1212
'start',
1313
];
14+
1415
private const ID_GET = 'v';
1516

1617
/**
1718
* @param string $url
1819
* @return YouTubeUrlInterface|null
1920
*/
20-
public function parse(string $url): ?YouTubeUrlInterface {
21+
public function parse(string $url): ?YouTubeUrlInterface
22+
{
2123
if (parse_url($url, PHP_URL_HOST) !== self::HOST || parse_url($url, PHP_URL_PATH) !== self::PATH) {
2224
return null;
2325
}
@@ -36,5 +38,4 @@ public function parse(string $url): ?YouTubeUrlInterface {
3638

3739
return new YouTubeUrl($getParams[self::ID_GET]);
3840
}
39-
4041
}

src/YouTubeShortUrlParser.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Zoon\CommonMark\Ext\YouTubeIframe;
44

5-
final class YouTubeShortUrlParser implements YouTubeUrlParserInterface {
6-
5+
final class YouTubeShortUrlParser implements YouTubeUrlParserInterface
6+
{
77
private const HOST = 'youtu.be';
88
private const TIMESTAMP_GET = [
99
't',
@@ -15,7 +15,8 @@ final class YouTubeShortUrlParser implements YouTubeUrlParserInterface {
1515
* @param string $url
1616
* @return YouTubeUrlInterface|null
1717
*/
18-
public function parse(string $url): ?YouTubeUrlInterface {
18+
public function parse(string $url): ?YouTubeUrlInterface
19+
{
1920
if (parse_url($url, PHP_URL_HOST) !== self::HOST) {
2021
return null;
2122
}
@@ -35,5 +36,4 @@ public function parse(string $url): ?YouTubeUrlInterface {
3536

3637
return new YouTubeUrl($videoId);
3738
}
38-
3939
}

0 commit comments

Comments
 (0)