Skip to content

Instantly share code, notes, and snippets.

@0b10011
Created July 10, 2015 05:06
Show Gist options
  • Select an option

  • Save 0b10011/9017f38586404f161298 to your computer and use it in GitHub Desktop.

Select an option

Save 0b10011/9017f38586404f161298 to your computer and use it in GitHub Desktop.

Revisions

  1. 0b10011 created this gist Jul 10, 2015.
    28 changes: 28 additions & 0 deletions Converter.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    <?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)
    );
    }
    }
    5 changes: 5 additions & 0 deletions YouTubeVideo.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    <?php

    namespace CommonMark\Inline\Element;

    class YouTubeVideo extends \League\CommonMark\Inline\Element\Link {}
    37 changes: 37 additions & 0 deletions YouTubeVideoProcessor.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <?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
    ));
    }
    }
    }
    46 changes: 46 additions & 0 deletions YouTubeVideoRenderer.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    <?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;
    }
    }
    16 changes: 16 additions & 0 deletions style.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@

    .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%;
    }
    }