From 171413c108d81a42699f7f55fb490e8894185aae Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 10 Nov 2023 04:12:02 +0000 Subject: [PATCH] add video embeding --- Utils/Parser/Markdown/Markdown.php | 79 ++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/Utils/Parser/Markdown/Markdown.php b/Utils/Parser/Markdown/Markdown.php index 4e400aa0b..578b45785 100755 --- a/Utils/Parser/Markdown/Markdown.php +++ b/Utils/Parser/Markdown/Markdown.php @@ -511,6 +511,13 @@ class Markdown if ($state !== false) { $this->blockTypes['['][] = 'Checkbox'; } + + // Embedding + $state = $this->options['embedding'] ?? false; + if ($state !== false) { + $this->inlineTypes['['][] = 'Embeding'; + $this->inlineMarkerList .= '['; + } } /** @@ -1233,6 +1240,78 @@ class Markdown ]; } + /** + * Handle embeding + * + * @param array{text:string, context:string, before:string} $excerpt Inline data + * + * @return null|array{extent:int, element:array} + * + * @since 1.0.0 + */ + protected function inlineEmbeding(array $excerpt) : ?array + { + if (!($this->options['embedding'] ?? false) + || \preg_match('/\[video.*src="([^"]*)".*\]/', $excerpt['text'], $matches) !== 1 + ) { + return null; + } + + $url = $matches[1]; + $type = ''; + + $needles = ['youtube', 'vimeo', 'dailymotion']; + foreach ($needles as $needle) { + if (\strpos($url, $needle) !== false) { + $type = $needle; + } + } + + switch ($type) { + case 'youtube': + $element = 'iframe'; + $attributes = [ + 'src' => preg_replace('/.*\?v=([^\&\]]*).*/', 'https://www.youtube.com/embed/$1', $url), + 'frameborder' => '0', + 'allow' => 'autoplay', + 'allowfullscreen' => '', + 'sandbox' => 'allow-same-origin allow-scripts allow-forms' + ]; + break; + case 'vimeo': + $element = 'iframe'; + $attributes = [ + 'src' => preg_replace('/(?:https?:\/\/(?:[\w]{3}\.|player\.)*vimeo\.com(?:[\/\w:]*(?:\/videos)?)?\/([0-9]+)[^\s]*)/', 'https://player.vimeo.com/video/$1', $url), + 'frameborder' => '0', + 'allow' => 'autoplay', + 'allowfullscreen' => '', + 'sandbox' => 'allow-same-origin allow-scripts allow-forms' + ]; + break; + case 'dailymotion': + $element = 'iframe'; + $attributes = [ + 'src' => $url, + 'frameborder' => '0', + 'allow' => 'autoplay', + 'allowfullscreen' => '', + 'sandbox' => 'allow-same-origin allow-scripts allow-forms' + ]; + break; + default: + $element = 'video'; + } + + return [ + 'extent' => strlen($matches[0]), + 'element' => [ + 'name' => $element, + 'text' => $matches[1], + 'attributes' => $attributes + ], + ]; + } + /** * Handle super script *