Skip to content

Instantly share code, notes, and snippets.

@aleemb
Forked from 0b10011/Converter.php
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleemb/a88e032f266da4bc9e6d to your computer and use it in GitHub Desktop.
Save aleemb/a88e032f266da4bc9e6d to your computer and use it in GitHub Desktop.
<?php
namespace CommonMark;
use CommonMark\Inline\Processor\YouTubeVideoProcessor;
use CommonMark\Inline\Renderer\YouTubeVideoRenderer;
use League\CommonMark\Converter;
use League\CommonMark\Environment;
use League\CommonMark\DocParser;
use League\CommonMark\HtmlRenderer;
class CommonMarkConverter extends Converter {
public function __construct(array $config = []){
$environment = new Environment($config);
$environment->addExtension(new Extension\CommonMarkExtension());
$environment->addInlineProcessor(new YouTubeVideoProcessor());
$environment->addInlineRenderer(
"CommonMark\Inline\Element\YouTubeVideo",
new YouTubeVideoRenderer()
);
parent::__construct(
new DocParser($environment),
new HtmlRenderer($environment)
);
}
}
.youtube-video {
display:block;
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 25px;
height: 0;
> iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
}
<?php
namespace CommonMark\Inline\Element;
class YouTubeVideo extends \League\CommonMark\Inline\Element\Link {}
<?php
namespace CommonMark\Inline\Processor;
use CommonMark\Inline\Element\YouTubeVideo;
use League\CommonMark\Inline\Processor\InlineProcessorInterface;
use League\CommonMark\Delimiter\Delimiter;
use League\CommonMark\Delimiter\DelimiterStack;
use League\CommonMark\Inline\Element\Link;
use League\CommonMark\Util\ArrayCollection;
class YouTubeVideoProcessor implements InlineProcessorInterface
{
public function processInlines(ArrayCollection $inlines, DelimiterStack $delimiterStack, Delimiter $stackBottom = null) {
foreach ($inlines as $key => $inline) {
if (!($inline instanceof Link)) {
continue;
}
$matched = preg_match(
'%^https://(?:www\.youtube\.com/watch\?v=|youtu\.be/)([^&#]+)%',
$inline->getUrl(),
$matches
);
if (!$matched) {
continue;
}
$inlines->set($key, new YouTubeVideo(
"https://youtu.be/$matches[1]",
$inline->getChildren(),
isset($inline->data['title']) ? $inline->data['title'] : null
));
}
}
}
<?php
namespace CommonMark\Inline\Renderer;
use CommonMark\Inline\Element\YouTubeVideo;
use League\CommonMark\HtmlElement;
use League\CommonMark\HtmlRendererInterface;
use League\CommonMark\Inline\Element\AbstractInline;
use League\CommonMark\Inline\Renderer\LinkRenderer;
class YouTubeVideoRenderer extends LinkRenderer {
public function render(
AbstractInline $inline,
HtmlRendererInterface $htmlRenderer
){
if (!($inline instanceof YouTubeVideo)) {
throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
}
$matched = preg_match(
'%^https://(?:www\.youtube\.com/watch\?v=|youtu\.be/)([^&#]+)%',
$inline->getUrl(),
$matches
);
if(!$matched){
return parent::render($inline, $htmlRenderer);
}
if (count($inline->getParent()->getInlines()) > 1) {
return parent::render($inline, $htmlRenderer);
}
$iframe = new HtmlElement('iframe', [
'width' => 640,
'height' => 390,
'src' => "https://www.youtube.com/embed/$matches[1]",
'type' => "text/html",
'frameborder' => 0,
]);
$div = new HtmlElement('span', ['class' => 'youtube-video'], $iframe);
return $div;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment